Java 即使存在默认构造函数,也无法从对象值(没有基于委托或基于属性的创建者)反序列化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/52708773/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
cannot deserialize from Object value (no delegate- or property-based Creator) even with default constructor present
提问by nishant
I have a class that looks like
我有一个看起来像的课程
class MyClass {
private byte[] payload;
public MyClass(){}
@JsonCreator
public MyClass(@JsonProperty("payload") final byte[] payload) {
this.payload = payload;
}
public byte[] getPayload() {
return this.payload;
}
}
I am using Hymanson so serialize and then to deserialize. Serialization works fine, but during deserialization, I am getting this error message -
我正在使用 Hymanson 进行序列化然后反序列化。序列化工作正常,但在反序列化期间,我收到此错误消息 -
Cannot construct instance of `mypackage.MyClass` (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
I was reading about this problem online, and came across several texts recommending to have a default constructor or a constructor with @JsonCreator
annotation. I tried adding both, but still getting that exception. What am I missing here?
我正在网上阅读有关此问题的信息,并遇到了几篇建议使用默认构造函数或带@JsonCreator
注释的构造函数的文本。我尝试添加两者,但仍然出现异常。我在这里缺少什么?
回答by flavio.donze
EDIT:
编辑:
I just found a much better solution, add the ParanamerModuleto the ObjectMapper
:
我刚刚找到了一个更好的解决方案,将ParanamerModule添加到ObjectMapper
:
mapper.registerModule(new ParanamerModule());
Maven:
马文:
<dependency>
<groupId>com.fasterxml.Hymanson.module</groupId>
<artifactId>Hymanson-module-paranamer</artifactId>
<version>${Hymanson.version}</version>
</dependency>
The advantage against the ParameterNamesModuleseems to be that the classes do not need to be compiled with the -parameters
argument.
与ParameterNamesModule相比的优势似乎是类不需要使用-parameters
参数进行编译。
END EDIT
结束编辑
With Hymanson 2.9.9 I tried to deserialize this simple POJO and came accros the same exception, adding a default constructor solved the problem:
在 Hymanson 2.9.9 中,我尝试反序列化这个简单的 POJO 并出现相同的异常,添加一个默认构造函数解决了这个问题:
POJO:
POJO:
public class Operator {
private String operator;
public Operator(String operator) {
this.operator = operator;
}
public String getOperator() {
return operator;
}
}
ObjectMapper and Serialize/Deserialize:
ObjectMapper 和序列化/反序列化:
ObjectMapper mapper = new ObjectMapper();
mapper.setVisibility(PropertyAccessor.ALL, Visibility.NONE);
mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
mapper.setVisibility(PropertyAccessor.CREATOR, Visibility.ANY);
String value = mapper.writeValueAsString(new Operator("test"));
Operator result = mapper.readValue(value, Operator.class);
JSON:
JSON:
{"operator":"test"}
Exception:
例外:
com.fasterxml.Hymanson.databind.exc.MismatchedInputException:
Cannot construct instance of `...Operator` (although at least one Creator exists): cannot deserialize from Object value (no delegate- or property-based Creator)
at [Source: (String)"{"operator":"test"}"; line: 1, column: 2]
Solution (POJO with default constructor):
解决方案(具有默认构造函数的 POJO):
public class Operator {
private String operator;
private Operator() {
}
public Operator(String operator) {
this();
this.operator = operator;
}
public String getOperator() {
return operator;
}
}
回答by Andreas Presthammer
I observed this same issue. My issue was caused by me using the wrongJsonCreator type. I incorrectly used org.codehaus.Hymanson.annotate.JsonCreator, but should have used com.fasterxml.Hymanson.annotation.JsonCreatorinstead.
我观察到了同样的问题。我的问题是由我使用错误的JsonCreator 类型引起的。我错误地使用了org.codehaus.Hymanson.annotate.JsonCreator,但应该使用com.fasterxml.Hymanson.annotation.JsonCreator代替。