'No Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator
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.
Solution 1:[1]
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 2:[2]
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 3:[3]
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 4:[4]
I know this is an old post, but for anyone using Retrofit, this can be useful useful.
If you are using Retrofit + Jackson + Kotlin + Data classes, you need:
- add
implement group: 'com.fasterxml.jackson.module', name: 'jackson-module-kotlin', version: '2.7.1-2'to your dependencies, so that Jackson can de-serialize into Data classes - When building retrofit, pass the Kotlin Jackson Mapper, so that Retrofit uses the correct mapper, ex:
val jsonMapper = com.fasterxml.jackson.module.kotlin.jacksonObjectMapper()
val retrofit = Retrofit.Builder()
...
.addConverterFactory(JacksonConverterFactory.create(jsonMapper))
.build()
Note: If Retrofit is not being used, @Jayson Minard has a more general approach answer.
Solution 5:[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
Solution 6:[6]
I am using Quarkus, Jackson and Lombok. So I solved this issue by adding @Jacksonized attribute on model class.
So all attributes are:
@Jacksonized //missing
@Builder
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ...
Solution 7:[7]
I solved this issue by adding a no argument constractor. If you are using Lombok, you only need to add @NoArgsConstructor annotation:
@AllArgsConstructor
@NoArgsConstructor
@Getter
@ToString
@EqualsAndHashCode
public class User {
private Long userId;
private String shortName;
}
Solution 8:[8]
As the error mentioned the class does not have a default constructor.
Adding @NoArgsConstructor to the entity class should fix it.
Solution 9:[9]
I'm using rescu with Kotlin and resolved it by using @ConstructorProperties
data class MyResponse @ConstructorProperties("message", "count") constructor(
val message: String,
val count: Int
)
Jackson uses @ConstructorProperties. This should fix Lombok @Data as well.
Solution 10:[10]
Just need to add @NoArgsConstructor and it works.
Solution 11:[11]
Extending Yoni Gibbs's answer, if you are in an android project using retrofit and configure serialization with Jackson you can do these things in order to deserialization works as expected with kotlin's data class.
In your build gradle import:
implementation "com.fasterxml.jackson.module:jackson-module-kotlin:2.11.+"
Then, your implementation of retrofit:
val serverURL = "http://localhost:8080/api/v1"
val objectMapper = ObjectMapper()
objectMapper.registerModule(KotlinModule())
//Only if you are using Java 8's Time API too, require jackson-datatype-jsr310
objectMapper.registerModule(JavaTimeModule())
Retrofit.Builder()
.baseUrl(serverURL)
.client(
OkHttpClient.Builder()
.readTimeout(1, TimeUnit.MINUTES)//No mandatory
.connectTimeout(1, TimeUnit.MINUTES)//No mandatory
.addInterceptor(UnauthorizedHandler())//No mandatory
.build())
.addConverterFactory(
JacksonConverterFactory.create(objectMapper)
)
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build()
Data class:
@JsonIgnoreProperties(ignoreUnknown = true)
data class Task(val id: Int,
val name: String,
@JsonSerialize(using = LocalDateTimeSerializer::class)
@JsonDeserialize(using = LocalDateTimeDeserializer::class)
val specificDate: LocalDateTime?,
var completed: Boolean,
val archived: Boolean,
val taskListId: UUID?
Solution 12:[12]
I had this issue and i fixed with the code below.
@Configuration
open class JacksonMapper {
@Bean
open fun mapper(): ObjectMapper {
val mapper = ObjectMapper()
...
mapper.registerModule(KotlinModule())
return mapper
}
}
Solution 13:[13]
Bek's answer is correct.
But in-case someone is trying to use immutable class in restcontroller i.e they are using lombok's @value then you need to add lombok.anyConstructor.addConstructorProperties=true
You can create a file named lombok.config in the same location where the root pom.xml is present and add this line into the file
Solution 14:[14]
Be carefull with Lombok, expecially with @Builder.
what fixed the issue for me was :
@JsonDeserialize(builder = X.XBuilder.class)
class X{
@JsonPOJOBuilder(withPrefix = "")
public static class XBuilder{
}
}
I hope it will make your life easier
Solution 15:[15]
by adding @NoArgsConstructor , it will fix the issue.
Because compiler will add the default constructor if we have not provided any constructor, but if we have added any parameterized constructor and missed to add NoArgsConstructor we will get this exception. We should compulsorily add the default Constructor.
Solution 16:[16]
If you are using LOMBOK. Create a file lombok.config if you don't have one and add this line.
lombok.anyconstructor.addconstructorproperties=true
Solution 17:[17]
Add Constructor in class OR if you are using pojo add @NoArgsConstructor @AllArgsConstructor
Also include JsonProperty -
@JsonProperty("name") private List<Employee> name;
Solution 18:[18]
I got the same error and after I added a constructor without parameters and then the problem was solved.
Solution 19:[19]
For cases where we don't have a default construct, example when working with immutable objects, Jackson by default will not be able to deserialize JSON to the object.
We can resolve this using some annotations like @JsonCreator which can help Jackson to know how to deserialize a given JSON.
A sample code will look like:
package com.test.hello;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
public class Payment {
private final Card card;
private final Amount amount;
@JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
public Payment(@JsonProperty("card") Card card, @JsonProperty("amount") Amount amount) {
this.card = card;
this.amount = amount;
}
public Card getCard() {
return card;
}
public Amount getAmount() {
return amount;
}
}
Solution 20:[20]
If you are using Unirest as the http library, using the GsonObjectMapper instead of the JacksonObjectMapper will also work.
<!-- https://mvnrepository.com/artifact/com.konghq/unirest-object-mappers-gson -->
<dependency>
<groupId>com.konghq</groupId>
<artifactId>unirest-object-mappers-gson</artifactId>
<version>2.3.17</version>
</dependency>
Unirest.config().objectMapper = GsonObjectMapper()
Solution 21:[21]
When you are using Lombok builder you will get the above error.
@JsonDeserialize(builder = StationResponse.StationResponseBuilder.class)
public class StationResponse{
//define required properties
}
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonPOJOBuilder(withPrefix = "")
public static class StationResponseBuilder {}
Reference : https://projectlombok.org/features/Builder With Jackson
Solution 22:[22]
I could resolve this problem in Kotlin with help of @JacksonProperty annotation. Usage example for above case would be:
import com.fasterxml.jackson.annotation.JsonProperty
...
data class Station(
@JacksonProperty("repsol_id") val repsol_id: String,
@JacksonProperty("name") val name: String,
...
Solution 23:[23]
Just want to point out that this answer provides a better explanation.
Basically you can either have @Getter and @NoArgConstructor together
or let Lombok regenerates @ConstructorProperties using lombok.config file,
or compile your java project with -parameters flags,
or let Jackson use Lombok's @Builder
Solution 24:[24]
My cause of issue seems very uncommon to me, not sure if anybody else gets the error under same condition, I found the cause by diffing previous commits, here you go :
Via my build.gradle I was using these 2 compiler options, and commenting out this line fixed the issue
//compileJava.options.compilerArgs = ['-Xlint:unchecked','-Xlint:deprecation']
Solution 25:[25]
Encountered the same error in below Usecase.
I tried to hit the Rest(Put mapping) end point using sprint boot(2.0.0 Snapshot Version) without having default constructor in respective bean.
But with latest Spring Boot versions(2.4.1 Version) the same piece of code is working without error.
so the bean default constructor is no longer needed in latest version of Spring Boot
Solution 26:[26]
I got the same error and the problem was that my model didn't implement Serializable, so check this as well, might help since this is also one of the reason.
Solution 27:[27]
I also faced the exception in Kotlin. If you're still having problem after applying KotlinModule, you might (though not quite probably) have value class somewhere.
Solution 28:[28]
I'm adding my answer, because I myself, due to my inattention, encountered this error.
Accidentally introduced the wrong serializer through the import of a static object and for a long time could not understand what was the reason. Maybe this will help someone else.
// Wrong serializer via static object import
import static org.keycloak.util.JsonSerialization.mapper;
Be careful.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow

