我有一堂课看起来像
class MyClass {
private byte[] payload;
public MyClass(){}
@JsonCreator
public MyClass(@JsonProperty("payload") final byte[] payload) {
this.payload = payload;
}
public byte[] getPayload() {
return this.payload;
}
}
我使用 Jackson 进行序列化,然后进行反序列化。序列化工作正常,但在反序列化期间,我收到此错误消息 -
Cannot construct instance of `mypackage.MyClass` (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
我在网上阅读有关此问题的信息,并遇到了一些建议使用默认构造函数或带有
@JsonCreator
注释的构造函数的文本。我尝试添加两者,但仍然遇到该异常。我在这里缺少什么?
编辑:
我刚刚找到了一个更好的解决方案,将 ParanamerModule 添加到
ObjectMapper
:
mapper.registerModule(new ParanamerModule());
马夫:
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-paranamer</artifactId>
<version>${jackson.version}</version>
</dependency>
相对于 ParameterNamesModule 的优势似乎是这些类不需要使用
-parameters
参数进行编译。
结束编辑
在 Jackson 2.9.9 中,我尝试反序列化这个简单的 POJO 并遇到相同的异常,添加默认构造函数解决了问题:
POJO:
public class Operator {
private String operator;
public Operator(String operator) {
this.operator = operator;
}
public String getOperator() {
return operator;
}
}
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:
{"operator":"test"}
例外:
com.fasterxml.jackson.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]
解决方案(带有默认构造函数的POJO):
public class Operator {
private String operator;
private Operator() {
}
public Operator(String operator) {
this();
this.operator = operator;
}
public String getOperator() {
return operator;
}
}
我也观察到了同样的问题。我的问题是由我使用错误 JsonCreator 类型引起的。我错误地使用了 org.codehaus.jackson.annotate.JsonCreator,但应该使用 com.fasterxml.jackson.annotation.JsonCreator 来代替。
基本已添加
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
装饰器,它对我有用。
在你的子类对象中添加默认构造函数:
public NameOfClass() {
super();
}
我遇到了这个序列化和反序列化
kotlin data classes
。我所要做的就是为我的属性添加默认值,然后读取有效的值。
data class DataClass(
val key: String = "", // adding these default
val value: String = "" // values worked
)
如果没有
= ""
,我会收到错误:
Cannot construct instance of `com.xxx.DataClass` (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
我遇到了同样的问题,上述解决方案都没有帮助。在发现调试版本运行良好后,R8 受到了指责,因为它在发布版本中删除了 Jackson 所需的一些代码。将
@JsonAutoDetect
添加到所有自定义类依赖项解决了该错误。
@JsonAutoDetect
data class Car(
val name: String // annotation not needed
val engine: Engine // annotation needed
)
// @JsonAutoDetect is needed here
data class Engine(
val name: String
)
我也面临同样的问题,在我的例子中,我在声明中使用Java中的REST API...但经过多次尝试,我发现我需要添加默认构造函数,因为我已经使用字段声明了构造函数..
public NameofClass() {
super();
// TODO Auto-generated constructor stub
}
Kotlin 中的情况与我使用
MockMvc
进行测试时相同,我必须返回到 RequestBody 和 Response 的数据类以获得默认值,并模仿默认构造函数。
data class IntBodyPayload(val key: String = ""): Serializable
data class IntBodyResponse(val value: Int = 0): Serializable