java之xml报⽂传值问题的解决~~~xml格式报⽂请求后端
1.这⾥本⼈使⽤postman来请求java后端,在postman中的设置
<TradeData>
<BaseInfo>
<RequestDate>2017-07-06 15:47:30</RequestDate>
<TransNo>00920170627091902001</TransNo>
<UserCode>test</UserCode>
<Password>1234@abcd</Password>
</BaseInfo>
<ContInfo>
<AppointMentNo>HXJK0000000000000002</AppointMentNo>
<ContSource>009</ContSource>
<BizType>TB</BizType>
<DocTypeCode>A101</DocTypeCode>
<DocImgOrder>1</DocImgOrder>
<DocImgCount>10</DocImgCount>
</ContInfo>
</TradeData>
使⽤post请求,选中Body中的row将上诉报⽂放⼊,设置请求⽅式为XML(这⾥注意:因为postman版本的不同,选择的⽅式也不同,有的postman中只有XML格式,⽽有的postman格式为XML(application/xml)和XML(test/xml)格式),我们选⽤
XML(application/xml)格式来请求,具体体现如下图所⽰:
2. 后端java控制层,这⾥我是⽤当下较流⾏、智能的IDEA来展⽰后端代码
先来看实体类的写法。
此处为最外层的封装类
import com.fasterxml.l.annotation.JacksonXmlProperty; import com.fasterxml.l.annotation.JacksonXmlRootElement; import com.sinosoft.mid.
modulemon.quest.BaseInfo; import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@JacksonXmlRootElement(localName ="TradeData")
@NoArgsConstructor
@AllArgsConstructor
public class TradeDataHCSImgUploadNotifyRequest {
@JacksonXmlProperty(localName ="BaseInfo")
BaseInfo baseInfo;
@JacksonXmlProperty(localName ="ContInfo")
ContInfo contInfo;
}
BaseInfo实体类如下:
import com.fasterxml.l.annotation.JacksonXmlProperty; import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class BaseInfo {
@JacksonXmlProperty(localName ="TradeCode")
private String tradeCode;
@JacksonXmlProperty(localName ="TransNo")
private String transNo;
@JacksonXmlProperty(localName ="RequestDate")
private String requestDate;
@JacksonXmlProperty(localName ="ResponseDate")
private String responseDate;
@JacksonXmlProperty(localName ="UserCode")
private String userCode;
@JacksonXmlProperty(localName ="Password")
private String password;
}
ContInfo 实体类:
import com.fasterxml.l.annotation.JacksonXmlProperty;
import com.dulemon.quest.DocImgInfos; import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructorjava xml是什么
public class ContInfo {
@JacksonXmlProperty(localName ="AppointMentNo")
private String appointMentNo;
@JacksonXmlProperty(localName ="ContSource")
private String contSource;
@JacksonXmlProperty(localName ="BizType")
private String bizType;
@JacksonXmlProperty(localName ="DocTypeCode")
private String docTypeCode;
@JacksonXmlProperty(localName ="ReqType")
private String reqType;
@JacksonXmlProperty(localName ="DocImgInfos")
private DocImgInfos docImgInfos;
@JacksonXmlProperty(localName ="DocImgOrder")
private String docImgOrder;
@JacksonXmlProperty(localName ="DocImgCount")
private String docImgCount;
}
这⾥拾荒者有⽐较解释⼀下
3. ContInfo中的实体类DocImgInfos
import com.fasterxml.l.annotation.JacksonXmlElementWrapper; import com.fasterxml.l.annotation.JacksonXmlProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class DocImgInfos {
@JacksonXmlProperty(localName ="DocImgInfos")
private String docImgInfos;
@JacksonXmlProperty(localName ="DocTypeCount")
private String docTypeCount;
@JacksonXmlElementWrapper(useWrapping =false)
@JacksonXmlProperty(localName ="DocImgInfo")
private List<DocImgInfo> docImgInfo;
}
4. 关于List集合中的DocImgInfo
@Data
@NoArgsConstructor
@AllArgsConstructor
public class DocImgInfo {
@JacksonXmlProperty(localName ="DocTypeCode")
private String docTypeCode;
@JacksonXmlProperty(localName ="DocImgPages")
private String docImgPages;
@JacksonXmlElementWrapper(useWrapping =false)
@JacksonXmlProperty(localName ="DocImgPage")
private List<DocImgPage> docImgInfo;
}
关于List中的实体类,拾荒者本⼈就不多解释了 ,想必都知道,就是在某⼀挤报⽂下,有多个该实体类型。只是层级不同。
本⼈有必要解释⼀下***@JacksonXmlProperty(localName = “DocImgPage”)***的意思。
由于***spring容器***的特性,实体类字段都会转⼩驼峰,⽽我们在postman中测试的时候采⽤的有时候是⼤写开头的报⽂。所以此注解是保证与请求⼀致,防⽌请求的时候映射器处理不到。⽽实体类名我们有时候也要处理使⽤***@JacksonXmlRootElement(localName =“TradeData”)***来控制。
5. 控制层中来设计⼀个⽅法来接收这种类型的数据:
@Autowired
private ImgUploadService imgUploadService;
@SneakyThrows
@PostMapping(value ="/upLoadImg",consumes = MediaType.APPLICATION_XML_VALUE, produces = MediaType.APPLICATION_XML_VALUE,heade rs ={"content-type=application/xml"})
@ResponseBody
private TradeDataHCSImgUploadNotifyResponse upLoadImg(@RequestBody TradeDataHCSImgUploadNotifyRequest tradeData){
System.out.println(tradeData);
TradeDataHCSImgUploadNotifyResponse response = imgUploadService.upLoadImg(tradeData);
System.out.println(response);
return response;
}
请求的PostMapping注解就不多解释了 本⼈要解释的是⾥⾯的值 特别是**headers ={“content-type=application/xml”}**要添加,防⽌接收数据失败,
接收到如下结果:
TradeDataHCSImgUploadNotifyRequest(baseInfo=BaseInfo(tradeCode=null, transNo=00920170627091902001, requestDate=2017-07-0615:47:30, respo nseDate=null, userCode=test, password=1234@abcd), contInfo=ContInfo(appointMentNo=HXJK0000000000000002, contSource=009, bizType=TB, docTy peCode=A101, reqType=null, docImgInfos=null, docImgOrder=1, docImgCount=10))
TradeDataHCSImgUploadNotifyRequest(baseInfo=BaseInfo(tradeCode=null, transNo=00920170627091902001, requestDate=2017-07-0615:47:30, respo nseDate=null, userCode=test, password=1234@abcd), contInfo=ContInfo(appointMentNo=HXJK0000000000000002, contSource=009, bizType=TB, reqTy pe=null, docImgCount=10, docImgInfos=DocImgInfos(docImgInfos=null, docTypeCount=null, docImgInfo=[DocImgInfo(docTypeCode=A101, docImgPages =null, docImgInfo=[DocImgPage(docImgOrder=1, docImgAttachNo=null)])])))
希望对同道有所帮助。
此处需要提⽰:此⽂章绝⾮抄袭。
未经本⼈许可,禁⽌转载,

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。