前文展示了使用别名更改XML展示内容,在这一节,可以使用注解更简单的更改XML展示。
(1)最简单的注解示例
1. 输出响应消息
packagecom.favccxx.favsoft.pojo; publicclassResponseMessage{ privateintmessageType; privateStringmessageContent; publicResponseMessage(intmessageType,StringmessageContent){ this.messageType=messageType; this.messageContent=messageContent; } publicintgetMessageType(){ returnmessageType; } publicvoidsetMessageType(intmessageType){ this.messageType=messageType; } publicStringgetMessageContent(){ returnmessageContent; } publicvoidsetMessageContent(StringmessageContent){ this.messageContent=messageContent; } }
2. 开始测试
packagecom.favccxx.favsoft.main; importcom.favccxx.favsoft.pojo.ResponseMessage; importcom.thoughtworks.xstream.XStream; publicclassMainAnnotations{ publicstaticvoidmain(String[]args){ XStreamstream=newXStream(); ResponseMessagemsg=newResponseMessage(200,"成功"); System.out.println(stream.toXML(msg)); } }
3. 测试结果
<com.favccxx.favsoft.pojo.ResponseMessage> <messageType>200</messageType> <messageContent>成功</messageContent> </com.favccxx.favsoft.pojo.ResponseMessage>
packagecom.favccxx.favsoft.pojo; importcom.thoughtworks.xstream.annotations.XStreamAlias; @XStreamAlias("message") publicclassResponseMessage{ @XStreamAlias("type") privateintmessageType; @XStreamAlias("content") privateStringmessageContent; publicResponseMessage(intmessageType,StringmessageContent){ this.messageType=messageType; this.messageContent=messageContent; } publicintgetMessageType(){ returnmessageType; } publicvoidsetMessageType(intmessageType){ this.messageType=messageType; } publicStringgetMessageContent(){ returnmessageContent; } publicvoidsetMessageContent(StringmessageContent){ this.messageContent=messageContent; } }
xstream.processAnnotations(ResponseMessage.class);
<message> <type>200</type> <content>成功</content> </message>
(2)使用集合注解
2.1 响应消息对象
packagecom.favccxx.favsoft.pojo; importjava.util.Arrays; importjava.util.List; importcom.thoughtworks.xstream.annotations.XStreamAlias; @XStreamAlias("message") publicclassResponseMessage{ @XStreamAlias("type") privateintmessageType; @XStreamAlias("content") privateList<String>messageContent; publicResponseMessage(intmessageType,String...messageContent){ this.messageType=messageType; this.messageContent=Arrays.asList(messageContent); } publicintgetMessageType(){ returnmessageType; } publicvoidsetMessageType(intmessageType){ this.messageType=messageType; } publicList<String>getMessageContent(){ returnmessageContent; } publicvoidsetMessageContent(List<String>messageContent){ this.messageContent=messageContent; } }
2.2 开始测试
packagecom.favccxx.favsoft.main; importcom.favccxx.favsoft.pojo.ResponseMessage; importcom.thoughtworks.xstream.XStream; publicclassMainAnnotations{ publicstaticvoidmain(String[]args){ XStreamxstream=newXStream(); xstream.processAnnotations(ResponseMessage.class); ResponseMessagemsg=newResponseMessage(28,"终于爬上来了!","我忘带钥匙了!!!"); System.out.println(xstream.toXML(msg)); } }
2.3 输出结果
<message> <type>28</type> <contentclass="java.util.Arrays$ArrayList"> <aclass="string-array"> <string>终于爬上来了!</string> <string>我忘带钥匙了!!!</string> </a> </content> </message>
2.4使用@XStreamImplicit注解将集合标记为隐式集合
packagecom.favccxx.favsoft.pojo; importjava.util.Arrays; importjava.util.List; importcom.thoughtworks.xstream.annotations.XStreamAlias; importcom.thoughtworks.xstream.annotations.XStreamImplicit; @XStreamAlias("message") publicclassResponseMessage{ @XStreamAlias("type") privateintmessageType; @XStreamImplicit privateList<String>messageContent; publicResponseMessage(intmessageType,String...messageContent){ this.messageType=messageType; this.messageContent=Arrays.asList(messageContent); } publicintgetMessageType(){ returnmessageType; } publicvoidsetMessageType(intmessageType){ this.messageType=messageType; } publicList<String>getMessageContent(){ returnmessageContent; } publicvoidsetMessageContent(List<String>messageContent){ this.messageContent=messageContent; } }
2.5 输出结果
<message> <type>28</type> <string>终于爬上来了!</string> <string>我忘带钥匙了!!!</string> </message>
2.6 在原有注解对象上,对字段进行别名
packagecom.favccxx.favsoft.pojo; importjava.util.Arrays; importjava.util.List; importcom.thoughtworks.xstream.annotations.XStreamAlias; importcom.thoughtworks.xstream.annotations.XStreamImplicit; @XStreamAlias("message") publicclassResponseMessage{ @XStreamAlias("type") privateintmessageType; @XStreamImplicit(itemFieldName="dialog") privateList<String>messageContent; publicResponseMessage(intmessageType,String...messageContent){ this.messageType=messageType; this.messageContent=Arrays.asList(messageContent); } publicintgetMessageType(){ returnmessageType; } publicvoidsetMessageType(intmessageType){ this.messageType=messageType; } publicList<String>getMessageContent(){ returnmessageContent; } publicvoidsetMessageContent(List<String>messageContent){ this.messageContent=messageContent; } }
<message> <type>28</type> <dialog>终于爬上来了!</dialog> <dialog>我忘带钥匙了!!!</dialog> </message>
(3)属性别名
3.1响应消息
packagecom.favccxx.favsoft.pojo; importjava.util.Arrays; importjava.util.List; importcom.thoughtworks.xstream.annotations.XStreamAlias; importcom.thoughtworks.xstream.annotations.XStreamAsAttribute; importcom.thoughtworks.xstream.annotations.XStreamConverter; importcom.thoughtworks.xstream.annotations.XStreamImplicit; importcom.thoughtworks.xstream.converters.basic.BooleanConverter; @XStreamAlias("message") publicclassResponseMessage{ @XStreamAlias("type") @XStreamAsAttribute privateintmessageType; @XStreamImplicit(itemFieldName="dialog") privateList<String>messageContent; @XStreamAsAttribute @XStreamConverter(value=BooleanConverter.class,booleans={false},strings={"yes","no"}) privatebooleanimportant; publicResponseMessage(intmessageType,booleanimportant,String...messageContent){ this.messageType=messageType; this.messageContent=Arrays.asList(messageContent); this.important=important; } publicintgetMessageType(){ returnmessageType; } publicvoidsetMessageType(intmessageType){ this.messageType=messageType; } publicList<String>getMessageContent(){ returnmessageContent; } publicvoidsetMessageContent(List<String>messageContent){ this.messageContent=messageContent; } }
<messagetype="28"important="yes"> <dialog>终于爬上来了!</dialog> <dialog>我忘带钥匙了!!!</dialog> </message>
(4)所有字段变为文本,值得注意的是,转换为文本的内容,只能是String类型的,因为转换器是StringValueConvertor。
packagecom.favccxx.favsoft.pojo; importcom.thoughtworks.xstream.annotations.XStreamAlias; importcom.thoughtworks.xstream.annotations.XStreamConverter; importcom.thoughtworks.xstream.converters.basic.BooleanConverter; importcom.thoughtworks.xstream.converters.extended.ToAttributedValueConverter; @XStreamAlias("message") @XStreamConverter(value=ToAttributedValueConverter.class,strings={"messageContent"}) publicclassResponseMessage{ @XStreamAlias("type") privateintmessageType; @XStreamAlias("content") privateStringmessageContent; @XStreamConverter(value=BooleanConverter.class,StringmessageContent){ this.messageType=messageType; this.messageContent=messageContent; this.important=important; } publicintgetMessageType(){ returnmessageType; } publicvoidsetMessageType(intmessageType){ this.messageType=messageType; } publicStringgetMessageContent(){ returnmessageContent; } publicvoidsetMessageContent(StringmessageContent){ this.messageContent=messageContent; } publicbooleanisImportant(){ returnimportant; } publicvoidsetImportant(booleanimportant){ this.important=important; } }
<messagetype="200"important="yes">今天星期五</message>
(5)省略字段
packagecom.favccxx.favsoft.pojo; importjava.util.Arrays; importjava.util.List; importcom.thoughtworks.xstream.annotations.XStreamAlias; importcom.thoughtworks.xstream.annotations.XStreamConverter; importcom.thoughtworks.xstream.annotations.XStreamImplicit; importcom.thoughtworks.xstream.annotations.XStreamOmitField; importcom.thoughtworks.xstream.converters.basic.BooleanConverter; @XStreamAlias("message") publicclassResponseMessage{ @XStreamOmitField privateintmessageType; @XStreamImplicit(itemFieldName="content") privateList<String>messageContent; @XStreamConverter(value=BooleanConverter.class,String...messageContent){ this.messageType=messageType; this.important=important; this.messageContent=Arrays.asList(messageContent); } publicintgetMessageType(){ returnmessageType; } publicvoidsetMessageType(intmessageType){ this.messageType=messageType; } publicList<String>getMessageContent(){ returnmessageContent; } publicvoidsetMessageContent(List<String>messageContent){ this.messageContent=messageContent; } publicbooleanisImportant(){ returnimportant; } publicvoidsetImportant(booleanimportant){ this.important=important; } }
<message> <content>今天星期五</content> <content>明天去逛街</content> <important>yes</important> </message>
(6)自动检测注解
xstream.autodetectAnnotations(true); //xstream.processAnnotations(ResponseMessage.class);
总结:使用注解比别名方便易用,但是当一个类被多次重用时,使用别名会更明智些。