Want to hide some fields of an object that are being mapped to JSON by Jackson

后端 未结 7 1925
北海茫月
北海茫月 2020-11-30 03:45

I have a User class that I want to map to JSON using Jackson.

public class User {
    private String name;
    private int age;
    prviate int securityCode;         


        
7条回答
  •  误落风尘
    2020-11-30 04:11

    If you don't want to put annotations on your Pojos you can also use Genson.

    Here is how you can exclude a field with it without any annotations (you can also use annotations if you want, but you have the choice).

    Genson genson = new Genson.Builder().exclude("securityCode", User.class).create();
    // and then
    String json = genson.serialize(user);
    

提交回复
热议问题