문제

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?

도움이 되었습니까?

해결책

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top