Take the 2-minute tour ×

How do I tell Xstream to serialize only fields which are annotated explicitly and ignore the rest?

I am trying to serialize a hibernate persistent object and all proxy related fields get serialized which I don’t want in my xml.

<createdBy class="com..domain.Users " reference="../../values/createdBy"/>

is not something I want in my xml.

Edit: I don’t think I made this question clear. A class may inherit from a base class on which I have no control (as in hibernate’s case) on the base class properties.

public class A {
    private String ShouldNotBeSerialized;

public class B extends A {
    private String ThisShouldbeSerialized;

In this case when I serialize class B, the base class field ShouldNotBeSerialized will also get serialized. This is not something I want. In most circumstances I will not have control on class A.

Therefore I want to omit all fields by default and serialize only fields for which I explicitly specify the annotation. I want to avoid what GaryF is doing, where I need to explicitly specify the fields I need to omit.

share|improve this question
Have you achieved this? If Yes, can you please post the answer. I am in same trouble –  Tech Jerk Feb 6 '10 at 12:07

4 Answers 4

up vote 3 down vote accepted

You can omit fields with the @XstreamOmitField annotation. Straight from the manual:

class RendezvousMessage {

    private int messageType;

    private List<String> content;

    private Calendar created = new GregorianCalendar();

    public RendezvousMessage(int messageType, String... content) {
        this.messageType = messageType;
        this.content = Arrays.asList(content);
share|improve this answer
Don't forget to call autodetectAnnotations(true) on your XStream instance if you are using annotations. –  Sly Aug 3 '11 at 16:24
I don't think this answers the OP's question. He wants to "implicitly" omit without having to explicitly annotate each and every field. See my answer below for another option. –  Twice Circled Aug 3 '13 at 20:08

I can take no credit for this answer, just sharing what I have found. You can override the wrapMapper method of the XStream class to achieve what you need.

This link explains in detail: http://pvoss.wordpress.com/2009/01/08/xstream/

Here is the code you need if you don't want the explanation:

    // Setup XStream object so that it ignores any undefined tags
    XStream xstream = new XStream() {
            protected MapperWrapper wrapMapper(MapperWrapper next) {
                return new MapperWrapper(next) {
                    public boolean shouldSerializeMember(Class definedIn,
                            String fieldName) {
                        if (definedIn == Object.class) {
                            return false;
                        return super
                                .shouldSerializeMember(definedIn, fieldName);

You might want to do all your testing before you implement this code because the exceptions thrown by the default XStream object are useful for finding spelling mistakes.

share|improve this answer

I guess the only direct way is to dive into writing a MapperWrapper and exclude all fields you have not annotated. Sounds like a feature request for XStream.

share|improve this answer

There was already a ticket for the XStream people:

Again, this is by design. XStream is a serialization tool, not a data binding tool. It is made to serialize Java objects to XML and back. It will write anything into XML that is necessary to recreate an equal object graph. The generated XML can be tweaked to some extend by configuration for convenience, but this is already an add-on. What you like to do can be done by implementing a custom mapper, but that's a question for the user's list and cannot be handled here.


share|improve this answer

Your Answer


