Quarkus REST Jackson object mapper configuration does not seem to work

Viewed 7426

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.

2 Answers

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);
    }
}
Related