Cannot construct instance of X (no Creators, like default constructor, exist)
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `com.example.Order` (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
What it means
Jackson needs a way to create your class, and this one has no default constructor and no annotations on the one it has. Add @JsonCreator to the constructor and @JsonProperty to each parameter.
Last verified on Jackson 2.21 and 3.1; Spring 6.2 and 7.0.
This JSON is valid. The problem is on the Java side, so a JSON validator would say it is fine.
- Usually
- A constructor with arguments and no annotationsA constructor with one argumentAn abstract classSpring only names the class
- Fix
- Put
@JsonCreatoron the constructor, and@JsonProperty("id")and@JsonProperty("status")on its parameters, as in the fixed code.
The fix
Put @JsonCreator on the constructor, and @JsonProperty("id") and @JsonProperty("status") on its parameters, as in the fixed code. For an abstract class, read into a concrete class instead.
{
"id": 7,
"status": "shipped"
}public class Order {
private final long id;
private final String status;
public Order(long id, String status) {
this.id = id;
this.status = status;
}
public long getId() {
return id;
}
public String getStatus() {
return status;
}
}
Order order = mapper.readValue(json, Order.class);Lines 5 and 6 change.
public class Order {
private final long id;
private final String status;
@JsonCreator
public Order(@JsonProperty("id") long id, @JsonProperty("status") String status) {
this.id = id;
this.status = status;
}
public long getId() {
return id;
}
public String getStatus() {
return status;
}
}
Order order = mapper.readValue(json, Order.class);How the message appears
Jackson 2, then Jackson 3, then Spring (the same on 6.2 and 7.0). Spring only prints Type definition error and the class name. After that, two other versions of the problem: a class with a single-argument constructor, and an abstract class.
- com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `com.example.Order` (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
- at [Source: REDACTED (`StreamReadFeature.INCLUDE_SOURCE_IN_LOCATION` disabled); line: 2, column: 3]
- tools.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `com.example.Order` (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
- at [Source: REDACTED (`StreamReadFeature.INCLUDE_SOURCE_IN_LOCATION` disabled); byte offset: #UNKNOWN]
- org.springframework.http.converter.HttpMessageConversionException: Type definition error: [simple type, class com.example.Order]
- Cannot construct instance of `com.example.Order` (although at least one Creator exists): cannot deserialize from Object value (no delegate- or property-based Creator)
- org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot construct instance of `com.example.Order` (although at least one Creator exists): cannot deserialize from Object value (no delegate- or property-based Creator)
- Cannot construct instance of `com.example.Shape` (no Creators, like default constructor, exist): abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information
What causes it
A constructor with arguments and no annotations
The
Orderhere has one constructor,Order(long id, String status), and no annotations. There is no default constructor to call, and nothing tells Jackson which key goes to which parameter. So it gives up withno Creators, like default constructor, exist. The JSON is fine. (The class was compiled with plainjavac, without the-parametersflag.)A constructor with one argument
With only
Order(long id)the wording changes:although at least one Creator exists, thenno delegate- or property-based Creator. Jackson found the constructor but cannot use it for a JSON object. Spring shows this one asJSON parse error, not as a type definition error.An abstract class
Reading into an abstract
Shapegivesabstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information. Read into a concrete class, or add the type information the message asks for.Spring only names the class
Spring 6.2 and 7.0 print
HttpMessageConversionException: Type definition error: [simple type, class com.example.Order]. The reason is not in that message. To see it, read the same JSON with a plainObjectMapper, like the code on this page. Other Spring messages are on the Spring page.