波神

导航

springboot实现xml传参和返回值

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

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.0</modelVersion>

    <groupId>xml.bean.convert</groupId>
    <artifactId>xml-bean-convert</artifactId>
    <version>1.0-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.1.RELEASE</version>
    </parent>
    <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>
            <version>2.9.8</version>
        </dependency>
    </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<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 +
                '}';
    }
}

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

 

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

 

posted on 2019-01-29 17:01  波神  阅读(17437)  评论(0编辑  收藏  举报