如何隐藏一些json作为回应?

时间:2016-08-17 14:51:38

标签: json spring spring-mvc spring-data-jpa spring-data-rest

我有基于spring-data-rest的模块。我得到了这个回复,例如@Entity User。

URL:

http://localhost:8080/Product/rest/users

回复:

 {
      "_embedded" : {
        "users" : [ {
          "def" : "dmityushin",
          "delDate" : null,
          "displayDef" : "Митюшин Дмитрий Александрович",
          "email" : "dmitry.mityushin@megafon.ru",
          "naviDate" : "2015-10-21T12:54:08.559+0000",
          "naviUser" : "admin",
          "phone" : null,
          "pwd" : "266810e0707d7aeb8e838308aa248f3ea116e483",
          "_links" : {
            "self" : {
              "href" : "http://localhost:8080/ProductCatalog/rest/2/users/308"
            },
            "user" : {
              "href" : "http://localhost:8080/ProductCatalog/rest/2/users/308"
            },
            "pscUser" : {
              "href" : "http://localhost:8080/ProductCatalog/rest/2/users/308/pscUser"
            },
            "pscBranch" : {
              "href" : "http://localhost:8080/ProductCatalog/rest/2/users/308/pscBranch"
            }
          }
        }, {
          "def" : "grak",
          "delDate" : null,
          "displayDef" : "Григорий Рак",
          "email" : "grigory.rak@MegaFon.ru",
          "naviDate" : "2015-10-23T11:59:19.546+0000",
          "naviUser" : "admin",
          "phone" : null,
          "pwd" : "c7f33616778ba938272eac0ca0a3364bfc17203d",
          "_links" : {
            "self" : {
              "href" : "http://localhost:8080/ProductCatalog/rest/2/users/316"
            },
            "user" : {
              "href" : "http://localhost:8080/ProductCatalog/rest/2/users/316"
            },
            "pscUser" : {
              "href" : "http://localhost:8080/ProductCatalog/rest/2/users/316/pscUser"
            },
            "pscBranch" : {
              "href" : "http://localhost:8080/ProductCatalog/rest/2/users/316/pscBranch"
            }
          }
        }...OTHERS 

我想隐藏密码和其他一些字段。我怎样才能做到这一点?我发现关于Spring的这个模块的材料非常少。

我的实体代码:

@XmlRootElement
@XmlAccessorType(value = XmlAccessType.FIELD)
@Entity
@Table(name = "psc_users")
@NamedQuery(name = "User.findAll", query = "SELECT u FROM User u")
public class User implements Serializable {
    private static final long serialVersionUID = 8885916014620036457L;

    @Id
    private static final String SEQUENCE_NAME = "psc_users_user_id_seq";
    @Id
    @GeneratedValue(...)
    @GenericGenerator(...)
    @Column(name = "USER_ID")
    private Long userId;

    @Column(name = "DEF")
    private String def;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "DEL_DATE")
    private Date delDate;

    @Column(name = "DISPLAY_DEF", length = 60)
    private String displayDef;

    @Column(name = "EMAIL", length = 60)
    private String email;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "NAVI_DATE")
    private Date naviDate;

    @Column(name = "NAVI_USER")
    private String naviUser;

    @Column(name = "PHONE", length = 30)
    private String phone;

    @Column(name = "PWD", length = 40)
    private String pwd;

    //bi-directional many-to-one association to Branch
    @ManyToOne(cascade = CascadeType.MERGE)
    @JoinColumn(name = "BRNC_BRNC_ID", nullable = false)
    private Branch pscBranch;

    //bi-directional many-to-one association to User
    @ManyToOne(cascade = CascadeType.MERGE)
    @JoinColumn(name = "CURATOR_USER_ID")
    private User pscUser;

    public User() {
    }

    ...get() methods

}

抱歉我的英文不好并提前致谢(:

1 个答案:

答案 0 :(得分:6)

要隐藏某些字段,您可以在要隐藏的字段的getter上使用@JsonIgnore,但不要忘记在该字段的setter上添加@JsonSetter