20

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?

morten.c
  • 3,414
  • 5
  • 40
  • 45
IngeniousTom
  • 467
  • 2
  • 7
  • 22

1 Answers1

50

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();
    }
}
malana
  • 5,045
  • 3
  • 28
  • 41
Gangadhar
  • 766
  • 7
  • 8
  • @ Gangadhar So basically JsonView might be used like java access modifiers in terms of REST services right? If so - then I see what is the reason of existence of this variaty of view classes. Your answer is pretty clear. Thank you. But could you tell me, please, if I had only one view (access level i mean) in my porgramm - could I use just JasonView() (without class in bracers) to dispense just default object.class as view class? – IngeniousTom Jul 09 '16 at 09:05
  • 1
    If you don't have different classes, you probably don't need to use @JsonView, and the serializer will include everything. But JsonView also solves serialization problems with bi-directional relationships with JPA and Jackson, so it's pretty common to use it, but you also have another ways to solve the serialization problem. – fabioresner Jul 18 '17 at 20:38
  • By classes I refer to views/levels in you question – fabioresner Jul 18 '17 at 20:44