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)格式来请求,具体体现如下图所示:
- 后端java控制层,这里我是用当下较流行、智能的IDEA来展示后端代码
先来看实体类的写法。
此处为最外层的封装类
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
import com.sinosoft.mid.module.common.pojo.hcsimguploadnotify.request.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.jackson.dataformat.xml.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.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.sinosoft.mid.module.common.pojo.hcsimguploadnotify.request.DocImgInfos;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
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;
}
这里拾荒者有比较解释一下
- ContInfo中的实体类DocImgInfos
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.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;
}
- 关于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”)***来控制。
- 控制层中来设计一个方法来接收这种类型的数据:
@Autowired
private ImgUploadService imgUploadService;
@SneakyThrows
@PostMapping(value = "/upLoadImg",consumes = MediaType.APPLICATION_XML_VALUE, produces = MediaType.APPLICATION_XML_VALUE,headers ={"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-06 15:47:30, responseDate=null, userCode=test, password=1234@abcd), contInfo=ContInfo(appointMentNo=HXJK0000000000000002, contSource=009, bizType=TB, docTypeCode=A101, reqType=null, docImgInfos=null, docImgOrder=1, docImgCount=10))
TradeDataHCSImgUploadNotifyRequest(baseInfo=BaseInfo(tradeCode=null, transNo=00920170627091902001, requestDate=2017-07-06 15:47:30, responseDate=null, userCode=test, password=1234@abcd), contInfo=ContInfo(appointMentNo=HXJK0000000000000002, contSource=009, bizType=TB, reqType=null, docImgCount=10, docImgInfos=DocImgInfos(docImgInfos=null, docTypeCount=null, docImgInfo=[DocImgInfo(docTypeCode=A101, docImgPages=null, docImgInfo=[DocImgPage(docImgOrder=1, docImgAttachNo=null)])])))
希望对同道有所帮助。
此处需要提示:此文章绝非抄袭。
未经本人许可,禁止转载,