亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從Spring MVC作為JSON發送時動態忽略Java對象中的字段

從Spring MVC作為JSON發送時動態忽略Java對象中的字段

海綿寶寶撒 2019-11-25 14:29:55
我有這樣的模型類,用于休眠@Entity@Table(name = "user", catalog = "userdb")@JsonIgnoreProperties(ignoreUnknown = true)public class User implements java.io.Serializable {    private Integer userId;    private String userName;    private String emailId;    private String encryptedPwd;    private String createdBy;    private String updatedBy;    @Id    @GeneratedValue(strategy = IDENTITY)    @Column(name = "UserId", unique = true, nullable = false)    public Integer getUserId() {        return this.userId;    }    public void setUserId(Integer userId) {        this.userId = userId;    }    @Column(name = "UserName", length = 100)    public String getUserName() {        return this.userName;    }    public void setUserName(String userName) {        this.userName = userName;    }    @Column(name = "EmailId", nullable = false, length = 45)    public String getEmailId() {        return this.emailId;    }    public void setEmailId(String emailId) {        this.emailId = emailId;    }    @Column(name = "EncryptedPwd", length = 100)    public String getEncryptedPwd() {        return this.encryptedPwd;    }   在Spring MVC控制器中,使用DAO,我可以獲取對象。并返回為JSON對象。@Controllerpublic class UserController {    @Autowired    private UserService userService;    @RequestMapping(value = "/getUser/{userId}", method = RequestMethod.GET)    @ResponseBody    public User getUser(@PathVariable Integer userId) throws Exception {        User user = userService.get(userId);        user.setCreatedBy(null);        user.setUpdatedBy(null);        return user;    }}視圖部分是使用AngularJS完成的,因此它將獲得像這樣的JSON{  "userId" :2,  "userName" : "john",  "emailId" : "[email protected]",  "encryptedPwd" : "Co7Fwd1fXYk=",  "createdBy" : null,  "updatedBy" : null}如果我不想設置加密的密碼,則將該字段也設置為null。但是我不想這樣,我不想將所有字段發送到客戶端。如果我不希望發送密碼,updatedby,createdby字段,則我的結果JSON應該像{  "userId" :2,  "userName" : "john",  "emailId" : "[email protected]"}我不想發送給其他數據庫表的客戶端的字段列表。因此,它將根據登錄的用戶進行更改。我該怎么做?
查看完整描述

3 回答

?
梵蒂岡之花

TA貢獻1900條經驗 獲得超5個贊


將@JsonIgnoreProperties("fieldname")注釋添加到您的POJO。


或者,您可以@JsonIgnore在反序列化JSON時在要忽略的字段名稱之前使用。例:


@JsonIgnore

@JsonProperty(value = "user_password")

public java.lang.String getUserPassword() {

    return userPassword;

}


查看完整回答
反對 回復 2019-11-25
  • 3 回答
  • 0 關注
  • 1139 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號