Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to register a Jackson Module in Quarkus?

I'd like to register the Kotlin-Module with Jackson in a Quarkus application so Jackson can deserialize JSON into data classes without needing a NoArgsConstructor. What is the best way to do this?

Update after comment

The application is a REST service written in Kotlin. At the API level we use View classes (e.g. PersonView or PersonWriteView) to abstract from the models and entities on the service resp. persistence layer. To reduce overhead while still getting nice and meaningful equals() and hashcode() functions we use data classes for this.

In the vanilla settings Jackson is not able to directly deserialize into data classes because it needs an empty constructor. This can be monkey patched by applying the no-arg compiler plugin and add an annotation e.g. @NoArgConstructor similar to what Lombok offers. But of course this way we have to annotate each data class in the API which is error prone.

A better solution for this is to include com.fasterxml.jackson.module:jackson-module-kotlin which gets you the marvelous KotlinModule. After this mapper.treeToValue() can directly create data class instances without the need for an empty constructor. For this to work we have to register the module with the ObjectMapper e.g. via ObjectMapper().findAndRegisterModules(). All I want to know is how to configure the ObjectMapper which is used by Resteasy to unmarshal the JSON to objects.

like image 403
Christoph Grimmer-Dietrich Avatar asked Oct 19 '25 03:10

Christoph Grimmer-Dietrich


2 Answers

You can try to add @JsonbCreator annotation to the request model classes. So in this case JSON-B will try to use the constructor to create your objects instead of the no-args / reflection

For example:

data class MyRequest @JsonbCreator constructor(
    @JsonbProperty("propertyOne") val propertyOne: String,
    @JsonbProperty("propertyTwo") val propertyTwo: : String
)
like image 79
Vinicius Costa Avatar answered Oct 22 '25 03:10

Vinicius Costa


According to the documentation, you have to register an implementation of the (quarkus-specific) interface ObjectMapperCustomizer.

import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.KotlinModule
import io.quarkus.jackson.ObjectMapperCustomizer
import javax.inject.Singleton

@Singleton
class AddKotlinModuleCustomizer : ObjectMapperCustomizer {

    override fun customize(objectMapper: ObjectMapper) {
        objectMapper.registerModule(KotlinModule())
    }
}

And of course add the kotlin module as dependency in the pom

    <dependency>
      <groupId>com.fasterxml.jackson.module</groupId>
      <artifactId>jackson-module-kotlin</artifactId>
      <version>2.10.2</version>
    </dependency>
like image 40
kocka Avatar answered Oct 22 '25 01:10

kocka



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!