yoann esnaud
yoann esnaud

Reputation: 113

Quarkus REST Jackson object mapper configuration does not seem to work

I have added the jackson extension to my quarkus gradle project (0.23.2), however this configuration does not seem to be applied when running my application and calling my rest endpoint:

@ApplicationScoped
public class ObjectMapperConfiguration {

    @Singleton
    @Produces
    public ObjectMapper objectMapper() {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);

        return objectMapper;
    }
}

The json serialized still includes null entries and empty arrays. This is following the example on the quarkus guides.

on start-up, I can see that the jackson extension is present:

2019-10-08 07:04:00,613 INFO  [io.quarkus] (main) Installed features: [cdi, hibernate-validator, resteasy, resteasy-jackson, smallrye-openapi, swagger-ui]

Have I missed something?

example of the jackson serialized output returned from a curl http request:

"code":"invalid.request", "message": null, "attributes": null, "errors": [{"code":"data", "message":"must not be blank","attributes":null,"errors":null}]}

as you can see, message and attributes are being output despite being configured not to.

Thanks for your help.

Upvotes: 7

Views: 15507

Answers (2)

Guillaume Smet
Guillaume Smet

Reputation: 10529

I'm not entirely sure this is supported right now. But the good news is that we have a brand new JSON customization feature coming in 0.24.0 (which should be released tomorrow).

You can find more information about it here: https://github.com/quarkusio/quarkus/blob/main/docs/src/main/asciidoc/rest-json.adoc#jackson .

It will allow you to customize the ObjectMapper easily.

Upvotes: 0

Diogo Carleto
Diogo Carleto

Reputation: 191

there is an easy way to do that, take a look at https://quarkus.io/guides/rest-json.

Your code should be some like that:

@Singleton
public class RegisterCustomModuleCustomizer implements ObjectMapperCustomizer {
    @Override
    public void customize(ObjectMapper objectMapper) {
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
    }
}

Upvotes: 17

Related Questions