我一直在为我的一个类使用自定义typeId解析器,到目前为止我一直依赖于注释支持:
@JsonTypeInfo( use = JsonTypeInfo.Id.CUSTOM, include = JsonTypeInfo.As.PROPERTY, property = "@type") @JsonTypeIdResolver(ColumnDefinitionTypeResolver.class)但是现在我需要通过构造函数或setter将一些其他依赖项传递给它来自定义类型解析器的创建,并且由于Jackson是实例化它的人,我无法找到解决方法。
有没有办法配置ObjectMapper使用TypeIdResolver而不是依赖注释?
问候
I've been using a custom typeId resolver for one of my classes, so far I've been leaning on the annotation support:
@JsonTypeInfo( use = JsonTypeInfo.Id.CUSTOM, include = JsonTypeInfo.As.PROPERTY, property = "@type") @JsonTypeIdResolver(ColumnDefinitionTypeResolver.class)But now I need to customize the creation of the type resolver by passing some other dependencies to it via constructor or setters, and since Jackson is the one who instantiate it I can't find a way around it.
Is there a way to configure the ObjectMapper to use a TypeIdResolver instead of relying on annotations?
Regards
最满意答案
所以你有两个选择:
1)如果您已经开始使用@JsonTypeIdResolver在@JsonTypeIdResolver使用static状态。 这可能不是你想要的。
默认的JacksonAnnotationIntrospector将尝试使用JsonTypeIdResolver按照其默认构造函数创建您提供的类型的实例。 目前没有办法将其配置为其他方式。
public final class ColumnDefinitionTypeResolver implements TypeIdResolver { // You could rely on static state. public static String SOME_ACCESSIBLE_OBJECT = null; public ColumnDefinitionTypeResolver() { // This is what gets called. } } ColumnDefinitionTypeResolver.SOME_ACCESSIBLE_OBJECT = "I can affect the implementation from here, but using static state ... be careful";2)创建一个模块来处理类型和子类型的反序列化。
SimpleModule columnDefinitionModule = new SimpleModule("colDefMod", new Version(1, 0, 0, null)) .addDeserializer(ColumnDefinition.class, new JsonDeserializer() { @Override public Object deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { // Need to read the type out and then use ObjectMapper to deserialize using the correct token } }) .registerSubtypes(...); // add your subtypes here. (new ObjectMapper()).registerModule(columnDefinitionModule);有关更详细的示例,请参阅Jackson文档操作方法:自定义反序列化程序 。
So you have two options:
1) If you're set on using the @JsonTypeIdResolver your stuck using static state in your TypeIdResolver. This probably isn't what you want.
The default JacksonAnnotationIntrospector will try to create an instance of the type you provide using JsonTypeIdResolver per its default constructor. There is currently no way to configure it to do otherwise.
public final class ColumnDefinitionTypeResolver implements TypeIdResolver { // You could rely on static state. public static String SOME_ACCESSIBLE_OBJECT = null; public ColumnDefinitionTypeResolver() { // This is what gets called. } } ColumnDefinitionTypeResolver.SOME_ACCESSIBLE_OBJECT = "I can affect the implementation from here, but using static state ... be careful";2) Is create a module to handle deserialization of your type and subtypes.
SimpleModule columnDefinitionModule = new SimpleModule("colDefMod", new Version(1, 0, 0, null)) .addDeserializer(ColumnDefinition.class, new JsonDeserializer() { @Override public Object deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { // Need to read the type out and then use ObjectMapper to deserialize using the correct token } }) .registerSubtypes(...); // add your subtypes here. (new ObjectMapper()).registerModule(columnDefinitionModule);For more detailed examples, see Jackson documentation How-To: Custom Deserializers.
更多推荐
发布评论