As part of my requirement, I am looking at a web service that uses the Employee class to input in JSON format. The class of employees is as follows. If you see that there are 3 properties inside the class, such as status, password, createTime. Now I'm trying to prevent the user from providing properties such as status and creationTime. I want to say that I do not want to allow the user to enter JSON as: -
{ "emp_id": " xyz@gmail.com ", "credentials" : {"password": "xxxxx"}, "status": "ACTIVE", "creationTime": "<UTC time>" }
When the status and creation time are entered, this should lead to the 400 error message. Similarly, when I return the result back to the user, for example, return Response.status(Status.ACCEPTED).entity(employee).build(); , it should not display createTime or credentials. It should look like this: -
{ "emp_id": " xyz@gmail.com ", "status": "ACTIVE", }
I could see that there is a @JsonIgnore property that does not work in my case for status. I tried Jackson.
My Employee class is as follows:
import java.util.Date; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; import org.codehaus.jackson.annotate.JsonIgnore; import org.codehaus.jackson.annotate.JsonProperty; @XmlRootElement public class Employee { @XmlElement(name = "emp_id", required = true) @JsonProperty("emp_id") private String empId; private Credentials credentials; private String status; private Date creationTime; public String getEmpId() { return empId; } public void setEmpId(String empId) { this.empId = empId; } public Credentials getCredentials() { return credentials; } public void setCredentials(Credentials credentials) { this.credentials = credentials; } @JsonIgnore public String getStatus() { return status; } public void setStatus(String status) { this.status = status; } public Date getCreationTime() { return creationTime; } public void setCreationTime(Date creationTime) { this.creationTime = creationTime; } }
source share