40

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?

flavio.donze
  • 7,432
  • 9
  • 58
  • 91
krackoder
  • 2,841
  • 7
  • 42
  • 51
  • 2
    I tested [like this](https://github.com/haba713/jackson-databind-test/blob/master/src/main/java/Main.java) with Jackson 2.9.7 and also deserialization worked fine. – haba713 Oct 08 '18 at 20:55
  • You also need to have setters for every field. – Om Gupta Aug 28 '23 at 13:26

7 Answers7

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;
    }
}
flavio.donze
  • 7,432
  • 9
  • 58
  • 91
15

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.

Andreas Presthammer
  • 1,886
  • 2
  • 19
  • 31
12

I got this error and I tried https://newbedev.com/no-creators-like-default-construct-exist-cannot-deserialize-from-object-value-no-delegate-or-property-based-creator

Basically added

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)

decorators and it worked for me.

coda
  • 2,188
  • 2
  • 22
  • 26
8

In your subclass object add default constructor:

public NameOfClass() {
    super();
}
  • 3
    `@Data` (which adds `@RequiredArgsConstructor`) indeed wasn't sufficient. I had to make the property non-`final` and add a `@NoArgsConstructor` – lilalinux Jun 03 '21 at 14:38
4

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)
orpheus
  • 1,060
  • 2
  • 15
  • 23
0

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
)
clever_trevor
  • 1,530
  • 2
  • 22
  • 42
0

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
    }
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-ask). – Community Sep 21 '21 at 11:42
  • please improvise your comments by giving code snippets clear – Dilip D Sep 21 '21 at 12:25