我想忽略JSON中的字段只在少数情况下是否可能?

Ani*_*nil 2 java spring json model spring-mvc

我的ModelClass

public class UserPojo{

   private String userEmailId;
   private String userPassword;
   private String userContactNumber;
   //setters and getters
}
Run Code Online (Sandbox Code Playgroud)

我想将UserPojo类对象作为json发送,但在某些情况下,我希望使用userpassword发送,有些情况下没有userpassword可能会发送吗?

在下面的方法中,我想发送没有userpassword的userPojo对象.我的Spring版本是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;
 }   

}
Run Code Online (Sandbox Code Playgroud)

在下面的方法中我想用密码发送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;
 }   
Run Code Online (Sandbox Code Playgroud)

}

我希望你明白我的观点

Mic*_*tel 6

对于您的要求使用@JsonView,如果您想完全忽略某些字段@JsonIgnore.