Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the JSON View class in Jackson and how does it work?

I don't understand what is Jackson's @JsonView(Views.MyClass.class). I know that I can annotate POJO's fields and methods in this way to filter non-annotated ones from being serialized with JSON. But what is the Views.Myclass class? Is it a template class for Jackson library?

And why can there be many classes inside the Views class? For example like this:

 class Views {
            static class Public { }
            static class ExtendedPublic extends PublicView { }
            static class Internal extends ExtendedPublicView { }
  }

Why is it needed and how does it work?

like image 810
IngeniousTom Avatar asked Jul 09 '16 08:07

IngeniousTom


People also ask

What is the use of JSON view?

The JsonView annotation can be used to include/exclude a property during the serialization and deserialization process dynamically. We need to configure an ObjectMapper class to include the type of view used for writing a JSON from Java object using the writerWithView() method.

How Jackson JSON works?

The Jackson ObjectMapper can parse JSON from a string, stream or file, and create a Java object or object graph representing the parsed JSON. Parsing JSON into Java objects is also referred to as to deserialize Java objects from JSON. The Jackson ObjectMapper can also create JSON from Java objects.

How does JSON property work?

The @JsonProperty annotation is used to map property names with JSON keys during serialization and deserialization. By default, if you try to serialize a POJO, the generated JSON will have keys mapped to the fields of the POJO.

What is the use of ObjectMapper class?

Class ObjectMapper. ObjectMapper provides functionality for reading and writing JSON, either to and from basic POJOs (Plain Old Java Objects), or to and from a general-purpose JSON Tree Model ( JsonNode ), as well as related functionality for performing conversions.


1 Answers

Use @JsonView to filter fields depending on the context of serialization. When returning data to a REST client, depending on which REST service was called, we need to limit which data will be serialized while using the same data model.

Lets say we want to create two REST services:

The first service returns some user information like first name and last name but not the messages attached to it.

The second service returns all information from the first service and also the messages attached to the current user.

Sample POJO classes with @JsonView annotation

User Pojo classs

@JsonView(User.Views.Public.class)
    public String getFirstname() {
        return firstname;
    }

 @JsonView(User.Views.Public.class)
    public String getLastname() {
        return lastname;
    }

Message Pojo class

@JsonView(User.Views.Internal.class)
    public List<Message> getMessages() {
        return messages;
    }

Rest controller

@RestController
public class SimpleRestController {

    @Autowired
    SimpleService simpleService;

    @RequestMapping(value = "/user/public", method = RequestMethod.GET)
    @JsonView(User.Views.Public.class)
    public User getUserWithPublicData() {
        return simpleService.loadUser();
    }


    @RequestMapping(value = "/user/internal", method = RequestMethod.GET)
    @JsonView(User.Views.Internal.class)
    public User getUserWithInternalData() {
        return simpleService.loadUser();
    }
}
like image 125
Gangadhar Avatar answered Sep 28 '22 17:09

Gangadhar