2016-08-17 49 views
1

我ModelClass我只想在少數情況下忽略來自JSON的字段是否可能?

public class UserPojo{ 

    private String userEmailId; 
    private String userPassword; 
    private String userContactNumber; 
    //setters and getters 
} 

我想送UserPojo類對象爲JSON,但在一些情況下我想,userPassword爲發送和無的userPassword一些情況下這可能嗎?

在下面的方法中,我想發送沒有userpassword的userPojo對象。 我的春季版本是4.3.1.RELEASE。我有傑克遜

@RestController 
@RequestMapping("/user") 
public class UserController{ 

@GetMapping(value="/{userId}") 
public UserPojo getUser(@PathVariable("userId") String userId){ 

    //code goes here 
    //finally returning UserPojo Object 
    return userPojo; 
} 

} 

在下面的方法我想送userPojo對象與密碼

@RestController 
@RequestMapping("/admin") 
public class AdminController{ 

@GetMapping(value="/{userId}") 
public UserPojo getUser(@PathVariable("userId") String userId){ 

    //code goes here 
    //finally returning UserPojo Object 
    return userPojo; 
} 

}

我希望你有我的觀點

+1

你想在什麼情況下忽略字段? – ThatAwesomeCoder

+0

當然,你試過了嗎?你的json庫應該自動執行,如果你需要更多的需求請解釋 –

回答

5

對於您的要求使用@JsonView

1

是的,這是通過使用@JsonView註釋實現。

這裏是一個很好的教程如何,如果你想完全忽略一些領域則@JsonIgnore使用feature

相關問題