我的@JsonCreator和MixIn注释出了什么问题? [英] what is wrong with my @JsonCreator and MixIn annotation?

查看:260
本文介绍了我的@JsonCreator和MixIn注释出了什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用jackson 1.7尝试从第三方库反序列化对象。

I'm currently using jackson 1.7 attempting to deserialize an object from a third party library.

所以我设置我的ObjectMapper来使用我的mixIn类,如下所示:

So I set up my ObjectMapper to use my mixIn class like this:

    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    mapper.getDeserializationConfig().addMixInAnnotations(com.vividsolutions.jts.geom.Point.class, MixIn.class);

我的MixIn类使用 @JsonCreator 注释并使用逻辑来实例化那里的 Point 对象

And my MixIn class annotated with @JsonCreator and with the logic to instantiate the Point object there

public class MixIn {
private static final GeometryFactory geometryFactory = GeometryFactoryFactory.getGeometryFactory();

@JsonCreator
public static Point createPoint(@JsonProperty("x")double x, @JsonProperty("y")double y) {
    return geometryFactory.createPoint(new Coordinate(x, y));
}}

但是我得到了例外

No suitable constructor found for type [simple type, class com.vividsolutions.jts.geom.Point]: can not instantiate from JSON object (need to add/enable type information?)

调试显示我的MixIn类永远不会被调用,我认为它需要是具体课程,但结果相同。

Debugging shows that my MixIn class is never called, I thought that it needed to be concrete class but had the same result.

我做错了什么?我的配置有什么问题?

What am I doing wrong? What is wrong with my configuration?

谢谢

推荐答案

问题在于假设混合将用于除添加注释之外的任何其他内容。因此,在您的情况下,将添加createPoint()的注释,但除非目标类具有匹配的工厂方法(以添加注释),否则这将不会产生任何影响。
具体来说,mix-ins不能用于注入静态工厂方法;它们只能用于将注释与现有类相关联。

The problem is in assumption that mix-ins would be used for anything other than adding annotations. So in your case, annotation for 'createPoint()' would be added, but unless target class has matching factory method (to add annotations to), this will not have any effect. Specifically, then, mix-ins can not be used to inject static factory methods; they can only be used to associate annotations with existing classes.

这篇关于我的@JsonCreator和MixIn注释出了什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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