cannot deserialize from Object value (no delegate- or property-based Creator) even with default constructor present
Matthew Harrington
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 Jackson so serialize and then to deserialize. Serialization works fine, but during deserialization, I am getting this error message -
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?
7 Answers
EDIT:
I just found a much better solution, add the ParanamerModule to the ObjectMapper:
mapper.registerModule(new ParanamerModule());Maven:
<dependency> <groupId>com.fasterxml.jackson.module</groupId> <artifactId>jackson-module-paranamer</artifactId> <version>${jackson.version}</version>
</dependency>The advantage against the ParameterNamesModule seems to be that the classes do not need to be compiled with the -parameters argument.
END EDIT
With Jackson 2.9.9 I tried to deserialize this simple POJO and came accros the same exception, adding a default constructor solved the problem:
POJO:
public class Operator { private String operator; public Operator(String operator) { this.operator = operator; } public String getOperator() { return operator; }
}ObjectMapper and Serialize/Deserialize:
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"}Exception:
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]Solution (POJO with default constructor):
public class Operator { private String operator; private Operator() { } public Operator(String operator) { this(); this.operator = operator; } public String getOperator() { return operator; }
} 3 I observed this same issue. My issue was caused by me using the wrong JsonCreator type. I incorrectly used org.codehaus.jackson.annotate.JsonCreator, but should have used com.fasterxml.jackson.annotation.JsonCreator instead.
0I got this error and I tried
Basically added
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)decorators and it worked for me.
In your subclass object add default constructor:
public NameOfClass() { super();
} 1 I came across this serializing and deserializing kotlin data classes. All I had to do was add default values for my properties and then reading the values worked.
data class DataClass( val key: String = "", // adding these default val value: String = "" // values worked
)without the = "", I would get the error:
Cannot construct instance of `com.xxx.DataClass` (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator) I ran into this same issue, and none of the above solutions helped. After discovering debug builds worked fine, R8 was to blame because it was stripping some code needed by Jackson in release builds. Adding @JsonAutoDetect to all the custom class dependencies resolved the bug.
@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
) I was also facing to the same, in my case i was in the statement to consume REST API in Java... but after multiple attempts i was founding that i need to add the default Constructor because i had already declared constructor with the fields..
public NameofClass() { super(); // TODO Auto-generated constructor stub } 2