阅读文本大概需要3分钟。
虽然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.
5. <groupId>com.jemter</groupId>
6. <artifactId>com-lesson17</artifactId>
7. <version>0.0.1-SNAPSHOT</version>
8. <packaging>jar</packaging>
9.
10. <name>com-lesson1</name>
11. <url>http://maven.apache.org</url>
12.
13. <parent>
14. <groupId>org.springframework.boot</groupId>
15. <artifactId>spring-boot-starter-parent</artifactId>
16. <version>2.0.4.RELEASE</version>
17. </parent>
18.
19. <properties>
20. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
21. </properties>
22.
23. <dependencies>
24. <dependency>
25. <groupId>org.springframework.boot</groupId>
26. <artifactId>spring-boot-starter-web</artifactId>
27. </dependency>
28.
29. <dependency>
30. <groupId>com.fasterxml.jackson.dataformat</groupId>
31. <artifactId>jackson-dataformat-xml</artifactId>
32. </dependency>
33. </dependencies>
34. </project>
jackson-dataformat-xml是xml和bean转换依赖的包
2、新建实体类,并添加xml和和bean转换的注解(注解要写在get方法上)
教师实体类
1. package com.lesson17.model;
2.
3. import java.util.List;
4.
5. import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
6. import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
7. import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
8.
9. @JacksonXmlRootElement(localName = "MESSAGE")
10. public class Teacher {
11.
12. private Integer id;
13.
14. private String teacherName;
15.
16. private List<Student> studentList;
17.
18. @JacksonXmlProperty(isAttribute = true, localName = "TEACHER_ID")
19. public Integer getId() {
20. return id;
21. }
22.
23. public void setId(Integer id) {
24. this.id = id;
25. }
26.
27. @JacksonXmlProperty(localName = "TEACHER_NAME")
28. public String getTeacherName() {
29. return teacherName;
30. }
31.
32. public void setTeacherName(String teacherName) {
33. this.teacherName = teacherName;
34. }
35.
36. @JacksonXmlElementWrapper(localName = "STUDENT_LIST")
37. @JacksonXmlProperty(localName = "STUDENT")
38. public List<Student> getStudentList() {
39. return studentList;
40. }
41.
42. public void setStudentList(List<Student> studentList) {
43. this.studentList = studentList;
44. }
45.
46. @Override
47. public String toString() {
48. return "Teacher{" + "id=" + id + ", teacherName=" + teacherName + ", studentList=" + studentList + "}";
49. }
50. }
学生实体类
1. package com.lesson17.model;
2.
3. import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
4.
5. public class Student {
6. private Integer id;
7.
8. private String stuName;
9.
10. private String sex;
11.
12. @JacksonXmlProperty(isAttribute = true, localName = "STUDENT_ID")
13. public Integer getId() {
14. return id;
15. }
16.
17. public void setId(Integer id) {
18. this.id = id;
19. }
20.
21. @JacksonXmlProperty(localName = "STUDENT_NAME")
22. public String getStuName() {
23. return stuName;
24. }
25.
26. public void setStuName(String stuName) {
27. this.stuName = stuName;
28. }
29.
30. @JacksonXmlProperty(localName = "STUDENT_SEX")
31. public String getSex() {
32. return sex;
33. }
34.
35. public void setSex(String sex) {
36. this.sex = sex;
37. }
38.
39. @Override
40. public String toString() {
41. return "Student{" + "id=" + id + ", stuName=" + stuName + ", sex=" + sex + "}";
42. }
43. }
3、编程控制类
1. package com.lesson17.controller;
2.
3. import java.util.Arrays;
4. import java.util.HashMap;
5. import java.util.Map;
6.
7. import org.springframework.web.bind.annotation.RequestBody;
8. import org.springframework.web.bind.annotation.RequestMapping;
9. import org.springframework.web.bind.annotation.RequestMethod;
10. import org.springframework.web.bind.annotation.ResponseBody;
11. import org.springframework.web.bind.annotation.RestController;
12.
13. import com.lesson17.model.Student;
14. import com.lesson17.model.Teacher;
15.
16. @RestController
17. @RequestMapping("/teacher")
18. public class TeacherController {
19.
20. @RequestMapping(value = "/getInfo", method = RequestMethod.GET, produces = "application/xml")
21. @ResponseBody
22. public Teacher getInfo() {
23. Student student1 = new Student();
24. student1.setId(1);
25. student1.setStuName("张三");
26. student1.setSex("男");
27. Student student2 = new Student();
28. student2.setId(2);
29. student2.setStuName("李四");
30. student2.setSex("男");
31. Teacher teacher = new Teacher();
32. teacher.setId(11);
33. teacher.setTeacherName("杨老师");
34. teacher.setStudentList(Arrays.asList(student1, student2));
35. return teacher;
36. }
37.
38. @RequestMapping(value = "/postInfo", method = RequestMethod.POST, consumes = "application/xml")
39. public Map<String, Object> postInfo(@RequestBody Teacher teacher) {
40. System.out.println("postman传过来的xml信息转换成实体类如下:==========" + teacher.toString());
41. Map<String, Object> results = new HashMap<String, Object>();
42. results.put("code", "000000");
43. results.put("msg", "success");
44. return results;
45. }
46. }
注:关键步骤是RequestMapping注解的produces和consumes这两个属性,如果参数是xml,则需要把consumes配置成application/xml;如果是返回值是xml,则需要把把produces配置成application/xml。
4、编写SpringBoot启动类
1. package com.lesson17;
2.
3. import org.springframework.boot.SpringApplication;
4. import org.springframework.boot.autoconfigure.SpringBootApplication;
5.
6. @SpringBootApplication
7. public class Application {
8.
9. public static void main(String[] args) {
10. SpringApplication.run(Application.class, args);
11. }
12.
13. }
5、application.yml配置如下
1. server:
2. port: 8080
3. servlet:
4. context-path: /lesson17
5.
6. spring:
7. application:
8. name: jmeter-lesson17
6、启动验证
请求http://127.0.0.1:8080/lesson17/teacher/getInfo接口
请求http://127.0.0.1:8080/lesson17/teacher/postInfo接口
关注我
每天进步一点点