Jersey2 with Jackson and @JsonIgnoreProperties annotation

rest_jersey2_json_ignore_properties_thumb

This tutorial is a follow up to “Consume, produce and ignore JSON properties with Jersey2“. In that previous tutorial we explained the usage of @JsonIgnore and @JsonProperty to manipulate and filter JSON requests and responses using Jersey2 with FasterXML/Jackson. We will extend the provided example (Github) with the @JsonIgnoreProperties annotation.

1. Prerequisites

  1. Install Apache Tomcat (we use version 8.5). If not already installed, please have a look at these 2 tutorials:
  2. Install Apache Maven to build the project
  3. You can just run a “clean install” on the Maven project and deploy the created WAR file to Tomcat directly. Although we recommend to work with an IDE like EclipseEE or IntelliJ to adapt or debug the project
  4. Some tool to test the REST Api (we use Postman)
  5. If you import the Maven project to EclipseEE, activate and adapt the Project Facets to use the Dynamic Web Project 3.1, Java 1.8 and JAX-RS 2.0

2. Code step-by-step

We prepared two classes with the same content in terms of properties. The only difference is the usage of the @JsonIgnoreProperties( ignoreUnknown = false ) and @JsonIgnoreProperties( ignoreUnknown = true ). Additionally we use more complex Object types to showcase the JSON to POJO conversion (Date, List). We generate the id attribute randomly and do not want to use the property provided by JSON request. The date will be ignored as well and set to the current server time.

2.1 POST JSON request

The JSON data contains all attributes of the POJO and an “undefinedProperty” addition to test the behavior of the @JsonIgnoreProperties annotation.

2.2 REST web service

The REST web service consists of two methods which each respond to the following POJOs.

2.3 Set @JsonIgnoreProperties false

If we send the POST request with the JSON data defined above to the server, we get an exception:

Postman_JSON_Ignore_Properties_False_Screen

The exception is thrown because of the “undefinedProperty” defined in the JSON data. However, have a look at the full exception from the stack trace. You get information about the property causing the exception.

You can catch that exception in order to process or log the request. Now lets try to ignore the undefined property.

2.4 Set @JsonIgnoreProperties true

Using the same POST call we get the following result in Postman:

Postman_JSON_Ignore_Properties_True_Screen

You can see that the request was successful and we received a correct response. Consequently, the “undefinedProperty” is ignored. You do not get an exception here and have no chance to log a probably incorrect client request. Notice the differences between the request and response with the id or date property. That is attributed to the implementation and @JsonIgnore and @JsonProperty annotations.

3. Conclusion

Both approaches with @JsonIgnoreProperties true and false make sense. If set to true, you only parse JSON properties that match your POJO definition. But you have no chance to log or notify your client that the request consists of wrong or redundant data.

In contrary, you will receive an exception with the exact problem description if set to false. The problem description contains e.g. which property did not match any objects in your POJO etc.

If you have errors or problems, feel free to comment and ask.

Facebooktwittergoogle_plusredditpinterestlinkedinmail