SpringBoot之jackson之复杂XML和Object互转、泛型、传参/接参

引入依赖

		
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <optional>trueoptional>
        dependency>
        
        <dependency>
            <groupId>com.fasterxml.jackson.dataformatgroupId>
            <artifactId>jackson-dataformat-xmlartifactId>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

创建第1层请求对象Bsxml

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 lombok.Data;
import lombok.ToString;

import java.io.Serializable;

@Data
@ToString
@JacksonXmlRootElement(localName = "Bsxml") //XML根节点名称
public class Bsxml<T> implements Serializable {
    @JacksonXmlElementWrapper(localName = "MsgHeader")//反序列化XML参数的名称
    @JacksonXmlProperty(localName = "MsgHeader")//序列化XML参数的名称
    private MsgHeader msgHeader;//请求头对象
    @JacksonXmlElementWrapper(localName = "MsgBody")
    @JacksonXmlProperty(localName = "MsgBody")
    private T msgBody;//泛型消息体,我这里的泛型是可以去掉的,直接用MsgBody对象,这里只是为了模拟复杂对象。
}

创建第2层请求头对象MsgHeader

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 lombok.Data;
import lombok.ToString;

import java.io.Serializable;

@Data
@ToString
@JacksonXmlRootElement(localName = "MsgHeader")
public class MsgHeader implements Serializable {
    @JacksonXmlElementWrapper(localName = "Organization")
    @JacksonXmlProperty(localName = "Organization")
    private String organization;
    @JacksonXmlElementWrapper(localName = "Sender")
    @JacksonXmlProperty(localName = "Sender")
    private String sender;
    @JacksonXmlElementWrapper(localName = "ServiceType")
    @JacksonXmlProperty(localName = "ServiceType")
    private String serviceType;
    @JacksonXmlElementWrapper(localName = "MsgType")
    @JacksonXmlProperty(localName = "MsgType")
    private String msgType;
    @JacksonXmlElementWrapper(localName = "MsgVersion")
    @JacksonXmlProperty(localName = "MsgVersion")
    private String msgVersion;
}

创建第2层请求头体MsgData

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 lombok.Data;

/**
 * MsgBody对象就是Bsxml里面的泛型T
 */
@Data
@JacksonXmlRootElement(localName = "MsgBody")
public class MsgBody<D> {
    @JacksonXmlElementWrapper(localName = "Status")
    @JacksonXmlProperty(localName = "Status")
    private Boolean status;//状态
    @JacksonXmlElementWrapper(localName = "Code")
    @JacksonXmlProperty(localName = "Code")
    private Integer code;//成功状态码
    @JacksonXmlElementWrapper(localName = "ErrCode")
    @JacksonXmlProperty(localName = "ErrCode")
    private Integer errCode;//错误状态码
    @JacksonXmlElementWrapper(localName = "Detail")
    @JacksonXmlProperty(localName = "Detail")
    private String detail;//详细信息
    @JacksonXmlElementWrapper(localName = "Data")
    @JacksonXmlProperty(localName = "Data")
    private D data;//返回数据,泛型
}

创建第2层请求头体MsgData内层某个Data泛型对象

import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
import lombok.Data;
import lombok.ToString;

/**
 * LabReportAudit对象就是MsgBody里面的泛型 D
 */
@Data
@ToString
@JacksonXmlRootElement(localName = "Data")
public class LabReportAudit {
    @JacksonXmlElementWrapper(localName = "Name")
    private String name;
}

模拟Controller接参传参

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import lombok.extern.slf4j.Slf4j;
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;

@Slf4j
@RestController
@RequestMapping("test")
public class TestController {

    /**
     * 传xml转json,json转对象
     * @param bsxml Bsxml>
     */
    @PostMapping("xmlToObject")
    public void xmlToObject(@RequestBody Bsxml<MsgBody<LabReportAudit>> bsxml) throws JsonProcessingException {
        log.info("Bsxml转成实体后的信息:{}",bsxml);
        ObjectMapper objectMapper = new ObjectMapper();
        log.info("bsxml JSON:{}", objectMapper.writeValueAsString(bsxml));
    }

    /**
     * 传json转成对象,对象转xml
     * @param bsxml 实体类json
     */
    @PostMapping("objectToXml")
    public void objectToXml(@RequestBody Bsxml<MsgBody<LabReportAudit>> bsxml) throws JsonProcessingException {
        ObjectMapper objectMapper = new XmlMapper();
        String xmlString = objectMapper.writeValueAsString(bsxml);
        log.info("对象转xml:{}", xmlString);
    }

}

Postman传参

xmlToObject

传参

<Bsxml>
    <MsgHeader>
        <Organization>123456789Organization>
        <Sender>Meta39Sender>
        <ServiceType>serviceServiceType>
        <MsgType>msgTypeMsgType>
        <MsgVersion>1.0MsgVersion>
    MsgHeader>
    <MsgBody>
        <Code>1Code>
        <Status>trueStatus>
        <Detail>描述信息Detail>
        <Data>
            <Name>名称Name>
        Data>
    MsgBody>
Bsxml>

SpringBoot之jackson之复杂XML和Object互转、泛型、传参/接参_第1张图片

转换

在这里插入图片描述

objectToXml

传参

{
    "msgHeader": {
        "organization": "123456789",
        "sender": "Meta39",
        "serviceType": "service",
        "msgType": "msgType",
        "msgVersion": "1.0"
    },
    "msgBody": {
        "status": true,
        "code": 1,
        "errCode": null,
        "detail": "描述信息",
        "data": {
            "name": "名称"
        }
    }
}

SpringBoot之jackson之复杂XML和Object互转、泛型、传参/接参_第2张图片

转换

在这里插入图片描述

你可能感兴趣的:(#,Spring,Boot,spring,boot,xml,java)