I have model class like this, for hibernate
@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; } public void setEncryptedPwd(String encryptedPwd) { this.encryptedPwd = encryptedPwd; } public void setCreatedBy(String createdBy) { this.createdBy = createdBy; } @Column(name = "UpdatedBy", length = 100) public String getUpdatedBy() { return this.updatedBy; } public void setUpdatedBy(String updatedBy) { this.updatedBy = updatedBy; } } In Spring MVC controller, using DAO, I am able to get the object. and returning as JSON Object.
@Controller public 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; } } View part is done using AngularJS, so it will get JSON like this
{ "userId" :2, "userName" : "john", "emailId" : "[email protected]", "encryptedPwd" : "Co7Fwd1fXYk=", "createdBy" : null, "updatedBy" : null } If I don't want to set encrypted Password, I will set that field also as null.
But I don't want like this, I dont want to send all fields to client side. If I dont want password, updatedby, createdby fields to send, My result JSON should be like
{ "userId" :2, "userName" : "john", "emailId" : "[email protected]" } The list of fields which I don't want to send to client coming from other database table. So it will change based on the user who is logged in. How can I do that?
I hope You got my question.
entityClass.setFieldToIgnore(null). The spring-mvc is by default set to ignore null values when deserializing, or you can configure it manually