springboot获取xml中的数据(Boot实现xml数据格式传输)

虽然json作为数据传输的格式大型其道,但是使用xml格式传输的系统还是在一些存量的系统中存在。另外WebService本身就是使用xml格式进行数据传输。今天用个小例子看看Spring Boot如何实现xml传参和返回值。

1、新建maven项目,添加依赖

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>com.jemter</groupId>
  5. <artifactId>com-lesson17</artifactId>
  6. <version>0.0.1-SNAPSHOT</version>
  7. <packaging>jar</packaging>
  8. <name>com-lesson1</name>
  9. <url>http://maven.apache.org</url>
  10. <parent>
  11. <groupId>org.springframework.boot</groupId>
  12. <artifactId>spring-boot-starter-parent</artifactId>
  13. <version>2.0.4.RELEASE</version>
  14. </parent>
  15. <properties>
  16. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  17. </properties>
  18. <dependencies>
  19. <dependency>
  20. <groupId>org.springframework.boot</groupId>
  21. <artifactId>spring-boot-starter-web</artifactId>
  22. </dependency>
  23. <dependency>
  24. <groupId>com.fasterxml.jackson.dataformat</groupId>
  25. <artifactId>jackson-dataformat-xml</artifactId>
  26. </dependency>
  27. </dependencies>
  28. </project>

jackson-dataformat-xml是xml和bean转换依赖的包

2、新建实体类,并添加xml和和bean转换的注解(注解要写在get方法上)

教师实体类

  1. package com.lesson17.model;
  2. import java.util.List;
  3. import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
  4. import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
  5. import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
  6. @JacksonXmlRootElement(localName = "MESSAGE")
  7. public class Teacher {
  8. private Integer id;
  9. private String teacherName;
  10. private List<Student> studentList;
  11. @JacksonXmlProperty(isAttribute = true, localName = "TEACHER_ID")
  12. public Integer getId() {
  13. return id;
  14. }
  15. public void setId(Integer id) {
  16. this.id = id;
  17. }
  18. @JacksonXmlProperty(localName = "TEACHER_NAME")
  19. public String getTeacherName() {
  20. return teacherName;
  21. }
  22. public void setTeacherName(String teacherName) {
  23. this.teacherName = teacherName;
  24. }
  25. @JacksonXmlElementWrapper(localName = "STUDENT_LIST")
  26. @JacksonXmlProperty(localName = "STUDENT")
  27. public List<Student> getStudentList() {
  28. return studentList;
  29. }
  30. public void setStudentList(List<Student> studentList) {
  31. this.studentList = studentList;
  32. }
  33. @Override
  34. public String toString() {
  35. return "Teacher{" "id=" id ", teacherName=" teacherName ", studentList=" studentList "}";
  36. }
  37. }

学生实体类

  1. package com.lesson17.model;
  2. import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
  3. public class Student {
  4. private Integer id;
  5. private String stuName;
  6. private String sex;
  7. @JacksonXmlProperty(isAttribute = true, localName = "STUDENT_ID")
  8. public Integer getId() {
  9. return id;
  10. }
  11. public void setId(Integer id) {
  12. this.id = id;
  13. }
  14. @JacksonXmlProperty(localName = "STUDENT_NAME")
  15. public String getStuName() {
  16. return stuName;
  17. }
  18. public void setStuName(String stuName) {
  19. this.stuName = stuName;
  20. }
  21. @JacksonXmlProperty(localName = "STUDENT_SEX")
  22. public String getSex() {
  23. return sex;
  24. }
  25. public void setSex(String sex) {
  26. this.sex = sex;
  27. }
  28. @Override
  29. public String toString() {
  30. return "Student{" "id=" id ", stuName=" stuName ", sex=" sex "}";
  31. }
  32. }

3、编程控制类

  1. package com.lesson17.controller;
  2. import java.util.Arrays;
  3. import java.util.HashMap;
  4. import java.util.Map;
  5. import org.springframework.web.bind.annotation.RequestBody;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.bind.annotation.RequestMethod;
  8. import org.springframework.web.bind.annotation.ResponseBody;
  9. import org.springframework.web.bind.annotation.RestController;
  10. import com.lesson17.model.Student;
  11. import com.lesson17.model.Teacher;
  12. @RestController
  13. @RequestMapping("/teacher")
  14. public class TeacherController {
  15. @RequestMapping(value = "/getInfo", method = RequestMethod.GET, produces = "application/xml")
  16. @ResponseBody
  17. public Teacher getInfo() {
  18. Student student1 = new Student();
  19. student1.setId(1);
  20. student1.setStuName("张三");
  21. student1.setSex("男");
  22. Student student2 = new Student();
  23. student2.setId(2);
  24. student2.setStuName("李四");
  25. student2.setSex("男");
  26. Teacher teacher = new Teacher();
  27. teacher.setId(11);
  28. teacher.setTeacherName("杨老师");
  29. teacher.setStudentList(Arrays.asList(student1, student2));
  30. return teacher;
  31. }
  32. @RequestMapping(value = "/postInfo", method = RequestMethod.POST, consumes = "application/xml")
  33. public Map<String, Object> postInfo(@RequestBody Teacher teacher) {
  34. System.out.println("postman传过来的xml信息转换成实体类如下:==========" teacher.toString());
  35. Map<String, Object> results = new HashMap<String, Object>();
  36. results.put("code", "000000");
  37. results.put("msg", "success");
  38. return results;
  39. }
  40. }

注:关键步骤是RequestMapping注解的produces和consumes这两个属性,如果参数是xml,则需要把consumes配置成application/xml;如果是返回值是xml,则需要把把produces配置成application/xml。

4、编写SpringBoot启动类

  1. package com.lesson17;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. @SpringBootApplication
  5. public class Application {
  6. public static void main(String[] args) {
  7. SpringApplication.run(Application.class, args);
  8. }
  9. }

5、application.yml配置如下

  1. server:
  2. port: 8080
  3. servlet:
  4. context-path: /lesson17
  5. spring:
  6. application:
  7. name: jmeter-lesson17

6、启动验证

请求http://127.0.0.1:8080/lesson17/teacher/getInfo接口

springboot获取xml中的数据(Boot实现xml数据格式传输)(1)

请求http://127.0.0.1:8080/lesson17/teacher/postInfo接口

springboot获取xml中的数据(Boot实现xml数据格式传输)(2)

,

免责声明:本文仅代表文章作者的个人观点,与本站无关。其原创性、真实性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容文字的真实性、完整性和原创性本站不作任何保证或承诺,请读者仅作参考,并自行核实相关内容。文章投诉邮箱:anhduc.ph@yahoo.com

    分享
    投诉
    首页