本文介绍了有没有办法在mapper配置而不是注释上指定@JsonTypeIdResolver?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

有没有办法在mapper配置上指定@JsonTypeIdResolver而不是注释目标类?
示例,而不是:

Is there a way to specify @JsonTypeIdResolver on mapper config instead of annotating the target class ?example, instead of :

@JsonTypeInfo(
    use = JsonTypeInfo.Id.CUSTOM,
    include = JsonTypeInfo.As.PROPERTY,
    property = "@type")
@JsonTypeIdResolver(FooTypeIdResolver.class)
public class Foo {

}

只有

@JsonTypeInfo(
    use = JsonTypeInfo.Id.CUSTOM,
    include = JsonTypeInfo.As.PROPERTY,
    property = "@type")
public class Foo {

}

并在mapper上执行以下操作

And do something like the following on the mapper

mapper.getSerializationConfig()
.with(new JacksonAnnotationIntrospector())
.without(SerializationFeature.INDENT_OUTPUT);
...
// something like this :
.addTypeIdResolver(Foo.class, FooTypeIdResolver.class)

我问这是因为FooTypeIdResolver位于Foo库不能依赖的不同库上...

I am asking this because the FooTypeIdResolver is on a different "library" which the Foo library cannot depend on...

推荐答案

您可以使用杰克逊的,在您的情况下,您首先要创建一个包含您要添加的注释的类:

You can use Jackson's mix-in annotations, in your case you would first create a class with the annotations you want to add:

@JsonTypeIdResolver(FooTypeIdResolver.class)
private static class FooMixIn {

}

和然后你将这个混合应用到Foo类(粗略地说,这需要在 FooMixIn 上进行注释并将它们应用于 Foo

And then you would apply this mix-in to the Foo class (roughly speaking this takes annotations present on FooMixIn and applies them to Foo)

mapper.getSerializationConfig().addMixInAnnotations(Foo.class, FooMixIn.class);
mapper.getDeserializationConfig().addMixInAnnotations(Foo.class, FooMixIn.class);

这篇关于有没有办法在mapper配置而不是注释上指定@JsonTypeIdResolver?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 00:31