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
.
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 a constructor with parameters that didn't annotate its parameters with @JsonProperty("field_name")
. By default Java compiler creates an empty constructor if you didn't add a 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/.
jacksonObjectMapper()
fixed the issue –
Xanthe 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
Most relevant bits from the link above:
"This exception is thrown if Jackson can't access the constructor.
When we try to deserialize a JSON String, JsonMappingException: No Suitable Constructor Found
is thrown. To solve this problem, we just add a default constructor:
public Foo() {
super();
}
Now when we deserialize, the process will work just fine."
@JsonCreator
or @ConstructorProperties({"prop1Name", "propr2Name"})
on constructor solves the error above. –
Methadone @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; }
–
Petey 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
.
@NoArgsConstructor
was what solved my situation. My full anotations are @Data @AllArgsConstructor @NoArgsConstructor @Entity @JsonInclude(Include.NON_DEFAULT)
–
Goins @Data
is equivalent to @Getter @Setter @RequiredArgsConstructor @ToString @EqualsAndHashCode
So may be only @NoArgsConstructor
is needed –
Ketchum @Data
will not include @RequiredArgsConstructor
if you specify another constructor annotation along with it. –
Rachellerachis @NoArgsConstructor
and @AllArgsConstructor
were needed in my case. Thanks! –
Peipeiffer @NoArgsConstructor
and @AllArgsConstructor
helped to resolve this. –
Pinhead @Builder
, if @NoArgsConstructer
is also used, then that is when @AllArgsConstructor
must also be used. If no @Builder
, then no @AllArgsConstructor
needed (just @NoArgsConstructor
). –
Junoesque @Jacksonized
instead, is the simplest :) –
Junoesque 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.
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. –
Ratib mapper.registerModule(KotlinModule())
@jose –
Finely 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 ...
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
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.
I'm using Lombok. I got the error because I did not put @NoArgsConstructor
in my model class.
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;
}
If you use lombok
you can use @Jacksonized
annotation.
You don't need setters - this annotation works fine with @Value
.
You don't need @NoArgsConstructor
- you can use @Builder
or just @RequiredArgsConstructor
.
@Builder
, is for just @Jacksonized
additionally (no others, like @Value, are strictly necessary). –
Junoesque @Jacksonized
with @Builder
, @Value
is optional (maybe for other purposes) but not necessary for the Jackson-izing itself. –
Junoesque 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.
As the error mentioned the class does not have a default constructor.
Adding @NoArgsConstructor to the entity class should fix it.
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?
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
https://mcmap.net/q/94303/-using-lombok-in-immutable-request-class
I got the same error and after I added a constructor without parameters and then the problem was solved.
For classes 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;
}
}
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
}
}
KotlinModule()
is now deprecated so use this: val mapper = ObjectMapper().registerModule(KotlinModule.Builder().build())
–
Malayopolynesian 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
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.
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
If you are using LOMBOK. Create a file lombok.config if you don't have one and add this line.
lombok.anyconstructor.addconstructorproperties=true
Add Constructor in class OR if you are using pojo add @NoArgsConstructor @AllArgsConstructor
Also include JsonProperty -
@JsonProperty("name") private List<Employee> name;
This was mentioned already but not fully explained. With Lombok 1.14 and later you can addlombok.anyConstructor.addConstructorProperties=true
to the lombok.config
file located at the root directory of your project. If you have this and your POJO annotated with lombok
@Builder
@Getter
It will work because under the hood Lombok annotates the Builder's constuctor with @java.beans.ConstructorProperties() like here:
By having the @java.beans.ConstructorProperties({param1, param2 ...}) annotation each parameter of your buider's constructor will be exposed via its getter, i.e, getCar(), getBrand() etc. This information is enough for Jackson to serialize your POJO properly via the getters.
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()
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
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,
...
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']
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
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.
Serializable
marker interface is not used by Jackson deserializer. I often see people do not understand the reasons for adding Serializable
marker interface, which is mainly some result of EJB times and some confusion introduced by Hibernate/JPA requirements on embedded keys (where Serializable
is required). –
Padre 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.
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.
In my case the problem occurred only in the build type release and was due to the obscuring of some classes in the '\dto' folder which contains the stubs. To solve I added the following rules in the proguard-rules.pro file:
-keep public class com.your.app_package.dto.** { *; }
-keep @**annotation** class * {*;}
#Preserve JacksonXml (PUBLIC_ONLY crash fix)
-keepattributes *Annotation*,EnclosingMethod,Signature
-keepnames class com.fasterxml.jackson.** { *; }
-dontwarn com.fasterxml.jackson.databind.**
-keep class org.codehaus.** { *; }
-keepclassmembers public final enum com.fasterxml.jackson.annotation.JsonAutoDetect$Visibility {
public static final com.fasterxml.jackson.annotation.JsonAutoDetect$Visibility *; }
If you use Kotlin and value class like this:
value class SomeId(val value: String) {
override fun toString(): String = value
}
Then it won't work, even if you're going to nest this class within other e.g.
data class MyClass(val someId: SomeId)
There's open ticket about this, but it has been opened in 2018, so probably it won't be fixed. Instead you'll have to use primitive type instead so in this case
data class MyClass(val someId: String)
You can use @JsonProperty to resolve the issue in most case just like above answers,but when your object to deserialize is in another library which is imported by maven or gradle repository,you can't change the source code.In this case,you can use annotation like @JsonDeserialize(converter = GeoJsonPointConverter.class)
public class Person {
//GeoJsonPoint is a mongoDB object that provided by springframework
//GeoJsonPoint doesn't have default constructor.
@JsonDeserialize(converter = GeoJsonPointConverter.class)
private GeoJsonPoint point;
// getter & setter & other codes
}
public class GeoJsonPointConverter extends
StdConverter<Map<String,Double>,GeoJsonPoint> {
@Override
public GeoJsonPoint convert(Map<String,Double> value) {
double x = value.get("x");
double y = value.get("y");
return new GeoJsonPoint(x,y);
}
}
Your income json string looks like:
{point:{x:112.551243,y:28.274283}}
I think this is a more flexible way to resolve this issue.
© 2022 - 2025 — McMap. All rights reserved.
@JsonCreator
annotation on this non-default construtor in the class to deserialize. – Padre