Cannot construct instance of X (no Creators, like default constructor, exist)

ErrorJava

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 @JsonCreator on 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.

The JSON that was sent: request.jsonValid JSON
{
  "id": 7,
  "status": "shipped"
}
A JSON validator says this is fine.
Broken.javaThrows
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);
Throws InvalidDefinitionException

Lines 5 and 6 change.

Fixed.javaWorks
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);
Reads without an error.

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.

Console
  • 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

  1. A constructor with arguments and no annotations

    The Order here 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 with no Creators, like default constructor, exist. The JSON is fine. (The class was compiled with plain javac, without the -parameters flag.)

  2. A constructor with one argument

    With only Order(long id) the wording changes: although at least one Creator exists, then no delegate- or property-based Creator. Jackson found the constructor but cannot use it for a JSON object. Spring shows this one as JSON parse error, not as a type definition error.

  3. An abstract class

    Reading into an abstract Shape gives abstract 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.

  4. 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 plain ObjectMapper, like the code on this page. Other Spring messages are on the Spring page.

Your JSON and text are processed entirely in your browser and are never sent anywhere. To understand how the site is used we record each page view (the page, the referrer and an approximate location from your IP address) and keep it for 90 days. Nothing is stored on your device unless you choose it. Accept to also store a device identifier here and record your browser and device characteristics, which is what lets a return visit be told from a new one. Decline and nothing is sent at all. We respect Do Not Track. See our privacy policy.