Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Node.js, PHP and APIs

Jackson cannot build your class because it has no usable constructor

Your error

InvalidDefinitionException: Cannot construct instance of `com.example.User` (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator)1 more line it can show

Fix it fast

Most common cause, 1 of 3

Lombok @Builder or @Value left only an all-args constructor

Add @Jacksonized next to @Builder so Lombok wires the generated builder into Jackson (Lombok 1.18.14 and later).

Fix
import lombok.Builder;
import lombok.Value;
import lombok.extern.jackson.Jacksonized;

@Value
@Builder
@Jacksonized
public class User {
    String name;
    int age;
}
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

Jackson 2.23 and 3.3 report "Cannot construct instance (no Creators, like default constructor, exist)" when the class has no no-argument constructor and no constructor Jackson knows how to fill. Lombok @Builder or @Value classes, immutable classes and Kotlin data classes hit it most. Add @Jacksonized or a no-args constructor, mark the constructor @JsonCreator, or register the Kotlin module.

  • Applies to Jackson 2.23 and 3.3, Lombok 1.18.48, jackson-module-kotlin 2.23
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

List every constructor the compiled class really has, including the ones Lombok generated. No zero-argument constructor plus a Lombok annotation points to cause 1; a constructor you wrote yourself points to cause 2; a Kotlin class points to cause 3.

Run this first
javap -p target/classes/com/example/User.class

Then find the line you see in this list and open its fix:

  1. 1Lombok @Builder or @Value left only an all-args constructorjavap shows one package-private constructor that takes every field, and the class carries @Builder or @Value
  2. 2Your own constructor takes arguments and nothing maps JSON fields to themjavap lists only constructors with parameters, written by you (or by Lombok @AllArgsConstructor) and with no @JsonCreator on any of them
  3. 3A Kotlin class is read without jackson-module-kotlinThe target is a Kotlin data class with constructor parameters, and the ObjectMapper was created with ObjectMapper() and no Kotlin module

What does the error look like?

  • InvalidDefinitionException: Cannot construct instance of `com.example.User` (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
  • (no Creators, like default constructor, exist): cannot deserialize from Object value

Cause 1 of 3

Lombok @Builder or @Value left only an all-args constructor

You seejavap shows one package-private constructor that takes every field, and the class carries @Builder or @Value

Check

Jackson reports no Creators, like default constructor, exist because @Builder on a class acts like a package-private @AllArgsConstructor. Lombok's docs add that @Builder only does this when no other constructor exists, and that with @Value plus @Builder the builder's constructor wins. Jackson finds nothing it can call.

Check
grep -n "@Builder\|@Value\|@NoArgsConstructor\|@AllArgsConstructor" src/main/java/com/example/User.java

Fix

Add @Jacksonized next to @Builder so Lombok wires the generated builder into Jackson (Lombok 1.18.14 and later). If you would rather keep plain setters, add both @NoArgsConstructor and @AllArgsConstructor, since a no-args constructor alone stops @Builder from generating the one it needs.

Fix
import lombok.Builder;
import lombok.Value;
import lombok.extern.jackson.Jacksonized;

@Value
@Builder
@Jacksonized
public class User {
    String name;
    int age;
}

Verify

Run the test that reads the JSON body into the class. The object is built and its fields hold the JSON values.

Verify
mvn test

Confirmed by projectlombok.org

Did this fix it?

Cause 2 of 3

Your own constructor takes arguments and nothing maps JSON fields to them

You seejavap lists only constructors with parameters, written by you (or by Lombok @AllArgsConstructor) and with no @JsonCreator on any of them

Check

An immutable class with final fields and one constructor gives Jackson no default constructor and no names for the parameters. Jackson's README shows the fix: mark the constructor with @JsonCreator and give every argument a @JsonProperty name. Lombok can produce the same signal through @java.beans.ConstructorProperties, which Jackson treats as a creator by default, but Lombok only adds it when a config key asks for it (the default is false).

Check
grep -rn "addConstructorProperties" lombok.config

Fix

For a hand-written constructor, annotate it. For Lombok-generated constructors, switch on ConstructorProperties in lombok.config at the project root and rebuild.

Fix
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

public final class User {
    private final String name;
    private final int age;

    @JsonCreator
    public User(@JsonProperty("name") String name, @JsonProperty("age") int age) {
        this.name = name;
        this.age = age;
    }
}

# lombok.config (for Lombok-made constructors)
lombok.anyConstructor.addConstructorProperties = true

Verify

javap now shows the annotated constructor (or a ConstructorProperties annotation on the Lombok one), and the same readValue call returns an object.

Verify
javap -v -p target/classes/com/example/User.class | grep -i ConstructorProperties

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 3 of 3

A Kotlin class is read without jackson-module-kotlin

You seeThe target is a Kotlin data class with constructor parameters, and the ObjectMapper was created with ObjectMapper() and no Kotlin module

Check

Kotlin data classes have no default constructor. The jackson-module-kotlin README explains that before the module, Jackson needed one to deserialize a Kotlin object, and that the module lets single-constructor classes and data classes work directly.

Check
./gradlew dependencies --configuration runtimeClasspath | grep jackson-module-kotlin

Fix

Add the module that matches your Jackson version and build the mapper through it.

Fix
// build.gradle.kts
implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.23.+")

// code
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue

val mapper = jacksonObjectMapper()
val user = mapper.readValue<User>(json)

Verify

readValue returns the data class with every property filled from the JSON.

Verify
./gradlew test

Confirmed by raw.githubusercontent.com

Did this fix it?

How do I stop it coming back?

  • Add a round-trip unit test (write to JSON, read back, compare) for every request and response class, so a constructor change fails in CI.
  • Commit a lombok.config at the repo root with the constructor settings your team uses, instead of relying on Lombok defaults.
  • Create ObjectMappers in one shared factory that registers the Kotlin module, rather than calling ObjectMapper() in tests and clients.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine on the same stack: QistLock, Dispatch Pro, Nuclear Home Video.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.