SpringBoot项目使用xml格式的出入参
- 新建一个SpringBoot web项目
- 启用jackson的xml依赖(SpringBoot已经做好了版本适配,故不需要声明版本号)
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</dependency>
- 编写一个Controller
package com.example.mono;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
import lombok.Data;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
public class MonoApplication {
public static void main(String[] args) {
SpringApplication.run(MonoApplication.class, args);
}
}
@RestController
@RequestMapping("/my")
class MyController {
/**
* 1:consumes = MediaType.APPLICATION_XML_VALUE 表示接受xml格式的入参,框架会将xml格式的入参转换为bean。
* 2:produces = MediaType.APPLICATION_XML_VALUE 表示返回xml格式的结果,框架会将返回值bean转换为xml。
*
* @param me
* @return
*/
@PostMapping(value = "/getMe", consumes = MediaType.APPLICATION_XML_VALUE, produces = MediaType.APPLICATION_XML_VALUE)
public Me geMe(@RequestBody Me me) {
return me;
}
}
@Data
@JacksonXmlRootElement(localName = "YyghInterface") //本类对应的xml的根节点名称
class Me {
@JacksonXmlProperty(localName = "yyjsls") //声明字段在xml中的节点名称
private String hospitalSettleSerialNo;
@JacksonXmlProperty(localName = "pay")
private String amount;
@JacksonXmlProperty(localName = "zffs")
private String payChannel;
}
- postman请求localhost:8080/my/getMe(注意post方法,body为raw-xml)
接口输入:
<interface>
<yyjsls>111111</yyjsls>
<pay>11.11</pay>
<zffs>1</zffs>
</interface>
接口返回值:
<interface>
<yyjsls>111111</yyjsls>
<pay>11.11</pay>
<zffs>1</zffs>
</interface>
-
使用原生SpringBoot按上述步骤,完全可以秒支持xml的。由于公司项目框架基于SpringBoot做了改造,故死活以json格式处理出入参,问问技术组如何解决吧。
-
补充一个有嵌套层次的数据对象
package com.dto.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 io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.io.Serializable;
import java.util.List;
@Data
@JacksonXmlRootElement(localName = "YyghInterface")
public class InventoryDetailResponseXmlDTO implements Serializable {
private static final long serialVersionUID = -697190814864891722L;
@ApiModelProperty(value = "判断是否返回成功1表示成功,其余失败")
@JacksonXmlProperty(localName = "RtnValue")
private Integer RtnValue;
@ApiModelProperty(value = "表示返回信息,一般用于失败信息跟踪反馈")
private String bzxx;
@ApiModelProperty(value = "interface节点")
@JacksonXmlProperty(localName = "interface")
private InterFaceDTO interFaceDTO;
@Data
public static class InterFaceDTO implements Serializable {
private static final long serialVersionUID = 8864874279293665726L;
@JacksonXmlProperty(localName = "row")
@JacksonXmlElementWrapper(useWrapping = false)
private List<RowDTO> row;
}
@Data
public static class RowDTO implements Serializable {
private static final long serialVersionUID = 2554639268967759723L;
@ApiModelProperty(value = "就诊序号")
private String jzxh;
@ApiModelProperty(value = "就诊时间 精确到HH:MM")
private String jzsj;
private String jzsj2;
/**
* 号源类别,默认1
* 1.普通号源,默认 4.家庭医生专用号源 5.OA专用号源 6.医养护平台专用号源(06共享号源--杭州健康通) 7.医养护平台专用号
*/
private String yylb;
}
}
- 手动转换示例
package com.dto;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
import lombok.Data;
/**
* @description:
* @author: Yang
* @since: 2022/11/6
*/
@Data
@JacksonXmlRootElement(localName = "output")
public class DownloadFileResultDTO {
@JacksonXmlProperty(localName = "rowcount")
public Integer rowCount;
public static void main(String[] args) throws JsonProcessingException {
// 0.实例化一个转换器
ObjectMapper objectMapper = new XmlMapper();
// 1.将对象序列化为xml
DownloadFileResultDTO xmlObject = new DownloadFileResultDTO();
xmlObject.setRowCount(1000);
System.out.println(objectMapper.writeValueAsString(xmlObject));
// 2.将xml反序列化为对象
String xmlString = "<?xml version=\"1.0\" encoding=\"GBK\" standalone=\"yes\" ?><output><rowcount>1000</rowcount></output>";
DownloadFileResultDTO resultDTO = objectMapper.readValue(xmlString, DownloadFileResultDTO.class);
System.err.println(resultDTO.getRowCount());
}
}
学习使我充实,分享给我快乐!