Integrate Spring RestAPIs with @JsonView

In the previous post, We had learned how to use @JsonView to serialize/de-serialize Java objects. Now we go to next step, JavaSampleApproach shows you how to integrate Spring RestAPIs with @JsonView to customize views and resolve infinity loop problem.

Related posts:
How to use @JsonView to serialize/de-serialize and customize JSON format from Java Object.
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. Integrate Spring RestAPIs with @JsonView

For the tutorial, We create 2 models Company & Product which have one-to-many relationship.



What problems when retrieving Company/Product objects with 2 RestAPIs {‘/get/company’, ‘/get/product’}:

-> We will face with Infinity Loop problem:

spring restapis jsonview - Infinity Loop problem

Exceptions thrown from server side:

Another question: How to customize returned views?

=> Solution: @JsonView can help us to resolve 2 above problems: infinity loop issue and customized views:

III. Practice

We create a SpringBoot project with some RestAPIs which use @JsonView to customize returned views.

spring jsonview - project structure

Step to do:
– Create SpringBoot project
– Create JsonView class
– Create model classes
– Implement RestAPIs
– Configure logback for file
– Run and check results

1. Create SpringBoot project

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

2. Create JsonView class

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

3. Create model classes

Create 2 models {Company, Product}



4. Implement RestAPIs

We implement a WebController which has 5 RestAPIs {‘/get/company’, ‘/get/product’, ‘/get/company/overalview’, ‘/get/company/detailview’, ‘/get/product/view’} and use @JsonView to customize returned views.

5. Configure logback for file

Under folder /src/main/resources, we create a logback file logback-spring.xml:

Open file, then add path for log file:

6. Run and check results

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

– Make a request: http://localhost:8080/get/company

-> We face with infinity loop problem:

spring restapis jsonview - Infinity Loop problem

Open the jsa-app.log, see exceptions from server-side:

The same issue infinity loop when we make a request: http://localhost:8080/get/product

– Make a request to get overal view of company: http://localhost:8080/get/company/overalview

-> Result:

– Make a request to get detail view of company: http://localhost:8080/get/company/detailview

-> Result:

– Make a request to get product view: http://localhost:8080/get/product/view

-> Result:

IV. Source code


By grokonez | June 25, 2017.

Related Posts

Got Something To Say:

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