我正在尝试反序列化没有默认无参数构造函数的 java.net.HttpCookie 并得到: org.codehaus.jackson.map.JsonMappingException: No适合类型 [simple type, class java.net. HttpCookie]:无法从 JSON 对象实例化(需要添加/启用类型信息?)在 [Source: java.io.StringReader@5a395674; 行:1,列:35
这是 jackson-mapper-asl v 1.9.13
我发现Jackson 3rd Party Class With No Default Constructor并尝试通过 getDeserializationConfig 和 using 模块使用他们的解决方案。我在下面介绍模块代码。
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");
}
}
在服务器端点的构造函数中,我有以下内容:
public ServerEndpointConstructor() {
mapper = new ObjectMapper();
mapper.registerModule(new MyModule());
}
我看到在我的日志中的反序列化异常之前记录了“设置混合注释”。我没看到“混心叫!” 曾经(尽管我不确定是否会调用 mixin 构造函数中的代码)。有人可以告诉我我的错误吗?我需要注释 java.net.HttpCookie 中的所有字段吗?
http://docs.oracle.com/javase/7/docs/api/java/net/HttpCookie.html