Jackson does not support @JsonCreator in combination with builders. @JsonCreator basically says: The annotated method constructs a new instance, passing the JSON value as argument to its single parameter. That does not work for builders, because there is no such method that does it both (there are two separate methods, one for receiving a JSON value, and one for constructing the instance).

However, there is a simple workaround in your case without loosing immutability. Just put @JsonCreator on an all-args constructor as follows:

@Value
@AllArgsConstructor(onConstructor_ = {@JsonCreator})
public class Limit {
    @JsonValue
    String value;
}

You can keep the @Builder if you want to, but there is no need to do so.

Answer from Jan Rieke on Stack Overflow
🌐
Project Lombok
projectlombok.org › features › Builder
@Builder
If you do have an explicit constructor, put the @Builder annotation on the constructor instead of on the class. Note that if you put both @Value and @Builder on a class, the package-private constructor that @Builder wants to generate 'wins' and suppresses the constructor that @Value wants to make.
🌐
Grab Another Byte
grabanotherbyte.com › en › jackson › 2020 › 11 › 13 › jackson-builder-pattern.html
Jackson Deserialization and the Builder Pattern - Grab Another Byte
November 13, 2020 - As we can see, this class is immutable and we can only instantiate it through its builder. If we try to deserialize a JSON into our Address class, Jackson will throw an exception saying that it can’t find a creator to construct an instance of our class: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `com.grabanotherbyte.jackson.builder.Address` (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator) at [Source: (String)"{"street":"street","zipCode":"1234","city":"my city","province":"province","country":"country"}"; line: 1, column: 2]
Discussions

[BUG] @Builder problem with classes consisting of one field and ObjectMapper
I noticed that classes consisting ... with @Builder. ... String jsonStr = "{\"id\":1}"; ObjectMapper mapper = new ObjectMapper(); mapper.reader().readValue(jsonStr, MyClass.class); Expected behavior You'll face an exception like this: JSON parse error: Cannot construct instance of ... More on github.com
🌐 github.com
17
August 31, 2021
spring - Builder class does not have build method (name: 'build') - Jackson - Stack Overflow
Try with @Jacksonized annotation, ... = City.Builder.class) and removing @JsonPOJOBuilder annotated statis class. This way Lombok will manage these configurations automagically. ... The problem is that Jackson cannot deserialize the object value. My solution was to add the following annotations to my class: // constructor with no args ... More on stackoverflow.com
🌐 stackoverflow.com
[BUG] Jacksonized fails with Cannot construct instance
Exception in thread "main" com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `demo.LombokJacksonized$Foo` (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator) at [Source: ... More on github.com
🌐 github.com
7
September 2, 2020
java - Cannot construct instance of - Jackson - Stack Overflow
@AllArgsConstructor @NoArgsConstructor ... = ""; @Builder.Default @JsonProperty("id") String id = ""; } ... @JsonDeserialize(as = xxxProjectionImpl.class) public interface xxxProjection { String getkey(); String getValue(); } @Getter class xxxProjectionImpl implements xxxProjection { private String key; private String value; } ... Find the answer to your question by asking. Ask question ... See similar questions with these tags. ... 2 Jackson InvalidDefinitionException: Cannot construct instance because no ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
GitHub
github.com › projectlombok › lombok › issues › 2943
[BUG] @Builder problem with classes consisting of one field and ObjectMapper · Issue #2943 · projectlombok/lombok
August 31, 2021 - @Builder @Data class MyClass{ private Integer id; } String jsonStr = "{\"id\":1}"; ObjectMapper mapper = new ObjectMapper(); mapper.reader().readValue(jsonStr, MyClass.class); Expected behavior You'll face an exception like this: JSON parse error: Cannot construct instance of <the_class>(although at least one Creator exists): cannot deserialize from Object value (no delegate- or property-based Creator); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of <the_class> (although at least one Creator exists): cannot deserialize from Object value (no delegate- or property-based Creator) at [Source: (PushbackInputStream); line: 2, column: 2] Version info (please complete the following information): 1.8.20 ·
Author: projectlombok
🌐
Baeldung
baeldung.com › home › java › using lombok’s @builder annotation
Using Lombok's @Builder Annotation | Baeldung
July 25, 2024 - Learn how the @Builder annotation in Project Lombok can help you reduce boilerplate code when implementing the builder pattern to create instances of your Java classes.
🌐
GitHub
github.com › projectlombok › lombok › issues › 2575
[BUG] Jacksonized fails with Cannot construct instance · Issue #2575 · projectlombok/lombok
September 2, 2020 - [BUG] Jacksonized fails with Cannot construct instance#2575 · Copy link · cameronbraid · opened · on Sep 2, 2020 · Issue body actions · @Jacksonized fails with the following example · package demo; import com.fasterxml.jackson.databind.ObjectMapper; import lombok.Builder; import lombok.experimental.SuperBuilder; import lombok.extern.jackson.Jacksonized; @Builder @Jacksonized public class LombokJacksonized { private String name; private Foo foo; @SuperBuilder @Jacksonized public static class Thing { } @SuperBuilder @Jacksonized public static class Foo extends Thing { } public static void
Author: projectlombok
Find elsewhere
🌐
HowToDoInJava
howtodoinjava.com › home › lombok › lombok serialize and deserialize @builder class
Lombok Serialize and Deserialize @Builder Class
2 weeks ago - By default, Jackson throws the runtime exception InvalidDefinitionException when we deserialize the Article class. Jackson cannot construct the object because the class has no default no-args constructor.
🌐
Reddit
reddit.com › r/javahelp › objectmapper: getting a cannot deserialize from object value error
r/javahelp on Reddit: ObjectMapper: getting a cannot deserialize from Object value error
October 17, 2022 -

