title: Ignore fields from Java object dynamically while sending as JSON from Spring MVC
body: <p>I have model class like this, for hibernate</p>
  
  <pre><code>@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;
      }
  }
  </code></pre>
  
  <p>In Spring MVC controller, using DAO, I am able to get the object. and returning as JSON Object.</p>
  
  <pre><code>@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;
      }
  }
  </code></pre>
  
  <p>View part is done using AngularJS, so it will get JSON like this</p>
  
  <pre><code>{
    "userId" :2,
    "userName" : "john",
    "emailId" : "john@gmail.com",
    "encryptedPwd" : "Co7Fwd1fXYk=",
    "createdBy" : null,
    "updatedBy" : null
  }
  </code></pre>
  
  <p>If I don't want to set encrypted Password, I will set that field also as null.</p>
  
  <p>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</p>
  
  <pre><code>{
    "userId" :2,
    "userName" : "john",
    "emailId" : "john@gmail.com"
  }
  </code></pre>
  
  <p>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?</p>
  
  <p>I hope You got my question. </p>