Quarkus REST Jackson 对象映射器配置似乎不起作用

yoa*_*aud 3 quarkus

我已将 jackson 扩展添加到我的 quarkus gradle 项目(0.23.2)中,但是在运行我的应用程序并调用我的 rest 端点时似乎没有应用此配置:

@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;
    }
}
Run Code Online (Sandbox Code Playgroud)

序列化的 json 仍然包括空条目和空数组。这是在 quarkus guides 上的例子。

在启动时,我可以看到 jackson 扩展存在:

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

我错过了什么吗?

从 curl http 请求返回的 jackson 序列化输出示例:

"code":"invalid.request", "message": null, "attributes": null, "errors": [{"code":"data", "message":"must not be blank","attributes":null,"errors":null}]}
Run Code Online (Sandbox Code Playgroud)

如您所见,尽管配置为不输出,但仍在输出消息和属性。

谢谢你的帮助。

小智 9

有一种简单的方法可以做到这一点,请查看https://quarkus.io/guides/rest-json

你的代码应该是这样的:

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