spring boot 中使用xml请求
- 先在pom文件中引入依赖包
<dependency> <groupId>com.fasterxml.jackson.dataformat</groupId> <artifactId>jackson-dataformat-xml</artifactId> <version>2.9.0</version></dependency>
- 编写请求实体
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; } }
- 返回实体
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; }}
- 编写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; } }
- 扩展说明
@JacksonXmlElementWrapper:可用于指定List等集合类,外围标签名;
@JacksonXmlProperty:指定包装标签名,或者指定标签内部属性名;
@JacksonXmlRootElement:指定生成xml根标签的名字;
@JacksonXmlText:指定当前这个值,没有xml标签包裹。实体bean中也可以使用 @XmlRootElement 和@XmlElement这个标签,分别对应于@JacksonXmlRootElement 和@JacksonXmlProperty
- 测试结果 请求
<?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> - 测试结果,返回
<message>
<userreslist>
<userres>
<idcard>11111111111111</idcard>
<username>sky_fxh</username>
<sex/>
<age>0</age>
<salary>5000</salary>
</userres>
</userreslist>
</message>
原文地址:https://www.cnblogs.com/scai-fxh/p/10598535.html
时间: 2024-10-02 22:35:34