12

Using @XStreamOmitField in my POJO seems to have no effect whatsoever. the annotated field still gets exposed in the xml or json representation.

@XStreamAlias("Pojo")
@Entity
public class Pojo {
    private String name;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long key;

    @XStreamOmitField
    private String hidden;

    public Pojo(String name, String hidden) {
        this.name = name;
        this.hidden = hidden;
    }
}

and in the ServerResource

@Get
public Pojo test() {
    Pojo pj= new Pojo("hansi","hinter");
    return pj;
}

gets me

<com.myComp.ORMTest.Pojo>
  <name>hansi</name>
  <hidden>hinter</hidden>
</com.myComp.ORMTest.Pojo>

Any ideas why the annotations are ignored?

David Underhill
  • 15,896
  • 7
  • 53
  • 61
chaos0815
  • 811
  • 16
  • 27
  • Ok, this seems to be not the only problem in getting GAE, RESTlet and XStream working together. I'm looking for another solution now. – chaos0815 Feb 22 '10 at 12:04
  • Hi, did the solution worked for you? How did you apply that? I'm having the same issues: https://stackoverflow.com/questions/66794041/using-xstream-extension-with-swagger-instead-of-jackson – Fireburn Mar 25 '21 at 06:16
  • 1
    @fireburn , Sorry, I have no recollection of how I resolved the problem. This was 11 years ago. – chaos0815 Mar 26 '21 at 07:07

2 Answers2

15

You have to tell XStream to explicitly process annotations:

XStream xstream = new XStream();
xstream.processAnnotations(MyClass.class);

Or, you should add this code to tell XStream to process all annotations:

xstream.autodetectAnnotations(true);
Community
  • 1
  • 1
superdave
  • 1,928
  • 1
  • 17
  • 35
  • 2
    Just a note for other seekers: auto detection of annotations only works for serialization, *not* deserialization. You have to process each class (or an array of them) in order to parse XML. – reve_etrange Oct 23 '13 at 00:51
2

Two things come to mind:

1.) Did you tell XStream to parse the annotations?

2.) Does your web framework maybe use proxies to access the pojos and those don't delegate the annotations? (happened to a friend with Apache Tapestry)

Christopher Oezbek
  • 23,994
  • 6
  • 61
  • 85