1.maven引入
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.4.18</version>
</dependency>
2.对象与XML互相转化工具
public class XmlUtils {
/**
* 对象转换为xml字符串
*
* @param obj
* @return
*/
public static String toXml(Object obj) throws Exception{
XStream xstream = new XStream(new DomDriver("GBK"));
//自动识别注解
xstream.autodetectAnnotations(true);
//转换出来的xml文件都会加上class="实际类型"属性,如:<Body class="xxx">,class并不是我们想要的。去除class属性。
xstream.aliasSystemAttribute(null, "class");
String xmlStr="";
try{
xmlStr=xstream.toXML(obj);
}catch(Exception e){
throw new Exception("对象转化为xml异常");
}
//空格与换行符替换
Pattern p = Pattern.compile("\\s{2,}|\t|\r|\n");
Matcher m = p.matcher(xmlStr);
//增加xml报文前缀
String finalresult = Constants.XML_PREX+ m.replaceAll("");
return finalresult;
}
/**
* xml转换为对象
*
* @param xml
* @param type
* @return
*/
public static <T> T toObject(String xml, Class<T> type)throws Exception{
XStream xstream = new XStream();
T result = null;
try{
//转化为固定类型
xstream.processAnnotations(type);
//忽略未知元素
xstream.ignoreUnknownElements();
//转化为对象
Object obj = xstream.fromXML(xml);
result=type.cast(obj);
}catch(Exception e){
throw new Exception("xml转化为对象异常");
}
return result;
}
}
public class Constants {
/*
* xml文件前缀
*/
public final static String XML_PREX="<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
}
3.http请求的xml报文转化
/**
* 请求头
* @Date 2022/6/20 下午2:42
* @Version 1.0
*/
@Data
@XStreamAlias("commHead")
public class CommHead {
private String tranCode;
private String tranDate= DateUtil.formatDate(new Date(),DateUtil.FORMATTEXT04);
private String tranTime= DateUtil.formatDate(new Date(),DateUtil.FORMATTEXT19);
public CommHead(String tranCode) {
this.tranCode=tranCode;
}
}
/**
* @Date 2022/6/20 下午2:48
* @Version 1.0
*/
@Data
@XStreamAlias("Message")
public class Message<T> {
/**
* 请求头
*/
@XStreamAlias("commHead")
private CommHead commHead;
private T Body;
public Message(CommHead commHead, T Body) {
this.commHead=commHead;
this.Body = Body;
}
}
/**
* 请求报文头
* @Date 2022/6/20 下午2:55
* @Version 1.0
*/
@Data
@XStreamAlias("BEDC")
public class BEDC<T> {
/**
* 请求报文消息体
*/
private Message<T> Message;
}
/**
* 查询接口
* @param amountReq
* @return
*/
@Override
public AmountVo queryAmount(AmountReq amountReq) {
AmountVo amountVo = null;
try {
checkAmountBalanceRpcReq(amountReq);
String xmlReq=convertAmountRequest(amountReq);
log.info("接口入参【{}】", xmlReq);
String xmlResp= query.query(amountReq.getCustId(),xmlReq);
log.info("接口返回【{}】", xmlResp);
amountVo=buildAmountResultRpc(xmlResp,amountReq);
} catch (Exception e) {
log.error("查询异常:{}", e);
throw new Exception(ResponseCodeEnum.CALL_RPC_EXCEPTION.getCode(), ResponseCodeEnum.CALL_RPC_EXCEPTION.getMsg());
}
return amountVo;
}
/**
* 构建请求参数
* @param amountBalanceRpcReq
* @return
*/
private String convertAmountRequest(AmountBalanceRpcReq amountBalanceRpcReq) throws Exception {
//人力资源账簿开户接口
//报文头
CommHead commHead=new CommHead("SSSS");
//报文内容
A a = new A();
a.setAccNo(amountBalanceRpcReq.getOrderNo());
//报文消息
Message<A> message=new Message(commHead,a);
//报文封装
BEDC<A> bedc=new BEDC<A>();
bedc.setMessage(message);
String xmlReq=XmlUtils.toXml(bedc);
return xmlReq;
}
public AmountVo buildAmountResultRpc(String response, AmountBalanceRpcReq amountBalanceRpcReq) throws Exception {
//返解析泛型,指定具体class
response = response.replace("<Body>","<Body class=\"com.jd.AResp\">" );
AResp aResp=new AResp();
if (CheckUtil.isNull(response)) {
throw new YsHrPaymentException(ResponseCodeEnum.CALL_RPC_RESPONSE_NONE, "查询接口返回为空");
}
//报文头
CommHead commHead=new CommHead("SSSS");
//报文内容对象组装
Message<AResp> message=new Message(commHead,new AResp());
//报文封装
BEDC<AResp> bedc=new BEDC<AResp>();
bedc.setMessage(message);
//xml报文转化为对象
BEDC<AResp> bedcResponse=XmlUtils.toObject(response, bedc.getClass());
aResp=(AResp)bedcResponse.getMessage().getBody();
AmountVo amountVo = new AmountVo();
return amountVo;
}
4.Xstream相关的官方文档地址
XStream - Alias Tutorial