Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 565 Vote(s) - 3.48 Average
  • 1
  • 2
  • 3
  • 4
  • 5
No Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator

#1
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`.
Reply

#2
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:

1. 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
2. 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.
Reply

#3
If you are using [Unirest](

[To see links please register here]

) as the http library, using the `GsonObjectMapper` instead of the `JacksonObjectMapper` will also work.

```xml
<!--

[To see links please register here]

-->
<dependency>
<groupId>com.konghq</groupId>
<artifactId>unirest-object-mappers-gson</artifactId>
<version>2.3.17</version>
</dependency>
```

```kotlin
Unirest.config().objectMapper = GsonObjectMapper()
```
Reply

#4
I'm using [rescu][1] 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.


[1]:

[To see links please register here]

Reply

#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
Reply

#6
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 :

[To see links please register here]

With Jackson

Reply

#7
As the error mentioned the class does not have a default constructor.

Adding [@NoArgsConstructor][1] to the entity class should fix it.


[1]:

[To see links please register here]

Reply

#8
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,
...
```
Reply

#9
If you're using **Lombok** on a *POJO model*, make sure you have these annotations:
```java
@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
```
It could vary, but make sure `@Getter` and **especially `@NoArgsConstructor`**.
Reply

#10
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?
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through