How to use @JsonView to serialize/de-serialize and customize JSON format from Java Object.

In the tutorial, JavaSampleApproach shows you how to use the @JsonView annotation of Jackson library to serialize/de-serialize Java objects and customize the fields of JSON view.

Related articles:
Jackson to convert Java Object to/from Json String
Integrate Spring RestAPIs with @JsonView
How to resolve Json Infinite Recursion problems when working with Jackson

I. Technologies

– Java: 1.8
– Maven: 3.3.9
– Spring Tool Suite: Version 3.8.4.RELEASE
– Spring Boot: 1.5.4.RELEASE
– Jackson library
– Json library

II. @JsonView annotation

For the tutorial, We create 2 models Company & Product:



In the past tutorial, We use Jackson to convert Java Object to/from Json String. Can we customize the view of Json String?
-> Yes, @JsonView help us to do it! We use @JsonView to indicate what fields of Java Objects will be serialized for presenting in Json String.

We define 3 views {OveralView, DetailView, ProductView}:

Now make some changes for our models with @JsonView:



-> When serialize some Customer and Product objects, we have customized views as below:

III. Practice

Create a SpringBoot project, then use Jackson to serialize/de-serialize Java objects with @JsonView for customized view.

@JsonView - project structure

Step to do:
– Create SpringBoot project
– Create JsonView class
– Create Java models wih @JsonView
– Implement Serialize/Deserialize functions with JsonView
– Implement a simple application
– Run and check results

1. Create SpringBoot project

Using Spring Tool Suite to create a Spring Starter Project, then add dependencies {jackson-databind, org.json}:

Note: jackson-databind is main dependency to serialize/de-serialize Java objects, org.json is optional (just use to do some code in this tutorial).

2. Create JsonView class

– Create a JsonView class with 3 customized views {OveralView, DetailView, ProductView}

3. Create Java models with @JsonView

Create 2 models {Company, Product}



Question: Why need default constructors {public Company(),public Product()}?
-> They are used by Jackson for databind. If not having, an exception com.fasterxml.jackson.databind.JsonMappingException will be thrown.

4. Implement Serialize/Deserialize functions with JsonView

Create a utility class JsonViewSerializeUtils with 2 functions {serializeObjectToString, deserializeStringToObject}

What is MapperFeature.DEFAULT_VIEW_INCLUSION? Feature is used to determine the properties that have no view annotations are included in JSON serialization views. If enabled, non-annotated properties will be included; when disabled, they will be excluded.

Example: the Json String will contain “company”:null if we don’t disable DEFAULT_VIEW_INCLUSION

5. Implement a simple application

6. Run and check results

Build and Run the SpringBoot project with commandlines: mvn clean install and mvn spring-boot:run

-> Results:

IV. Source code


By grokonez | June 24, 2017.

Related Posts

Got Something To Say:

Your email address will not be published. Required fields are marked *