@XStreamAlias使用
@XStreamAlias使用
一、 特点:
简化的API;
无映射文件;
高性能,低内存占用;
整洁的XML;
不需要修改对象;支持内部私有字段,不需要setter/getter方法
提供序列化接口;
自定义转换类型策略;
二、 使用场景
Transport转换
Persistence 持久化对象
Configuration 配置
Unit Tests 单元测试
Interface服务之间XML消息的交互
三、 代码样例
请求类:
import org.apache.commons.lang.builder.ToStringBuilder; import org.apache.commons.lang.builder.ToStringStyle; import com.thoughtworks.xstream.annotations.XStreamAlias; @XStreamAlias("Response") public class RequestTest { @XStreamAlias("returncode") private String returnCode; @XStreamAlias("returnmessage") private String returnMessage; @XStreamAlias("status") private int status; @XStreamAlias("time") private String reqTime; public String getReturnCode() { return returnCode; } public void setReturnCode(String returnCode) { this.returnCode = returnCode; } public String getReturnMessage() { return returnMessage; } public void setReturnMessage(String returnMessage) { this.returnMessage = returnMessage; } public int getStatus() { return status; } public void setStatus(int status) { this.status = status; } public String getReqTime() { return reqTime; } public void setReqTime(String reqTime) { this.reqTime = reqTime; } @Override public String toString() { return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE); }
}
响应类
import java.sql.Timestamp; import org.apache.commons.lang.builder.ToStringBuilder; import org.apache.commons.lang.builder.ToStringStyle; import com.thoughtworks.xstream.annotations.XStreamAlias; @XStreamAlias("Response") public class ResponseTest { @XStreamAlias("returncode") private int resultCode; @XStreamAlias("time") private Timestamp time; @XStreamAlias("returnmessage") private String resultMessage; @XStreamAlias("status") private int state; @Override public String toString() { return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE); } }
Xstream工具类
import com.thoughtworks.xstream.XStream; import com.thoughtworks.xstream.io.xml.DomDriver; public class XstreamUtil { /** * 将bean转换为xml * @param obj 转换的bean * @return bean转换为xml */ public static String objectToXml(Object obj) { XStream xStream = new XStream(); //xstream使用注解转换 xStream.processAnnotations(obj.getClass()); return xStream.toXML(obj); } /** * 将xml转换为bean * @param <T> 泛型 * @param xml 要转换为bean的xml * @param cls bean对应的Class * @return xml转换为bean */ public static <T> T xmlToObject(String xml, Class<T> cls){ XStream xstream = new XStream(new DomDriver()); //xstream使用注解转换 xstream.processAnnotations(cls); return (T) xstream.fromXML(xml); } }
测试类
public class XStreamTest { public static void main(String[] args) { RequestTest requestTest = new RequestTest(); requestTest.setReturnCode("200"); requestTest.setReturnMessage("ok"); requestTest.setStatus(200); requestTest.setReqTime("2013-09-22 00:00:00"); System.out.println(requestTest); String request = XstreamUtil.objectToXml(requestTest); System.out.println(request); ResponseTest responseTest =XstreamUtil.xmlToObject(request, ResponseTest.class); System.out.println(responseTest); } }
输出:
xstream.RequestTest@1fb8ee3[ returnCode=200 returnMessage=ok status=200 reqTime=2013-09-22 00:00:00 ] <Response> <returncode>200</returncode> <returnmessage>ok</returnmessage> <status>200</status> <time>2013-09-22 00:00:00</time> </Response> xstream.ResponseTest@19dfbff[ resultCode=200 time=2013-09-22 00:00:00.0 resultMessage=ok state=200 ]
四、 总结
1. @XStreamAlias("message") 别名注解 。
2. 使用注解序列化、反序列化,只要别名一样、字段不需要是同一类型(通过JAVA的API可以转换),不需要是同一个类,这为接口之间的交互提供了极大的方便。
3. 简化了API,不需要单独写解析xml的API。
4. toString方法使用ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE)。