@JsonCreator和通过模块的mixin无法用于第三党课程 [英] @JsonCreator and mixin via Module not working for 3rd Party Class

查看:122
本文介绍了@JsonCreator和通过模块的mixin无法用于第三党课程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试反序列化java.net.HttpCookie,它没有默认的no-arg构造函数,并且正在获取: org.codehaus.jackson.map.JsonMappingException:未找到类型[简单类型,类java.net.HttpCookie]的合适构造函数:无法从JSON对象实例化(需要添加/启用类型信息吗?) 在[来源:java.io.StringReader@5a395674;第1行,第35列,

I am trying to deserialize java.net.HttpCookie which doesn't have a default no-arg constructor and am getting: org.codehaus.jackson.map.JsonMappingException: No suitable constructor found for type [simple type, class java.net.HttpCookie]: can not instantiate from JSON object (need to add/enable type information?) at [Source: java.io.StringReader@5a395674; line: 1, column: 35

这是与jackson-mapper-asl v 1.9.13一起使用的

This is with jackson-mapper-asl v 1.9.13

我找到了没有默认构造函数的杰克逊3rd Party类,并尝试同时通过getDeserializationConfig和using模块使用其解决方案.我在下面介绍了模块代码.

I found Jackson 3rd Party Class With No Default Constructor and attempted to use their solution via both getDeserializationConfig and using module. I present the module code below.

abstract class HttpCookieMixIn {
    @JsonCreator
    public HttpCookieMixIn(@JsonProperty("name") String name, @JsonProperty("value") String value) {
        logger.info("Mixin called!");
    }
}

public class MyModule extends SimpleModule {
    public MyModule() {
        super("ModuleName", new Version(0,0,1,null));
    }

    @Override
    public void setupModule(SetupContext context) {
        context.setMixInAnnotations(java.net.HttpCookie.class, HttpCookieMixIn.class);
        logger.info("Set mixin annotation");
    }
}

在服务器端点的构造函数中,我具有以下内容:

In the server endpoint's constructor I have the following:

public ServerEndpointConstructor() {
    mapper = new ObjectMapper();
    mapper.registerModule(new MyModule());
}

我看到在日志中反序列化异常之前记录了"Set mixin注解".我看不到密信叫!"曾经(尽管我不确定mixin构造函数中的代码是否会被调用).有人可以用我的方式向我显示错误吗?我需要注释java.net.HttpCookie内部的所有字段吗?

I see "Set mixin annotation" is logged prior to the deserialization exception in my logs. I do not see "Mixin called!" ever (though I'm not sure the code inside the mixin constructor would be called). Can someone please show me the error in my ways? Do I need to annotate all the fields inside of java.net.HttpCookie?

http://docs.oracle.com /javase/7/docs/api/java/net/HttpCookie.html

推荐答案

我解决了这个问题,问题是我已将mixin定义为使用它的类内部的非静态内部类.将其和模块移动到自己的程序包中的自己的类定义中可以解决此问题.可以选择将mixin内部类标记为静态"以解决该问题.

I solved this issue, the problem was that I had defined the mixin as a non-static inner class inside of the class in which it was used. Moving it and the module to their own class definitions in their own package fixed this. One could alternatively mark the mixin inner classes as "static" to fix the issue.

信用至 Jackson Mixin不能用于反序列化非默认构造函数对象的工作,其中作者在发布自己的问题时对此进行了评论.

Credit to Jackson Mixin not working for deserializing non-default constructor object wherein the author made a comment on the necessity of this while posting his own question.

这篇关于@JsonCreator和通过模块的mixin无法用于第三党课程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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