java Jackson 中的 JSON View 类是什么,它是如何工作的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38279782/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 03:16:52  来源:igfitidea点击:

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

javajsonserializationHymanson

提问by IngeniousTom

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

我不明白什么是Hyman逊的 @JsonView( Views.MyClass.class)。我知道我可以通过这种方式注释 POJO 的字段和方法,以过滤未注释的字段和方法,以免被 JSON 序列化。但是什么是 Views.Myclass 类?它是Hyman逊图书馆的模板类吗?

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

为什么 Views 类里面可以有很多类?例如像这样:

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

Why is it needed and how does it work?

为什么需要它以及它是如何工作的?

回答by Gangadhar

Use @JsonViewto 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.

用于@JsonView根据序列化上下文过滤字段。当向 REST 客户端返回数据时,根据调用的 REST 服务,我们需要限制在使用相同数据模型时将序列化哪些数据。

Lets say we want to create two REST services:

假设我们要创建两个 REST 服务:

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 @JsonViewannotation

@JsonView注释的示例 POJO 类

User Pojo classs

用户 Pojo 类

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

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

Message Pojo class

消息 Pojo 类

@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();
    }
}