spring boot中使用xml请求

Posted scai-fxh

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring boot中使用xml请求相关的知识,希望对你有一定的参考价值。

spring boot 中使用xml请求

  1. 先在pom文件中引入依赖包
    <dependency>
    <groupId>com.fasterxml.jackson.dataformat</groupId>
    <artifactId>jackson-dataformat-xml</artifactId>
    <version>2.9.0</version>
    </dependency>
  2. 编写请求实体
    package com.scai.perfirmance.request;


    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 com.scai.perfirmance.bean.User;

    import java.util.List;

    @JacksonXmlRootElement(localName = "message")
    public class UserRequest {


    private List<User> userList;

    @JacksonXmlElementWrapper(localName = "userlist")
    @JacksonXmlProperty(localName = "user")
    public List<User> getUserList() {
    return userList;
    }

    public void setUserList(List<User> userList) {
    this.userList = userList;
    }
    }
    package com.scai.perfirmance.bean;

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


    public class User {
    @JacksonXmlProperty(localName = "idcard")
    private String idCard;
    @JacksonXmlProperty(localName = "username")
    private String name;
    @JacksonXmlProperty(localName = "sex")
    private String sex;
    @JacksonXmlProperty(localName = "age")
    private int age;



    public String getIdCard() {
    return idCard;
    }

    public void setIdCard(String idCard) {
    this.idCard = idCard;
    }


    public String getName() {
    return name;
    }

    public void setName(String name) {
    this.name = name;
    }


    public String getSex() {
    return sex;
    }

    public void setSex(String sex) {
    this.sex = sex;
    }


    public int getAge() {
    return age;
    }

    public void setAge(int age) {
    this.age = age;
    }


    }
  3. 返回实体
    package com.scai.perfirmance.response;

    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 UserResponse {

    private List<UserRes> userResList;

    @JacksonXmlElementWrapper(localName = "userreslist")
    // @JacksonXmlElementWrapper(useWrapping = false) // 禁用该属性参与xml转换
    @JacksonXmlProperty(localName = "userres")
    public List<UserRes> getUserResList() {
    return userResList;
    }

    public void setUserResList(List<UserRes> userResList) {
    this.userResList = userResList;
    }



    }
    package com.scai.perfirmance.response;

    import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
    import com.scai.perfirmance.bean.User;

    public class UserRes extends User {

    private String salary;

    @JacksonXmlProperty(localName = "salary")
    public String getSalary() {
    return salary;
    }

    public void setSalary(String salary) {
    this.salary = salary;
    }
    }
  4. 编写Contoller 
    package com.scai.perfirmance.controller;


    import com.scai.perfirmance.request.UserRequest;
    import com.scai.perfirmance.response.UserRes;
    import com.scai.perfirmance.response.UserResponse;
    import io.swagger.annotations.Api;
    import io.swagger.annotations.ApiOperation;
    import org.springframework.http.MediaType;
    import org.springframework.web.bind.annotation.*;

    import java.util.ArrayList;
    import java.util.List;

    @Api(description="xml接口测试")
    @RestController
    @RequestMapping("/xmlTest")

    public class UserXmlController {

    @ApiOperation(value = "xml接口测试",notes = "xml接口测试")
    @PostMapping(value = "/user",consumes = MediaType.APPLICATION_XML_VALUE,produces = MediaType.APPLICATION_XML_VALUE)

    public UserResponse testXml(@RequestBody UserRequest userRequest){

    UserResponse res =new UserResponse();
    List<UserRes> list = new ArrayList<UserRes>();

    UserRes userRes = new UserRes();
    userRes.setIdCard("11111111111111");
    userRes.setName("sky_fxh");
    // userRes.setAge(30);
    userRes.setSalary("5000");
    list.add(userRes);
    res.setUserResList(list);
    return res;
    }

    }
  5. 扩展说明

    @JacksonXmlElementWrapper:可用于指定List等集合类,外围标签名;
    @JacksonXmlProperty:指定包装标签名,或者指定标签内部属性名;
    @JacksonXmlRootElement:指定生成xml根标签的名字;
    @JacksonXmlText:指定当前这个值,没有xml标签包裹。

    实体bean中也可以使用  @XmlRootElement 和@XmlElement这个标签,分别对应于@JacksonXmlRootElement 和@JacksonXmlProperty

  6. 测试结果 请求

    <?xml version="1.0" encoding="UTF-8"?>
    <message>
    <userlist>
    <user>
    <age>0</age>
    <idcard>111111111111111</idcard>
    <username>sky_fxh</username>
    <sex>man</sex>
    </user>
    </userlist>
    </message>

  7. 测试结果,返回

    <message>
    <userreslist>
    <userres>
    <idcard>11111111111111</idcard>
    <username>sky_fxh</username>
    <sex/>
    <age>0</age>
    <salary>5000</salary>
    </userres>
    </userreslist>
    </message>


 

以上是关于spring boot中使用xml请求的主要内容,如果未能解决你的问题,请参考以下文章

Spring boot:thymeleaf 没有正确渲染片段

Spring Boot 2.x基础教程:如何扩展XML格式的请求和响应

如何在 Spring Boot 2 中使用内容协商建立自定义 xml 序列化

解决spring-boot启动中碰到的问题:Cannot determine embedded database driver class for database type NONE(转)(代码片段

Spring Boot + Spring Security Restful 登录

spring boot:创建一个简单的web(maven web project)