结合杰克逊 @JsonView 和 @JsonProperty [英] Combine Jackson @JsonView and @JsonProperty

查看:25
本文介绍了结合杰克逊 @JsonView 和 @JsonProperty的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法不仅可以通过在@JsonView 中使用不同的类来查看/隐藏字段,还可以根据分别用于每个字段的视图定义不同的名称(如@JsonProperty)?

Is there a way to not only view/hide fields by using different classes in @JsonView but also define different names (like with @JsonProperty) depending on the view used for each field separately?

你好谢谢!蒂姆

推荐答案

我的解决方案涉及 Jackson Mixin功能.
我使用相同的视图类来放置不同的 @jsonProperty 注释.这比单独的类更方便,但是,现在您不能使用视图的继承.如果需要,您必须创建单独的类来保存 @jsonProperty 注释并相应地更改 mixin 模块.

My solution involves Jackson Mixin feature.
I used the same view class to place the different @jsonProperty annotations. This is more convinient than a seperate class, however, now you cannot use inheritence of views. If you need this, you will have to create seperate classes that hold the @jsonProperty annotations and change the mixin modules accordingly.

这里是要序列化的对象:

Here is the object to be serialized:

public class Bean
{
    @JsonView({Views.Public.class, Views.Internal.class})
    public String name;

    @JsonView(Views.Internal.class)
    public String ssn;
}

带有 json 属性注释的视图

The views with the json property annotations

public class Views
{
    public static class Public {
        @JsonProperty("public_name")
        public String name;
    }

    public static class Internal {
        @JsonProperty("internal_name")
        public String name;
    }
}

Jackson 所需的 mixin 模块:

The mixin modules, required by Jackson:

@SuppressWarnings("serial")
public class PublicModule extends SimpleModule
{
    public PublicModule() {
        super("PublicModule");
    }

    @Override
    public void setupModule(SetupContext context)
    {
        context.setMixInAnnotations(Bean.class, Views.Public.class);
    }
}

@SuppressWarnings("serial")
public class InternalModule extends SimpleModule
{
    public InternalModule() {
        super("InternalModule");
    }

    @Override
    public void setupModule(SetupContext context)
    {
        context.setMixInAnnotations(Bean.class, Views.Internal.class);
    }
}

测试方法:

public static void main(String[] args)
{
    Bean bean = new Bean();
    bean.name = "my name";
    bean.ssn = "123-456-789";

    ObjectMapper mapper = new ObjectMapper();
    System.out.println(args[0] + ": ");
    try {
        if (args[0].equals("public")) {
            mapper.registerModule(new PublicModule());
            mapper.writerWithView(Views.Public.class).writeValue(System.out, bean);
        } else {
            mapper.registerModule(new InternalModule());
            mapper.writerWithView(Views.Internal.class).writeValue(System.out, bean);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

带有两个单独调用的输出:

output with two separate invocations:

public: 
{"public_name":"my name"}
internal: 
{"ssn":"123-456-789","internal_name":"my name"}

这篇关于结合杰克逊 @JsonView 和 @JsonProperty的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