使用没有注释的@JsonIdentityInfo [英] Using @JsonIdentityInfo without annotations

查看:1183
本文介绍了使用没有注释的@JsonIdentityInfo的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Jackson 2.2.3将POJO序列化为JSON。然后我遇到了问题,我无法序列化递归结构......我通过使用解决了这个问题@JsonIdentityInfo =>效果很好。

I use Jackson 2.2.3 to serialize POJOs to JSON. Then I had the problem, that I couldn't serialize recursive structures...I solved this problem by using @JsonIdentityInfo => works great.

但是,我不希望这个注释位于我的POJO之上。

But, I don't want this annotation on the top of my POJO.

所以我的问题是:是否还有其他可能设置我的 ObjectMapper 的默认行为来使用该功能每个POJO?

So my question is: Is there any other possibility to set the default behavior of my ObjectMapper to use the feature for every POJO?

所以我想转换这个注释代码

So I want to transform this annotation code

@JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="@id")

类似于

ObjectMapper om = new ObjectMapper();
om.setDefaultIdentityInfo(ObjectIdGenerators.IntSequenceGenerator.class, "@id");

任何想法?

推荐答案

您可以使用 Jackson混合注释 Jackson注释内省运动员

这是一个显示两种方法的示例:

Here is an example showing both methods:

public class JacksonJsonIdentityInfo {
    @JsonIdentityInfo(
            generator = ObjectIdGenerators.IntSequenceGenerator.class, property = "@id")
    static class Bean {
        public final String field;

        public Bean(final String field) {this.field = field;}
    }

    static class Bean2 {
        public final String field2;

        public Bean2(final String field2) {this.field2 = field2;}
    }

    @JsonIdentityInfo(
            generator = ObjectIdGenerators.IntSequenceGenerator.class, property = "@id2")
    static interface Bean2MixIn {
    }

    static class Bean3 {
        public final String field3;

        public Bean3(final String field3) {this.field3 = field3;}
    }

    static class MyJacksonAnnotationIntrospector extends JacksonAnnotationIntrospector {
        @Override
        public ObjectIdInfo findObjectIdInfo(final Annotated ann) {
            if (ann.getRawType() == Bean3.class) {
                return new ObjectIdInfo(
                        PropertyName.construct("@id3", null),
                        null,
                        ObjectIdGenerators.IntSequenceGenerator.class,
                        null);
            }
            return super.findObjectIdInfo(ann);
        }
    }

    public static void main(String[] args) throws JsonProcessingException {
        final Bean bean = new Bean("value");
        final Bean2 bean2 = new Bean2("value2");
        final Bean3 bean3 = new Bean3("value3");
        final ObjectMapper mapper = new ObjectMapper();
        mapper.addMixInAnnotations(Bean2.class, Bean2MixIn.class);
        mapper.setAnnotationIntrospector(new MyJacksonAnnotationIntrospector());
        System.out.println(mapper.writeValueAsString(bean));
        System.out.println(mapper.writeValueAsString(bean2));
        System.out.println(mapper.writeValueAsString(bean3));
    }    
}

输出:

{"@id":1,"field":"value"}
{"@id2":1,"field2":"value2"}
{"@id3":1,"field3":"value3"}

这篇关于使用没有注释的@JsonIdentityInfo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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