XStream to use getter for serialization

java, spring, xml, xstream

Solution

You need to register a custom JavaBeanConverter, take a look at the unit tests here: https://fisheye.codehaus.org/browse/xstream/tags/XSTREAM_1_1_3/xstream/src/test/com/thoughtworks/xstream/converters/javabean/JavaBeanConverterTest.java?r=554

XStream xstream = new XStream();
xstream.registerConverter(new JavaBeanConverter(xstream.getClassMapper(), "class"), -20);

Credit goes to original thread at: http://xstream.10960.n7.nabble.com/How-to-use-public-accessor-instead-of-field-td1193.html

Problem

My web service returns data in both xml and json using Spring MVC. For json, spring uses Jackson and XStream for XML. However, XStream uses fields for serialization while Jackson uses methods (setter/getter). I would like to include all/some getter in xml serialization. How can this be accomplished via a custom converter or annotation?

Original source

Related problems