As part of my requirement I am exposing a web service which takes a Employee class as input in JSON format. Employee class as follows. If you see there are 3 properties inside the Class like status, password, creationTime. Now I am trying to stop user from giving properties such as status and creationTime. I mean to say I dont want to allow user to input the JSON as:-
{
"emp_id": "xyz@gmail.com",
"credentials" : {"password": "xxxxx"},
"status": "ACTIVE",
"creationTime": "<UTC time>"
}
When status and creationTime are entered it should result in 400 error message. Similarly when I display the result back to user something like return Response.status(Status.ACCEPTED).entity(employee).build();
it should not display creationTime or credentials. it should look like :-
{
"emp_id": "xyz@gmail.com",
"status": "ACTIVE",
}
I could see that there is a @JsonIgnore property which is not working 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;
}
}
First of all, check what is the Json/XML parser you are using. Jersey defaults to Moxy, so replace Moxy with Jackson as
@JsonIgnore
you are using is Jackson annotation.Add the below dependencies to enable Jackson parser for both Json and XML.
Jersey has the auto discovery mechanism that should register Jackson automatically, if that failed, use the below class.
To disable Moxy, add the below class.
Jersey default JSON provider
From Jersey documentation:
Since MOXy supports JAXB annotations, try using
@XmlTransient
. It should do the trick.Using Jackson as JSON provider
To use Jackson 2.x as your JSON provider you need to add
jersey-media-json-jackson
module to yourpom.xml
file:To use Jackson 1.x you'll need the
jersey-media-json-jackson1
module:For more information about the dependencies, have a look at Jersey documentation.
If you can, choose Jackson 2.x over Jackson 1.x.
Registering Jackson as JSON provider
In order to use Jackson as your JSON provider you need to register
JacksonFeature
for Jackson 2.x (orJackson1Feature
for Jackson 1.x) in yourResourceConfig
class (Jersey's own implementation ofApplication
class):For more details, have a look at the documentation.
Choosing the correct
JsonProperty
annotationEnsure you are using the correct
JsonProperty
annotation according to Jackson version:org.codehaus.jackson.annotate.JsonProperty
com.fasterxml.jackson.annotation.JsonProperty
For more details about the annotations, have a look at the documentation:
Try annotating your field
Additionally, instead of annotating the
getStatus()
method, try annotating thestatus
field with the proper@JsonProperty
annotation.While building the json response object from employee object using JSON-Lib, you can pass on a jsonconfig which can exclude specific properties or you can also set a json property filter to that config like below
OR
For the first part of your question, you can very well use json schema and validate the received json object before processing
whenever your rest web service will recent this json object, it will convert it to POJO object. If your JSON object doesn't contain some fields which are there in POJO object, then those values will be initialized to NULL.
Now you can give 404 response accordingly by checking the null values.