Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore as well as property editing for JSON objects in Jersey Rest

Tags:

java

rest

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;
    }

}
like image 228
Coding the beast Avatar asked Nov 23 '15 06:11

Coding the beast


People also ask

How do I ignore properties in JSON?

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.

How do you ignore certain fields based on a serializing object to JSON?

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.

How do I ignore properties in ObjectMapper?

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.


1 Answers

Jersey default JSON provider

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.

Using Jackson as JSON provider

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.

Registering Jackson as JSON provider

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.

Choosing the correct JsonProperty annotation

Ensure you are using the correct JsonProperty annotation according to Jackson version:

  • Jackson 1.x: org.codehaus.jackson.annotate.JsonProperty
  • Jackson 2.x com.fasterxml.jackson.annotation.JsonProperty

For more details about the annotations, have a look at the documentation:

  • Jackson 1.x annotations
  • Jackson 2.x annotations

Try annotating your field

Additionally, instead of annotating the getStatus() method, try annotating the status field with the proper @JsonProperty annotation.

like image 75
cassiomolin Avatar answered Sep 30 '22 20:09

cassiomolin