我的@JsonCreator和MixIn注释有什么问题?

mav*_*ick 5 java jackson

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

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

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

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

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));
}}
Run Code Online (Sandbox Code Playgroud)

但我得到了例外

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?)
Run Code Online (Sandbox Code Playgroud)

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

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

谢谢

Sta*_*Man 7

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