Spring MVC 4 REST + Jackson @JsonView Annotation Integration Example

This page will walk through Spring MVC 4 REST and Jackson @JsonView annotation integration example. @JsonView is used to filter JSON response. Spring 4.1 framework and onwards started to support Jackson @JsonView annotation directly. To work with @JsonView, first we need to define view definition. We can have more than one view definition. One view definition can inherit other view definition. Our POJO will be annotated with @JsonView by passing defined view as an attribute. In spring controller we can directly annotate our methods with Jackson @JsonView annotation. The JSON response will be filtered on the basis of configured view while serializing HTTP response body. We can also use spring REST client with @JsonView. Spring provides MappingJacksonValue that wraps the POJO and by setting serialization view, we can filter the JSON being sent to web service server using RestTemplate. Now find the complete example step by step.

Software Requirement

Find the required software to run the demo.
1. Java 8
2. Spring 4.2.6.RELEASE
3. Jackson 2.6.6
4. Tomcat 8
5. Gradle
6. Eclipse

Project Structure in Eclipse

Find the project structure in eclipse.

Gradle File with Spring Boot

Find the gradle file used in our example. We are using spring boot that will import all the required JAR dependencies of Spring and Jackson API.

Create JSON View Definition

The object can be contextually filtered while serializing HTTP response using @JsonView. The first step to use @JsonView, we need to create JSON view definitions. We need to create interfaces or static classes. For more than one view we can create more than one interface. It is better to create composite interfaces. Create more than interface within a class for more than one serialized view as HTTP response. To inherit one view by other, we need to inherit one interface by other.

Look at the class. We are creating a view filter mechanism for the user profile. We are creating three profile views and those are public view, friends view and family view. Friends view is inheriting public view and family view is inheriting friends view.

Create POJO Using Jackson @JsonView Annotation

Jackson provides the annotation com.fasterxml.jackson.annotation.JsonView that needs to be annotated on class properties to include in a specific JSON view serialized in HTTP response body. We have three views here in our example i.e PublicView, FriendsView and FamilyView. The properties annotated with @JsonView will take part in filtering JSON views.

1. In user public profile view, only those class properties will be available which are annotated with @JsonView(Profile.PublicView.class).
2. In user friends profile view, the class properties will be available which are annotated with @JsonView(Profile.PublicView.class) and @JsonView(Profile.FriendsView.class).
3. In user family profile view the properties will be available which are annotated with @JsonView(Profile.PublicView.class), @JsonView(Profile.FriendsView.class) and @JsonView(Profile.FamilyView.class)




Create Spring MVC Controller with @JsonView Annotation

Spring 4.1 onwards it directly supports Jackson @JsonView annotation. In our controller the methods annotated with @RequestMapping can be directly annotated with @JsonView to filter JSON view in serialized response body.

1. In our example the controller method annotated with @JsonView(Profile.PublicView.class) will produce JSON response only with public view properties.
2. The controller method annotated with @JsonView(Profile.FriendsView.class) will produce JSON response with public and friends view properties.
3. The controller method annotated with @JsonView(Profile.FamilyView.class) will produce JSON response with public, friends and family view properties.
4. The controller method getCompleteProfileById() has not been annotated with any view. It will produce JSON with each and every property as usual.


Create Java Configuration and Application Initializer

Find the java configuration used in our example.
AppConfig.java Find the web application initializer.

Run Application

To run the application, find the below steps.
1. Download source code and navigate to root directory of the project.
2. Run command gradle clean build using command prompt.
3. Deploy the WAR file in tomcat.

We will get JSON output as follows.
4. To test the @JsonView(Profile.FriendsView.class)
We will get JSON output as follows. 5. To test the @JsonView(Profile.FamilyView.class)
We will get JSON output as follows. 6. To test the getCompleteProfileById() method
We will get JSON output as follows.

Spring REST Client with @JsonView using MappingJacksonValue

The Jackson @JsonView annotation can also be used with RestTemplate in spring REST client. To achieve it spring provides MappingJacksonValue. It wraps the given POJO to be serialized. For the example we are creating user name example. A user name can have first, middle and last name. We are creating name views such as FirstName and FirstLastName. Find the view defined for user name.
NameView.java Find the POJO being used in our REST client example.
Name.java Now using MappingJacksonValue we wrap our POJO as follows.
Name.java To filter the view, set the serialization view as follows. In REST web service UserController class, we have exposed a method as given below.