No Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator

312,622

Solution 1

Reason: This error occurs because jackson library doesn't know how to create your model which doesn't have an empty constructor and the model contains constructor with parameters which didn't annotated its parameters with @JsonProperty("field_name"). By default java compiler creates empty constructor if you didn't add constructor to your class.

Solution: Add an empty constructor to your model or annotate constructor parameters with @JsonProperty("field_name")

If you use a Kotlin data class then also can annotate with @JsonProperty("field_name") or register jackson module kotlin to ObjectMapper.

You can create your models using http://www.jsonschema2pojo.org/.

Solution 2

I got here searching for this error:

No Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator

Nothing to do with Retrofit but if you are using Jackson this error got solved by adding a default constructor to the class throwing the error. More here: https://www.baeldung.com/jackson-exception

Solution 3

You need to use jackson-module-kotlin to deserialize to data classes. See here for details.

The error message above is what Jackson gives you if you try to deserialize some value into a data class when that module isn't enabled or, even if it is, when the ObjectMapper it uses doesn't have the KotlinModule registered. For example, take this code:

data class TestDataClass (val foo: String)

val jsonString = """{ "foo": "bar" }"""
val deserializedValue = ObjectMapper().readerFor(TestDataClass::class.java).readValue<TestDataClass>(jsonString)

This will fail with the following error:

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

If you change the code above and replace ObjectMapper with jacksonObjectMapper (which simply returns a normal ObjectMapper with the KotlinModule registered), it works. i.e.

val deserializedValue = jacksonObjectMapper().readerFor(TestDataClass::class.java).readValue<TestDataClass>(jsonString)

I'm not sure about the Android side of things, but it looks like you'll need to get the system to use the jacksonObjectMapper to do the deserialization.

Solution 4

If you're using Lombok on a POJO model, make sure you have these annotations:

@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor

It could vary, but make sure @Getter and especially @NoArgsConstructor.

Solution 5

I had a similar issue (using Jackson, lombok, gradle) and a POJO without no args constructor - the solution was to add

lombok.anyConstructor.addConstructorProperties=true

to the lombok.config file

Share:
312,622

Related videos on Youtube

José Nobre
Author by

José Nobre

Updated on May 12, 2022

