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": "[email protected]",
"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": "[email protected]",
"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;
}
}
To ignore individual properties, use the [JsonIgnore] attribute. You can specify conditional exclusion by setting the [JsonIgnore] attribute's Condition property. The JsonIgnoreCondition enum provides the following options: Always - The property is always ignored.
If there are fields in Java objects that do not wish to be serialized, we can use the @JsonIgnore annotation in the Jackson library. The @JsonIgnore can be used at the field level, for ignoring fields during the serialization and deserialization.
ObjectMapper; ObjectMapper objectMapper = new ObjectMapper(); objectMapper. configure(DeserializationFeature. FAIL_ON_UNKNOWN_PROPERTIES, false); This will now ignore unknown properties for any JSON it's going to parse, You should only use this option if you can't annotate a class with @JsonIgnoreProperties annotation.
From Jersey documentation:
JSON binding support via MOXy is a default and preferred way of supporting JSON binding in your Jersey applications since Jersey 2.0. When JSON MOXy module is on the classpath, Jersey will automatically discover the module and seamlessly enable JSON binding support via MOXy in your applications.
Since MOXy supports JAXB annotations, try using @XmlTransient
. It should do the trick.
To use Jackson 2.x as your JSON provider you need to add jersey-media-json-jackson
module to your pom.xml
file:
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.22.1</version>
</dependency>
To use Jackson 1.x you'll need the jersey-media-json-jackson1
module:
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson1</artifactId>
<version>2.22.1</version>
</dependency>
For more information about the dependencies, have a look at Jersey documentation.
If you can, choose Jackson 2.x over Jackson 1.x.
In order to use Jackson as your JSON provider you need to register JacksonFeature
for Jackson 2.x (or Jackson1Feature
for Jackson 1.x) in your ResourceConfig
class (Jersey's own implementation of Application
class):
public class MyApplication extends ResourceConfig {
public MyApplication() {
register(JacksonFeature.class);
}
}
For more details, have a look at the documentation.
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:
Additionally, instead of annotating the getStatus()
method, try annotating the status
field with the proper @JsonProperty
annotation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With