前文展示了使用别名更改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. 测试结果
<com.favccxx.favsoft.pojo.ResponseMessage> <messageType>200</messageType> <messageContent>成功</messageContent> </com.favccxx.favsoft.pojo.ResponseMessage>
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);
<message> <type>200</type> <content>成功</content> </message>
(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 List<String> messageContent;
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<String> getMessageContent() {
return messageContent;
}
public void setMessageContent(List<String> 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 输出结果
<message> <type>28</type> <content class="java.util.Arrays$ArrayList"> <a class="string-array"> <string>终于爬上来了!</string> <string>我忘带钥匙了!!!</string> </a> </content> </message>
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 List<String> messageContent;
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<String> getMessageContent() {
return messageContent;
}
public void setMessageContent(List<String> messageContent) {
this.messageContent = messageContent;
}
}
2.5 输出结果
<message> <type>28</type> <string>终于爬上来了!</string> <string>我忘带钥匙了!!!</string> </message>
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 List<String> messageContent;
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<String> getMessageContent() {
return messageContent;
}
public void setMessageContent(List<String> messageContent) {
this.messageContent = messageContent;
}
}
2.7 输出内容如下
<message> <type>28</type> <dialog>终于爬上来了!</dialog> <dialog>我忘带钥匙了!!!</dialog> </message>
(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 List<String> messageContent;
@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<String> getMessageContent() {
return messageContent;
}
public void setMessageContent(List<String> messageContent) {
this.messageContent = messageContent;
}
}
3.2输出内容
<message type="28" important="yes"> <dialog>终于爬上来了!</dialog> <dialog>我忘带钥匙了!!!</dialog> </message>
(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;
}
}
<message type="200" important="yes">今天星期五</message>
(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 List<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.important = important;
this.messageContent = Arrays.asList(messageContent);
}
public int getMessageType() {
return messageType;
}
public void setMessageType(int messageType) {
this.messageType = messageType;
}
public List<String> getMessageContent() {
return messageContent;
}
public void setMessageContent(List<String> messageContent) {
this.messageContent = messageContent;
}
public boolean isImportant() {
return important;
}
public void setImportant(boolean important) {
this.important = important;
}
}
<message> <content>今天星期五</content> <content>明天去逛街</content> <important>yes</important> </message>
(6)自动检测注解
可以使用下述方法自动检测注解,而不用每次都为每个类设置注解
xstream.autodetectAnnotations(true);
// xstream.processAnnotations(ResponseMessage.class);
总结:使用注解比别名方便易用,但是当一个类被多次重用时,使用别名会更明智些。