通过springboot提供请求参数以及返回值为XML的服务

说明:参考https://www.cnblogs.com/boshen-hzb/p/10334825.html实现

1:build.gradle增加以下依赖

compile group: 'com.fasterxml.jackson.dataformat', name: 'jackson-dataformat-xml', version: '2.11.1'

2:StudentVO

package com.wzh.app.test;

import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;

public class StudentVO {
    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;
    }
}

3:TeacherVO

package com.wzh.app.test;

import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;

import java.util.List;

@JacksonXmlRootElement(localName ="MESSAGE")
public class TeacherVO {

    private Integer id;
    private String teacherName;
    private List<StudentVO> 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<StudentVO> getStudentList() {
        return studentList;
    }

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

4:TestCtrl

package com.wzh.app.test;

import org.springframework.web.bind.annotation.*;

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

    @RequestMapping(value = "/post-info",method = RequestMethod.POST, consumes = "application/xml",produces = "application/xml")
    @ResponseBody
    public TeacherVO postTest(@RequestBody TeacherVO teacher){
        return teacher;
    }

}

5:通过postman进行测试

请求参数:

<MESSAGE TEACHER_ID="0001">
    <TEACHER_NAME>张老师</TEACHER_NAME>
    <STUDENT_LIST>
        <STUDENT STUDENT_ID="001">
            <STUDENT_NAME>张三</STUDENT_NAME>
            <STUDENT_SEX></STUDENT_SEX>
        </STUDENT>
        <STUDENT STUDENT_ID="002">
            <STUDENT_NAME>李四</STUDENT_NAME>
            <STUDENT_SEX></STUDENT_SEX>
        </STUDENT>
    </STUDENT_LIST>
</MESSAGE>

 

posted @ 2020-06-29 13:31  yshy  阅读(2270)  评论(0编辑  收藏  举报