user3378876
user3378876

Reputation: 1924

Hide JSON fields

I have the hibernate entity, that pass to controller as parameter and I want to hide userID field. I use it for database operations and don`t need to show it as input parameter

 @Entity
    @Table(name = "user_sessions")
    public class UserSession{

    @Column(name="uid")
    private Long userID;
    @Id
    @Column(name="access_key")
    private String accessKey;
    @Column(name="secret_key")
    private String secretKey;

    public Long getUserID() {
    return userID;
    }

    public void setUserID(Long s) {
        this.userID = s;
    }`

Upvotes: 3

Views: 2252

Answers (1)

Soham
Soham

Reputation: 4417

You can do something like

    @Entity
    @Table(name = "user_sessions")
    public class UserSession{

    @Column(name="uid")
    private Long userID;
    @Id
    @Column(name="access_key")
    private String accessKey;
    @Column(name="secret_key")
    private String secretKey;
    @JsonIgnore
    public Long getUserID() {
    return userID;
    }

    public void setUserID(Long s) {
        this.userID = s;
    }`

Now if you use jackson like

ObjectMapper mapper = new ObjectMapper();   
String json =  mapper.writeValueAsString(userSession);

then it will simple ignore the userId

or you can do as below

    @Entity
    @Table(name = "user_sessions")
    @JsonIgnoreProperties( { "userID" })
    public class UserSession{

    @Column(name="uid")
    private Long userID;
    @Id
    @Column(name="access_key")
    private String accessKey;
    @Column(name="secret_key")
    private String secretKey;

    public Long getUserID() {
    return userID;
    }

    public void setUserID(Long s) {
        this.userID = s;
    }`

Upvotes: 2

Related Questions