我有跟随XStream的问题:当我尝试阅读注释时,我需要使用以下句子:
xstream.processAnnotations(DataClass .class);
明确定义了我要序列化的类。但在我的代码中:
public class Tester {
/**
* @param args
*/
public static void main(String[] args) {
DataClass data = new DataClass();
data.familyName = "Pil";
data.firstName = "Paco";
data.ID = 33;
data.properties.put("one", "1");
data.properties.put("two", "2");
data.properties.put("three", "3");
String xml = getXmlString(data);
System.out.println(xml);
}
public static String getXmlString(Object data) {
String ret = "";
final StringWriter stringWriter = new StringWriter();
XStream xstream = new XStream(new StaxDriver());
xstream.processAnnotations(Object.class);
xstream.marshal(data, new PrettyPrintWriter(stringWriter));
ret = stringWriter.toString();
return ret;
}
}
其中dataClass是:
@XStreamAlias("data")
public class DataClass {
public Integer ID = 0;
public String firstName = "";
public String familyName = "";
public Map<String, String> properties = null;
public DataClass(){
properties = new HashMap<String,String>();
}
}
我想有类似的东西:
public static <T> String getXmlString(T data) {
String ret = "";
final StringWriter stringWriter = new StringWriter();
XStream xstream = new XStream(new StaxDriver());
xstream.processAnnotations(T.class);
xstream.marshal(data, new PrettyPrintWriter(stringWriter));
ret = stringWriter.toString();
return ret;
}
但它不起作用。
有人知道我是否有可能做的事情?
答案 0 :(得分:2)
您可能应启用“Auto-detect Annotations”模式:
public static <T> String getXmlString(T data) {
final StringWriter stringWriter = new StringWriter();
XStream xstream = new XStream(new StaxDriver());
xstream.autodetectAnnotations(true);
xstream.marshal(data, new PrettyPrintWriter(stringWriter));
return stringWriter.toString();
}
请阅读“Auto-detect Annotations”段落。您可以在其中找到与此解决方案相关的问题的所有信息。如:鸡蛋问题,并发,例外和表现。
结果:
<data>
<ID>33</ID>
<firstName>Paco</firstName>
<familyName>Pil</familyName>
<properties>
<entry>
<string>two</string>
<string>2</string>
</entry>
<entry>
<string>one</string>
<string>1</string>
</entry>
<entry>
<string>three</string>
<string>3</string>
</entry>
</properties>
</data>
答案 1 :(得分:1)
我建议您创建一个初始化xstream对象的init()
方法。让你的xstream对象具有类范围。我从来没有理由在项目中创建多个xstream对象,所以我通常以这种方式处理它。你甚至可以把它变成静态场。将所有注释处理方法放在init()
中,并显式注册您希望序列化的每个类。
答案 2 :(得分:1)
您可以使用
if(data != null) xstream.processAnnotations(data.getClass());
访问data
对象的直接类,但正如Thorn所建议的那样,声明一个知道所有将要序列化的类的XStream
实例可能更好。
答案 3 :(得分:0)
我认为我找到了一个解决方案,它可能不是最好的解决方案,但是因为我需要非常灵活,它才是最符合我需求的解决方案:
public static <T> String getXmlString(T data, Class<?> dataClass) {
String ret = "";
final StringWriter stringWriter = new StringWriter();
XStream xstream = new XStream(new StaxDriver());
xstream.processAnnotations(dataClass);
xstream.marshal(data, new PrettyPrintWriter(stringWriter));
ret = stringWriter.toString();
return ret;
}
如果有人找到更好的东西,我会很高兴的。我对Java很新,可能我没有使用这种语言的所有很酷的功能。
答案 4 :(得分:0)
可能与此问题没有直接关系,但如果您使用Spring XStreamMarshaller,则需要打开XStream注释的检测,如下所示:
<bean id="xStreamMarshaller" class="org.springframework.oxm.xstream.XStreamMarshaller">
<property name="autodetectAnnotations" value="true"/>
</bean>