本文共 10271 字,大约阅读时间需要 34 分钟。
前文展示了使用别名更改XML展示内容,在这一节,可以使用注解更简单的更改XML展示。
(1)最简单的注解示例
1. 输出响应消息
package com.favccxx.favsoft.pojo;public class ResponseMessage { private int messageType; private String messageContent; public ResponseMessage(int messageType, String messageContent){ this.messageType = messageType; this.messageContent = messageContent; } public int getMessageType() { return messageType; } public void setMessageType(int messageType) { this.messageType = messageType; } public String getMessageContent() { return messageContent; } public void setMessageContent(String messageContent) { this.messageContent = messageContent; }}
2. 开始测试
package com.favccxx.favsoft.main;import com.favccxx.favsoft.pojo.ResponseMessage;import com.thoughtworks.xstream.XStream;public class MainAnnotations { public static void main(String[] args) { XStream stream = new XStream(); ResponseMessage msg = new ResponseMessage(200, "成功"); System.out.println(stream.toXML(msg)); }}
3. 测试结果
200 成功
4. 使用注解后的输出内容
package com.favccxx.favsoft.pojo;import com.thoughtworks.xstream.annotations.XStreamAlias;@XStreamAlias("message")public class ResponseMessage { @XStreamAlias("type") private int messageType; @XStreamAlias("content") private String messageContent; public ResponseMessage(int messageType, String messageContent){ this.messageType = messageType; this.messageContent = messageContent; } public int getMessageType() { return messageType; } public void setMessageType(int messageType) { this.messageType = messageType; } public String getMessageContent() { return messageContent; } public void setMessageContent(String messageContent) { this.messageContent = messageContent; }}
xstream.processAnnotations(ResponseMessage.class);
200 成功
(2)使用集合注解
2.1 响应消息对象
package com.favccxx.favsoft.pojo;import java.util.Arrays;import java.util.List;import com.thoughtworks.xstream.annotations.XStreamAlias;@XStreamAlias("message")public class ResponseMessage { @XStreamAlias("type") private int messageType; @XStreamAlias("content") private ListmessageContent; public ResponseMessage(int messageType, String ... messageContent){ this.messageType = messageType; this.messageContent = Arrays.asList(messageContent); } public int getMessageType() { return messageType; } public void setMessageType(int messageType) { this.messageType = messageType; } public List getMessageContent() { return messageContent; } public void setMessageContent(List messageContent) { this.messageContent = messageContent; }}
2.2 开始测试
package com.favccxx.favsoft.main;import com.favccxx.favsoft.pojo.ResponseMessage;import com.thoughtworks.xstream.XStream;public class MainAnnotations { public static void main(String[] args) { XStream xstream = new XStream(); xstream.processAnnotations(ResponseMessage.class); ResponseMessage msg = new ResponseMessage(28, "终于爬上来了!","我忘带钥匙了!!!"); System.out.println(xstream.toXML(msg)); }}
2.3 输出结果
28 终于爬上来了! 我忘带钥匙了!!!
2.4使用@XStreamImplicit注解将集合标记为隐式集合
package com.favccxx.favsoft.pojo;import java.util.Arrays;import java.util.List;import com.thoughtworks.xstream.annotations.XStreamAlias;import com.thoughtworks.xstream.annotations.XStreamImplicit;@XStreamAlias("message")public class ResponseMessage { @XStreamAlias("type") private int messageType; @XStreamImplicit private ListmessageContent; public ResponseMessage(int messageType, String... messageContent) { this.messageType = messageType; this.messageContent = Arrays.asList(messageContent); } public int getMessageType() { return messageType; } public void setMessageType(int messageType) { this.messageType = messageType; } public List getMessageContent() { return messageContent; } public void setMessageContent(List messageContent) { this.messageContent = messageContent; }}
2.5 输出结果
28 终于爬上来了! 我忘带钥匙了!!!
2.6 在原有注解对象上,对字段进行别名
package com.favccxx.favsoft.pojo;import java.util.Arrays;import java.util.List;import com.thoughtworks.xstream.annotations.XStreamAlias;import com.thoughtworks.xstream.annotations.XStreamImplicit;@XStreamAlias("message")public class ResponseMessage { @XStreamAlias("type") private int messageType; @XStreamImplicit(itemFieldName="dialog") private ListmessageContent; public ResponseMessage(int messageType, String... messageContent) { this.messageType = messageType; this.messageContent = Arrays.asList(messageContent); } public int getMessageType() { return messageType; } public void setMessageType(int messageType) { this.messageType = messageType; } public List getMessageContent() { return messageContent; } public void setMessageContent(List messageContent) { this.messageContent = messageContent; }}
2.7 输出内容如下
28
(3)属性别名
3.1响应消息
package com.favccxx.favsoft.pojo;import java.util.Arrays;import java.util.List;import com.thoughtworks.xstream.annotations.XStreamAlias;import com.thoughtworks.xstream.annotations.XStreamAsAttribute;import com.thoughtworks.xstream.annotations.XStreamConverter;import com.thoughtworks.xstream.annotations.XStreamImplicit;import com.thoughtworks.xstream.converters.basic.BooleanConverter;@XStreamAlias("message")public class ResponseMessage { @XStreamAlias("type") @XStreamAsAttribute private int messageType; @XStreamImplicit(itemFieldName = "dialog") private ListmessageContent; @XStreamAsAttribute @XStreamConverter(value = BooleanConverter.class, booleans = { false }, strings = { "yes", "no" }) private boolean important; public ResponseMessage(int messageType, boolean important, String... messageContent) { this.messageType = messageType; this.messageContent = Arrays.asList(messageContent); this.important = important; } public int getMessageType() { return messageType; } public void setMessageType(int messageType) { this.messageType = messageType; } public List getMessageContent() { return messageContent; } public void setMessageContent(List messageContent) { this.messageContent = messageContent; }}
3.2输出内容
(4)所有字段变为文本,值得注意的是,转换为文本的内容,只能是String类型的,因为转换器是StringValueConvertor。
package com.favccxx.favsoft.pojo;import com.thoughtworks.xstream.annotations.XStreamAlias;import com.thoughtworks.xstream.annotations.XStreamConverter;import com.thoughtworks.xstream.converters.basic.BooleanConverter;import com.thoughtworks.xstream.converters.extended.ToAttributedValueConverter;@XStreamAlias("message")@XStreamConverter(value = ToAttributedValueConverter.class, strings = { "messageContent" })public class ResponseMessage { @XStreamAlias("type") private int messageType; @XStreamAlias("content") private String messageContent; @XStreamConverter(value = BooleanConverter.class, booleans = { false }, strings = { "yes", "no" }) private boolean important; public ResponseMessage(int messageType, boolean important, String messageContent) { this.messageType = messageType; this.messageContent = messageContent; this.important = important; } public int getMessageType() { return messageType; } public void setMessageType(int messageType) { this.messageType = messageType; } public String getMessageContent() { return messageContent; } public void setMessageContent(String messageContent) { this.messageContent = messageContent; } public boolean isImportant() { return important; } public void setImportant(boolean important) { this.important = important; }}
今天星期五
(5)省略字段
package com.favccxx.favsoft.pojo;import java.util.Arrays;import java.util.List;import com.thoughtworks.xstream.annotations.XStreamAlias;import com.thoughtworks.xstream.annotations.XStreamConverter;import com.thoughtworks.xstream.annotations.XStreamImplicit;import com.thoughtworks.xstream.annotations.XStreamOmitField;import com.thoughtworks.xstream.converters.basic.BooleanConverter;@XStreamAlias("message")public class ResponseMessage { @XStreamOmitField private int messageType; @XStreamImplicit(itemFieldName="content") private ListmessageContent; @XStreamConverter(value = BooleanConverter.class, booleans = { false }, strings = { "yes", "no" }) private boolean important; public ResponseMessage(int messageType, boolean important, String... messageContent) { this.messageType = messageType; this.important = important; this.messageContent = Arrays.asList(messageContent); } public int getMessageType() { return messageType; } public void setMessageType(int messageType) { this.messageType = messageType; } public List getMessageContent() { return messageContent; } public void setMessageContent(List messageContent) { this.messageContent = messageContent; } public boolean isImportant() { return important; } public void setImportant(boolean important) { this.important = important; }}
今天星期五 明天去逛街 yes
(6)自动检测注解
可以使用下述方法自动检测注解,而不用每次都为每个类设置注解
xstream.autodetectAnnotations(true);// xstream.processAnnotations(ResponseMessage.class);
总结:使用注解比别名方便易用,但是当一个类被多次重用时,使用别名会更明智些。
转载地址:http://zanoa.baihongyu.com/