xstream在接口报文中的应用

以maven项目为例:

首先在pom.xml中配置依赖


4.0.0

com
xstreamdemo
0.0.1-SNAPSHOT
jar

xstreamdemo
http://maven.apache.org


<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>





com.thoughtworks.xstream
xstream
1.4



org.dom4j
dom4j
2.0.0-RC1



com.cpcn
tools
1.6.2



com.cpcn
cpcn-payment-api
2.1.0



junit
junit
3.8.1
test





ch.qos.logback
logback-core
1.1.3


ch.qos.logback
logback-classic
1.1.3


org.slf4j
slf4j-api
1.7.5



org.slf4j
slf4j-api
1.7.5



net.sf.json-lib
json-lib
2.4
jdk15








nexus_public
http://localhost:8081/nexus/content/groups/public/

true


true





nexus_public
http://localhost:8081/nexus/content/groups/public/

true


true



生成报文类文件的方法

public static void main(String[] args) {
String str=”SerialNumber,ProjectNo,RepaymentType,AccountType,BankID,BankAccountName,BankAccountNumber,”

        + "BankProvince,BankCity,IdentificationType,IdentificationNumber,PaymentAccountName,PaymentAccountNumber,Amount,Remark";


String\[\] strs=str.split(",");

String con="";
for (int i = 0; i < strs.length; i++) {
    con+=" @XStreamAlias(""+strs\[i\]+"")

“;
con+=” private String “+strs[i].substring(0,1).toLowerCase()+strs[i].substring(1)+”;
“;
con+=”
“;

    }
    System.out.println(con);
}

基础类BeanToXml, XmlToBean

package com.xstreamdemo.base;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

public class BaseStream {

public String toXml(){
     XStream xStream = new XStream();  
     xStream.autodetectAnnotations(true);  
     xStream.aliasSystemAttribute(null, "class"); // important remove class attribute
     String str = xStream.toXML(this);

     //********* 去每行缩进,根据不同的报文可选 *********/
     str=str.trim();
    str=str.replaceAll(" {1,}<", "<");
    str=str.replaceAll("

“, “
“);

     //************************************************/
     return str; 
}

public String toXml(boolean withHead){
    if(withHead){
        StringBuffer buf=new StringBuffer();
        buf.append("<?xml version="1.0" encoding="UTF-8" standalone="no"?>");
        buf.append("

“);
buf.append(toXml());
return buf.toString();
}else{
return toXml();
}

}


@SuppressWarnings("unchecked")
public static <T> T fromXml(String str, Class<T> cls){
    XStream stream = new XStream(new DomDriver());
    stream.processAnnotations(cls);
    return (T)stream.fromXML(str  ); 
}

}

巧用继承:

package com.xstreamdemo.base;

import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;

@XStreamAlias(“Request”)
public class BaseRequest extends BaseStream {
@XStreamAlias(“version”)
@XStreamAsAttribute
private String version;

@XStreamAlias("Head")
private RequestHead head; 

public String getVersion() {
    return version;
}
public void setVersion(String version) {
    this.version = version; 
}
public RequestHead getHead() {
    return head;
}
public void setHead(RequestHead head) {
    this.head = head;
} 

}

package com.xstreamdemo.base;

import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;

@XStreamAlias(“Request”)
public class Request extends BaseStream {
@XStreamAlias(“version”)
@XStreamAsAttribute
private String version;

@XStreamAlias("Head")
private Head head;
@XStreamAlias("Body")
private BaseBody body; 

public String getVersion() {
    return version;
}
public void setVersion(String version) {
    this.version = version; 
}
public Head getHead() {
    return head;
}
public void setHead(Head head) {
    this.head = head;
}
public BaseBody getBody() {
    return body;
}
public void setBody(BaseBody body) {
    this.body = body;
}

}

BaseBody.java是一个空类,里面啥也没有,

然后其他的报文体继承这个类就好。

最后组装报文:

Request req = new Request();
req.setVersion(“2.1”);
RequestHead head = new RequestHead();
head.setTxCode(“4271”);
head.setInstitutionID(institutionID);
Body4271 body = new Body4271();
body.setPaymentAccountNumber(this.paymentNo);
body.setAgreementNo( NetUtil.getRandStr(32));
body.setAgreementType(“60”); // 60自动投资;20自动扣款
body.setPageUrl(pageUrl);
req.setHead(head);
req.setBody(body);

String str = req.toXml(true);
System.out.println(str); 


String\[\] ps=NetUtil.getMessageAndSignature(str);
String message=ps\[0\];
String signature=ps\[1\];

但是接收报文时,不能在报文类中用父类:

package com.xstreamdemo.response;

import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.xstreamdemo.base.ResponseHead;
@XStreamAlias(“Response”)
public class Response4232 {
@XStreamAlias(“Head”)
private ResponseHead responseHead;

public ResponseHead getResponseHead() {
    return responseHead;
}

public void setResponseHead(ResponseHead responseHead) {
    this.responseHead = responseHead;
}

@XStreamAlias("Body") 
private ResBody4232 baseBody;

public ResBody4232 getBaseBody() {
    return baseBody;
}

public void setBaseBody(ResBody4232 baseBody) {
    this.baseBody = baseBody;
}

}

Body必须指定明确的类。

0%