阅读文本大概需要3分钟。
虽然json作为数据传输的格式大型其道,但是使用xml格式传输的系统还是在一些存量的系统中存在。另外WebService本身就是使用xml格式进行数据传输。今天用个小例子看看Spring Boot如何实现xml传参和返回值。
1、新建maven项目,添加依赖
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.jemter</groupId>
<artifactId>com-lesson17</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>com-lesson1</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</dependency>
</dependencies>
</project>
jackson-dataformat-xml是xml和bean转换依赖的包
2、新建实体类,并添加xml和和bean转换的注解(注解要写在get方法上)
教师实体类
package com.lesson17.model;
import java.util.List;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
@JacksonXmlRootElement(localName = "MESSAGE")
public class Teacher {
private Integer id;
private String teacherName;
private List<Student> studentList;
@JacksonXmlProperty(isAttribute = true, localName = "TEACHER_ID")
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@JacksonXmlProperty(localName = "TEACHER_NAME")
public String getTeacherName() {
return teacherName;
}
public void setTeacherName(String teacherName) {
this.teacherName = teacherName;
}
@JacksonXmlElementWrapper(localName = "STUDENT_LIST")
@JacksonXmlProperty(localName = "STUDENT")
public List<Student> getStudentList() {
return studentList;
}
public void setStudentList(List<Student> studentList) {
this.studentList = studentList;
}
@Override
public String toString() {
return "Teacher{" + "id=" + id + ", teacherName=" + teacherName + ", studentList=" + studentList + "}";
}
}
学生实体类
package com.lesson17.model;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
public class Student {
private Integer id;
private String stuName;
private String sex;
@JacksonXmlProperty(isAttribute = true, localName = "STUDENT_ID")
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@JacksonXmlProperty(localName = "STUDENT_NAME")
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
@JacksonXmlProperty(localName = "STUDENT_SEX")
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "Student{" + "id=" + id + ", stuName=" + stuName + ", sex=" + sex + "}";
}
}
3、编程控制类
package com.lesson17.controller;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.lesson17.model.Student;
import com.lesson17.model.Teacher;
@RestController
@RequestMapping("/teacher")
public class TeacherController {
@RequestMapping(value = "/getInfo", method = RequestMethod.GET, produces = "application/xml")
@ResponseBody
public Teacher getInfo() {
Student student1 = new Student();
student1.setId(1);
student1.setStuName("张三");
student1.setSex("男");
Student student2 = new Student();
student2.setId(2);
student2.setStuName("李四");
student2.setSex("男");
Teacher teacher = new Teacher();
teacher.setId(11);
teacher.setTeacherName("杨老师");
teacher.setStudentList(Arrays.asList(student1, student2));
return teacher;
}
@RequestMapping(value = "/postInfo", method = RequestMethod.POST, consumes = "application/xml")
public Map<String, Object> postInfo(@RequestBody Teacher teacher) {
System.out.println("postman传过来的xml信息转换成实体类如下:==========" + teacher.toString());
Map<String, Object> results = new HashMap<String, Object>();
results.put("code", "000000");
results.put("msg", "success");
return results;
}
}
注:关键步骤是RequestMapping注解的produces和consumes这两个属性,如果参数是xml,则需要把consumes配置成application/xml;如果是返回值是xml,则需要把把produces配置成application/xml。
4、编写SpringBoot启动类
package com.lesson17;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
5、application.yml配置如下
server:
port: 8080
servlet:
context-path: /lesson17
spring:
application:
name: jmeter-lesson17
6、启动验证
请求http://127.0.0.1:8080/lesson17/teacher/getInfo接口
请求http://127.0.0.1:8080/lesson17/teacher/postInfo接口
☆
往期精彩
☆
03 精讲Spring Boot—入门+进阶+实例
关注我
每天进步一点点
喜欢!在看☟