springboot实现xml传参和返回值

1、新建maven工程xml-bean-convert

springboot实现xml传参和返回值_第1张图片

pom.xml如下

xml version="1.0" encoding="UTF-8"?>
<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.0modelVersion>

    <groupId>xml.bean.convertgroupId>
    <artifactId>xml-bean-convertartifactId>
    <version>1.0-SNAPSHOTversion>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>1.4.1.RELEASEversion>
    parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        
            com.fasterxml.jackson.dataformat
            jackson-dataformat-xml
            2.9.8
        
    dependencies>

project>

红色加粗部分是xml和bean转换依赖的包

2、启动类

@SpringBootApplication
public class Application {
    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }
}

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

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 + '\'' +
                '}';
    }
}
@JacksonXmlRootElement(localName ="MESSAGE")
public class Teacher {
    private Integer id;
    private String teacherName;
    private List 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 getStudentList() {
        return studentList;
    }

    public void setStudentList(List studentList) {
        this.studentList = studentList;
    }

    @Override
    public String toString() {
        return "Teacher{" +
                "id=" + id +
                ", teacherName='" + teacherName + '\'' +
                ", studentList=" + studentList +
                '}';
    }
}

4、写controller进行测试

@RestController
@RequestMapping("/teacher")
public class TeacherController {

    @RequestMapping(value = "/get-info",method = RequestMethod.GET, produces = "application/xml")
    @ResponseBody
    public Teacher getTest(){
        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 = "/post-info",method = RequestMethod.POST, consumes = "application/xml")
    public void postTest(@RequestBody Teacher teacher){
        System.out.println("postman传过来的xml信息转换成实体类如下:==========");
        System.out.println(teacher.toString());
    }
}

 

1)首先用postman测试返回xml

springboot实现xml传参和返回值_第2张图片

 

 2)首先用postman测试用xml格式传参

springboot实现xml传参和返回值_第3张图片

springboot实现xml传参和返回值_第4张图片

 

转载于:https://www.cnblogs.com/boshen-hzb/p/10334825.html

你可能感兴趣的:(springboot实现xml传参和返回值)