XStream JavaBeanConverter不序列化属性 [英] XStream JavaBeanConverter not serializing properties
问题描述
尝试使用XStream的JavaBeanConverter并遇到问题.我很可能错过了一些简单的东西,或者对XStream的转换器处理得不够好.
Attempting to use XStream's JavaBeanConverter and running into an issue. Most likely I'm missng something simple, or not understanding XStream's converter handling well enough.
@XStreamAlias("test")
public class TestObject
{
private String foo;
public String getFoo()
{
//return foo; -- Adjusted for EDIT#2
return foo.toLowerCase();
}
public void setFoo(String foo)
{
this.foo = foo;
}
}
public void test() throws Exception
{
XStream x = new XStream(new XppDriver());
x.autodetectAnnotations(true);
x.processAnnotations(TestObject.class);
x.registerConverter(new JavaBeanConverter(x.getMapper()));
TestObject o = new TestObject();
//o.setFoo("bar"); -- Adjusted for EDIT#2
o.setFoo("BAR");
String xml = x.toXML(o);
System.out.println(xml);
/*
Expecting...
<test>
<foo>bar</foo>
</test>
But instead getting...
<test>
<foo/>
</test>
*/
}
我尝试在TestObject.getFoo()
方法上添加跟踪,它似乎已被XStream调用,但数据并未写入输出流.
I tried adding a trace on the TestObject.getFoo()
method and it appears it is being called by XStream, but the data isn't being written to the output stream.
查看JavaBeanConverter的源代码后,看来我的实现应该可以工作,这使我相信我在XStream设置过程中未正确配置某些东西.
After looking at the source for JavaBeanConverter, it looks like my implementation should work, which leads me to believe I haven't configured something correctly during the XStream setup.
我只是缺少一些简单的东西吗?
Am I just missing something simple?
谢谢!
编辑1
此外,如果有帮助,我正在为此使用以下Maven部门...
Also, if it helps, I'm using the following Maven deps for this...
<dependency>
<groupId>org.apache.servicemix.bundles</groupId>
<artifactId>org.apache.servicemix.bundles.xstream</artifactId>
<version>1.3_3</version>
</dependency>
<dependency>
<groupId>org.apache.servicemix.bundles</groupId>
<artifactId>org.apache.servicemix.bundles.xpp3</artifactId>
<version>1.1.4c_3</version>
</dependency>
编辑2
我更改了TestObject.getFoo()
方法以更好地说明我的目标.
I altered the TestObject.getFoo()
method to better explain my objective.
我正在尝试使用getters/setter方法对内部对象的数据进行清理". 具体来说,我正在尝试清理传入的反序列化数据,同时试图避免实现Serializable的readResolve()方法.
I'm trying to use the getters/setters to do "sanitation" of the internal object's data. Specifically, I'm trying to clean up incoming deserialized data, while trying to avoid having to implement Serializable's readResolve() method.
如果我实现了readResolve的方法,则必须同时在setter方法(对于其他未反序列化的传入数据集)和readResolve(对于我反序列化的XML数据)中进行清理.
If I implemented readResolve's method I would have to do the cleanup in both the setter method (for any non-deserialized incoming data set elsewhere) and also in the readResolve (for XML data I deserialized).
这就是为什么我使用JavaBeanConverter而不是XStream基于常规字段的实践的原因...迫使数据通过getter/setter方法.
That's the reason why I'm using JavaBeanConverter instead of XStream's normal field based practices... to force data through the getters/setters.
希望这可以解释得更好一些.
Hopefully this explains a little better.
谢谢.
推荐答案
逐步浏览XStream的源代码,您可以在com.thoughtworks.xstream.core.DefaultConverterLookup中找到它.
Stepping through XStream's source you can find this in com.thoughtworks.xstream.core.DefaultConverterLookup.
public Converter lookupConverterForType(Class type) {
Converter cachedConverter = (Converter) typeToConverterMap.get(type);
if (cachedConverter != null) return cachedConverter;
Iterator iterator = converters.iterator();
while (iterator.hasNext()) {
Converter converter = (Converter) iterator.next();
if (converter.canConvert(type)) {
typeToConverterMap.put(type, converter);
return converter;
}
}
throw new ConversionException("No converter specified for " + type);
}
您会注意到String返回JavaBeanConverter,它这样做的原因是JavaBeanConverter将用于具有默认公共构造函数(String具有)的任何对象.
You will notice for the String it returns JavaBeanConverter and the reason it does that is that JavaBeanConverter will be used for any object with a default public constructor (which String has).
解决此问题的一种方法是赋予JavaBeanConverter较低的优先级.
One way to fix the problem is to give the JavaBeanConverter a lower priority.
x.registerConverter(new JavaBeanConverter(x.getMapper()), -10);
然后将JavaBeanConverter用于您的Bean,并将SingleValueConverterWrapper用于String值.
Then JavaBeanConverter is used for your Bean and SingleValueConverterWrapper is used for the String value.
这篇关于XStream JavaBeanConverter不序列化属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!