第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

從Spring MVC作為JSON發(fā)送時動態(tài)忽略Java對象中的字段

從Spring MVC作為JSON發(fā)送時動態(tài)忽略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" : "john@gmail.com",  "encryptedPwd" : "Co7Fwd1fXYk=",  "createdBy" : null,  "updatedBy" : null}如果我不想設(shè)置加密的密碼,則將該字段也設(shè)置為null。但是我不想這樣,我不想將所有字段發(fā)送到客戶端。如果我不希望發(fā)送密碼,updatedby,createdby字段,則我的結(jié)果JSON應(yīng)該像{  "userId" :2,  "userName" : "john",  "emailId" : "john@gmail.com"}我不想發(fā)送給其他數(shù)據(jù)庫表的客戶端的字段列表。因此,它將根據(jù)登錄的用戶進行更改。我該怎么做?
查看完整描述

3 回答

?
梵蒂岡之花

TA貢獻1900條經(jīng)驗 獲得超5個贊


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


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


@JsonIgnore

@JsonProperty(value = "user_password")

public java.lang.String getUserPassword() {

    return userPassword;

}


查看完整回答
反對 回復(fù) 2019-11-25
  • 3 回答
  • 0 關(guān)注
  • 1148 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號