So I have code with an object mapper to convert a String of JSON to an object:

And the JSON in question looks like so:

{
   "address": "url",
   "position": { 
        "positionType" : "type" 
   },
   "id": "id",
   "obj": "*"
}

So the object I have for the outer set of parameters is:

public class Example {

private String address;
private Position position;
private String id;
private String id;
private String obj;

//getters and setters for each field
}

(Note that I tried to do without the Position object and was able to convert the rest of the fields no problem on their own)

But with the Position object to cover the innermost fields from the JSON:

public class Position {

private String positionType;

public String getPositionType() {
return positionType;
}

public void setPositionType(String positionType) {
this.positionType = positionType;
}

When I include this and try to convert the whole JSON from earlier, I keep getting an error:

com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `Position` (although at least one Creator exists): cannot deserialize from Object value (no delegate- or property-based Creator)

Any ideas what I might be missing from Position? I can't understand since it worked with the outermost object on its own why the inclusion of the second object isn't working.

Top answer
1 of 3
2
Jackson cannot create a Position object for you. You have a fancy constructor for it you need to annotate the parameters. Easiest to get out is to make sure Position has a default constructor (no params). Jackson will use that constructor plus the setters.
2 of 3
1
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
🌐
Medium
medium.com › consulner › lombok-tricks-and-common-mistakes-fbf0ed044c3c
Lombok tricks and common mistakes | by marcin piczkowski | Marcin Piczkowski | Medium
February 6, 2019 - om.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `lombok.tricks.jackson.ValueObject` (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator) at [Source: (String)"{"login" : "johnsmith", "age": 77}"; line: 1, column: 2]
Top answer
1 of 8
24

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;
    }
}
2 of 8
18

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.

🌐
GitHub
github.com › projectlombok › lombok › issues › 1389
It can not work when @NoArgsConstructor and @Builder and@Data added · Issue #1389 · projectlombok/lombok
May 17, 2017 - Because there may be no setters. And final fields cannot be set even directly. The only way is to switch over to a constructor that uses the builder instance as its only parameter.
🌐
javathinking
javathinking.com › blog › cannot-construct-instance-of-com-domain-user-no-creators-like-default-constructor-exist-cannot-deserialize-from-object-value
How to Fix 'Cannot Construct Instance of User (No Creators)' Error When Deserializing ObjectNode to POJO in Spring Boot — javathinking.com
The "Cannot Construct Instance ... constructor for your POJO. By adding a no-arg constructor, using @JsonCreator annotations, ensuring constructor accessibility, or upgrading Jackson for records, you can resolve this ...
🌐
Medium
medium.com › @contactkumaramit9139 › builder-design-pattern-in-java-b1e33b403437
Builder Design Pattern in Java. Definition of Builder Pattern | by Amit Kumar | Medium
March 27, 2022 - Therefore, neither telescoping constructors nor any other possible combination of constructor parameters is a good approach to solve the issues related to the construction of objects that have numerous optional fields. This is where Builder pattern comes in. First, you create an instance of the Builder class by passing the mandatory fields to its constructor.
🌐
GitHub
github.com › FasterXML › jackson-module-kotlin › issues › 187
MismatchedInputException: Cannot construct instance when deserialising class with inline field · Issue #187 · FasterXML/jackson-module-kotlin
November 4, 2018 - com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of 'Person' (although at least one Creator exists): cannot deserialize from Object value (no delegate- or property-based Creator) at [Source: (String)"{"name":"Mark"}"; line: 1, column: 2] No one assigned ·
Author: FasterXML