Comments

  • José Nobre
    José Nobre almost 2 years

    I am trying to consume an API using Retrofit and Jackson to deserialize. I am getting the onFailure error No Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator.

    • Jayson Minard
      Jayson Minard over 5 years
      You appear to not be using the Jackson Module for Kotlin, which works with data classes that have no default constructor as yours does not. Check out that module and see if you have any problems after.
    • Noumenon
      Noumenon over 3 years
      Please change accepted answer.
  • Yoni Gibbs
    Yoni Gibbs over 5 years
    Have updated the answer with more info. I wonder if, although you have the module registered, the code which is doing the deserialization isn't explicitly using the jacksongObjectMapper? I'm just assuming it is related to this, because the error message you're getting is exactly what Jackson gives you when that object mapper isn't used.
  • José Nobre
    José Nobre over 5 years
    Can I pass you the repository for you to take a look?
  • Yoni Gibbs
    Yoni Gibbs over 5 years
    Go for it. Though I've never done any Android stuff so don't get your hopes up :-) I just answered this because I happened to come across exactly this problem the other day in some server-side Kotlin code.
  • Jayson Minard
    Jayson Minard over 5 years
    The issue is definitely related to not using this module and using a data class. So, you need to register the module on the object mapper that is being used by the system. Find in your frameworks that you are using where you can modify the configuration of the object mapper to call mapper.registerModule(KotlinModule()) @jose
  • Andres
    Andres almost 5 years
    great! i think retrofit was using other instance of jackson
  • mkki
    mkki over 4 years
    The reason this works is because they aren't data classes.
  • programaths
    programaths over 4 years
    José and Jayson writing about json :-D
  • Kirill
    Kirill over 4 years
    If you want your object to be immutable, you would not want to create a default constructor. Adding @JsonCreator or @ConstructorProperties({"prop1Name", "propr2Name"}) on constructor solves the error above.
  • Vladtn
    Vladtn over 4 years
    If you are using Lobok and @Data, make sure your fields are not final otherwise empty constructor is not possible. So this doesn't work: @Data static class LoginResponse { private final String token, message; private final int status; } But this works: @Data static class LoginResponse { private String token, message; private int status; }
  • d4c0d312
    d4c0d312 over 4 years
    I wish i could give you some rep points because you saved me in the last minute of extra time !! Thanks
  • davide
    davide about 4 years
    @NoArgsConstructor was what solved my situation. My full anotations are @Data @AllArgsConstructor @NoArgsConstructor @Entity @JsonInclude(Include.NON_DEFAULT)
  • 54l3d
    54l3d about 4 years
    @Data is equivalent to @Getter @Setter @RequiredArgsConstructor @ToString @EqualsAndHashCode So may be only @NoArgsConstructor is needed
  • Janac Meena
    Janac Meena about 4 years
    Does this apply to non-kotlin, i.e. Java 8 applications too?
  • Yoni Gibbs
    Yoni Gibbs about 4 years
    @JanacMeena, the solution in this answer is specific to Kotlin, but the error itself can potentially occur when deserializing to Java classes, if they are set up in some particular way (e.g. maybe only having a private constructor, or something like that, I haven't tried so not sure).
  • payne
    payne about 4 years
    @54l3d @Data will not include @RequiredArgsConstructor if you specify another constructor annotation along with it.
  • Pol Ortiz
    Pol Ortiz about 4 years
    Just wanted to point that you saved my day :), I was missing the @NoArgsConstructor annotation all this time.
  • Caio
    Caio almost 4 years
    I am using Spring + Kotlin for Backend, and I resolved this problem with the KotlinModule(), thanks.
  • O. Schnieders
    O. Schnieders over 3 years
    This is the solution. But which way is better? Creating a empy constructor in general or adding the JSON property?
  • Bek
    Bek over 3 years
    @Maximus I think there is no difference in performance( if you mean better in performance). Creating an empty constructor is easier than adding annotation on each parameter.
  • GJ.
    GJ. almost 3 years
    This is the only solution that works for me in a aws lambda + kotlin situation.
  • thomas.schuerger
    thomas.schuerger almost 3 years
    What if I can't modify the class I'm trying to deserialize? Is there any way to make this work? Is a custom deserializer the only option?
  • dpelisek
    dpelisek almost 3 years
    What if I cannot edit the class? For example, I want to deserialize spring's Pair. Can I create a custom deserialization method outside of the Pair class?
  • a9120bb
    a9120bb over 2 years
    @Vladtn -- thanks, that was my problem ;0
  • YazidEF
    YazidEF over 2 years
    I am using Quarkus, with Kotlin, and rest api endpoint body using data class object. Adding the jackson-module-kotlin solved the issue.
  • phancuongviet
    phancuongviet over 2 years
    Thank you, you saved my day when I add all the annotations
  • Captain Man
    Captain Man over 2 years
    Can you please add some brief details from this article? As it is it is technically a link-only answer but judging by the score this has clearly helped people. I don't want to see this get deleted over something so petty.
  • withoutOne
    withoutOne over 2 years
    I was added both the empty constructor and/or jsonproperty annnotation. But didnt work. The interesting think is works with debug variant but dont work with release variant
  • withoutOne
    withoutOne over 2 years
    I have found the error, maybe it will may minifed option or proguard
  • Abhishek Dutt
    Abhishek Dutt about 2 years
    This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
  • Lam Do
    Lam Do about 2 years
    This solution worked. Also totally agree with you @Bek as I had no concern regarding performance.
  • SKO
    SKO about 2 years
    @AbhishekDutt clarified my answer
  • Fatmajk
    Fatmajk about 2 years
    Both @NoArgsConstructor and @AllArgsConstructor were needed in my case. Thanks!