Pergunta

Let's say I have a Player class: (ignore access modifiers)

@javax.xml.bind.annotation.XmlRootElement
class Player {
    Long id;
    String name;
    String secret;
}

I also might have castles around the kingdom

@javax.xml.bind.annotation.XmlRootElement
class Kingdom {
    Long id;
    String name;
    Player owner;
}

Now, when I spit the kingdom object to json for consumption by the UI, the player object goes with it, too. This is good, it wouldn't be as useful if you couldn't tell if it was friendly or enemy. But what's bad is that the secret variable is also released. Is it possible to annotate my secret variable to prevent it from being marshaled?

Foi útil?

Solução

You can annotate your field or getter with

@JsonIgnore

The javadoc states

Marker annotation that indicates that the annotated method or field is to be ignored by introspection-based serialization and deserialization functionality. That is, it should not be consider a "getter", "setter" or "creator".

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top