引入依赖
< dependency>
< groupId> org.projectlombok</ groupId>
< artifactId> lombok</ artifactId>
< optional> true</ optional>
</ dependency>
< dependency>
< groupId> com.fasterxml.jackson.dataformat</ groupId>
< artifactId> jackson-dataformat-xml</ artifactId>
</ dependency>
< dependency>
< groupId> org.springframework.boot</ groupId>
< artifactId> spring-boot-starter-web</ artifactId>
</ 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" )
public class Bsxml < T > implements Serializable {
@JacksonXmlElementWrapper ( localName = "MsgHeader" )
@JacksonXmlProperty ( localName = "MsgHeader" )
private MsgHeader msgHeader;
@JacksonXmlElementWrapper ( localName = "MsgBody" )
@JacksonXmlProperty ( localName = "MsgBody" )
private T 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 ;
@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 ;
@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 {
@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) ) ;
}
@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> 123456789</ Organization>
< Sender> Meta39</ Sender>
< ServiceType> service</ ServiceType>
< MsgType> msgType</ MsgType>
< MsgVersion> 1.0</ MsgVersion>
</ MsgHeader>
< MsgBody>
< Code> 1</ Code>
< Status> true</ Status>
< Detail> 描述信息</ Detail>
< Data>
< Name> 名称</ Name>
</ Data>
</ MsgBody>
</ Bsxml>
转换
objectToXml
传参
{
"msgHeader" : {
"organization" : "123456789" ,
"sender" : "Meta39" ,
"serviceType" : "service" ,
"msgType" : "msgType" ,
"msgVersion" : "1.0"
} ,
"msgBody" : {
"status" : true ,
"code" : 1 ,
"errCode" : null ,
"detail" : "描述信息" ,
"data" : {
"name" : "名称"
}
}
}
转换