Spring Boot实现xml传参和返回值

阅读文本大概需要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. <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接口

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

往期精彩

01 漫谈发版哪些事,好课程推荐

02 Linux的常用最危险的命令

03 精讲Spring&nbsp;Boot—入门+进阶+实例

04 优秀的Java程序员必须了解的GC哪些

05 互联网支付系统整体架构详解

关注我

每天进步一点点

喜欢!在看☟

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

BUG弄潮儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值