Json View

Recently  I had a requirement. When exposing a service as REST and making data available at GET endpoint as JSON, I wanted to filter out some fields. For example, I have a User class with the following properties.

class User
{
   Integer userId;
   String firstName;
   String lastName;
   String email;
   String phone;
   Integer age;
   String homeAdress;
   String officeAddress;
}

Many times I don’t need the whole information of the User and instead of that I only want the [userId, firstName, lastName,email and phone] only. For that I could have used the @JsonIgnore annotation on those unnecessary properties. But if I do so, these properties will always  be ignored and I could never be able to get the full User information in case it becomes necessary.

Fortunately, JSON has a concept of Json View. And it’s really simple to implement. Here is the complete source code for it. Let me only show you what is the important point in the implementation.

Step 1

Let’s make a Profile class.

package com.anupam.app.profile;

import org.omg.CORBA.COMM_FAILURE;

/**
 * Created by brisatc186.gogoi on 10/11/2016.
 */
public class DataProfile
{

    /**
     * [id,firstName,lastName] of {@link com.anupam.app.controller.User}
     */
    public static class CommonInfo
    {
    }

    ;

    /**
     * [id,firstName,lastName,email,phone] of {@link com.anupam.app.controller.User}
     */
    public static class BriefInfo extends CommonInfo
    {
    }

    ;

    /**
     * [id,firstName,lastName,email,phone,age,homeAddress,officeAddress] of {@link com.anupam.app.controller.User}
     */
    public static class FullInfo extends CommonInfo
    {
    }

    ;
}

Step 2:

Let’s assign these profiles to the properties of the User.clss

class User
{
    @JsonView(DataProfile.CommonInfo.class)
    Integer userId;

    @JsonView(DataProfile.CommonInfo.class)
    String firstName;

    @JsonView(DataProfile.CommonInfo.class)
    String lastName;

    @JsonView({DataProfile.BriefInfo.class, DataProfile.FullInfo.class})
    String email;

    @JsonView({DataProfile.BriefInfo.class, DataProfile.FullInfo.class})
    String phone;

    @JsonView(DataProfile.FullInfo.class)
    Integer age;

    @JsonView(DataProfile.FullInfo.class)
    String homeAdress;

    @JsonView(DataProfile.FullInfo.class)
    String officeAddress;

    public String getPhone()
    {
        return phone;
    }

    public void setPhone(String phone)
    {
        this.phone = phone;
    }

    public Integer getUserId()
    {
        return userId;
    }

    public void setUserId(Integer userId)
    {
        this.userId = userId;
    }

    public String getFirstName()
    {
        return firstName;
    }

    public void setFirstName(String firstName)
    {
        this.firstName = firstName;
    }

    public String getLastName()
    {
        return lastName;
    }

    public void setLastName(String lastName)
    {
        this.lastName = lastName;
    }

    public Integer getAge()
    {
        return age;
    }

    public void setAge(Integer age)
    {
        this.age = age;
    }

    public String getHomeAdress()
    {
        return homeAdress;
    }

    public void setHomeAdress(String homeAdress)
    {
        this.homeAdress = homeAdress;
    }

    public String getOfficeAddress()
    {
        return officeAddress;
    }

    public void setOfficeAddress(String officeAddress)
    {
        this.officeAddress = officeAddress;
    }

    public String getEmail()
    {
        return email;
    }

    public void setEmail(String email)
    {
        this.email = email;
    }
}

Step 3:

Now lets create a simple controller and on each method let’s define the Profile we want.

package com.anupam.app.controller;

import com.anupam.app.profile.DataProfile;
import com.fasterxml.jackson.annotation.JsonView;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by brisatc186.gogoi on 10/11/2016.
 */

@RestController
@RequestMapping("/user")
public class UserController
{

    @GetMapping(path = "/full_info", produces = MediaType.APPLICATION_JSON_VALUE)
    @JsonView(DataProfile.FullInfo.class)
    public User getFullUserInfo()
    {
        User user = makeDummyData();
        return user;
    }

    @GetMapping(path = "/brief_info", produces = MediaType.APPLICATION_JSON_VALUE)
    @JsonView(DataProfile.BriefInfo.class)
    public User getBriefUserInfo()
    {
        User user = makeDummyData();
        return user;
    }

    public User makeDummyData()
    {
        User user = new User();
        user.setUserId(1);
        user.setFirstName("Json");
        user.setLastName("Bond");
        user.setEmail("json.bond@gmail.com");
        user.setPhone("11-34560987");
        user.setAge(37);
        user.setHomeAdress("LA");
        user.setOfficeAddress("CA");
        return user;
    }
}

That’s it. Now run the application and use the uris,

http://localhost:8080/user/brief_info

http://localhost:8080/user/full_info

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s