• 通过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>
  • 相关阅读:
    PHP curl_setopt函数用法介绍补充篇
    Javascript的setTimeOut和setInterval的定时器用法
    PHP curl_setopt函数用法介绍上篇
    开启PHP的伪静态
    关于MySQL的几个命令之load
    使用PHP生成和获取XML格式数据
    WEB开发中常用的正则表达式
    WEB开发中的页面跳转方法总结
    PHP的serialize序列化数据与JSON格式化数据
    PHP防止重复提交表单
  • 原文地址:https://www.cnblogs.com/yshyee/p/13207531.html
Copyright © 2020-2023  润新知