Steve Foster Steve Foster - 1 year ago 114
Java Question

XStream JavaBeanConverter not serializing properties

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.

public class TestObject
private String foo;

public String getFoo()
//return foo; -- Adjusted for EDIT#2
return foo.toLowerCase();

public void setFoo(String foo)
{ = foo;

public void test() throws Exception
XStream x = new XStream(new XppDriver());

x.registerConverter(new JavaBeanConverter(x.getMapper()));

TestObject o = new TestObject();
//o.setFoo("bar"); -- Adjusted for EDIT#2

String xml = x.toXML(o);



But instead getting...

I tried adding a trace on the
method and it appears it is being called by XStream, but the data isn't being written to the output stream.

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?


Edit 1

Also, if it helps, I'm using the following Maven deps for this...


Edit 2

I altered the
method to better explain my objective.

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.

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).

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.


Answer Source

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);
        if (converter.canConvert(type)) {
            typeToConverterMap.put(type, converter);
            return converter;
    throw new ConversionException("No converter specified for " + type);

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).

One way to fix the problem is to give the JavaBeanConverter a lower priority.

x.registerConverter(new JavaBeanConverter(x.getMapper()), -10);

Then JavaBeanConverter is used for your Bean and SingleValueConverterWrapper is used for the String value.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download