【问题标题】:How to auto configure `MetricsModule` using `JacksonAutoConfiguration` in Spring Boot如何在 Spring Boot 中使用“JacksonAutoConfiguration”自动配置“MetricsModule”
【发布时间】:2016-06-02 23:38:11
【问题描述】:

我正在尝试使用MetricsModule 来解析/metrics 端点的响应正文。由于某种原因,该模块永远不会注册到相应的ObjectMapper

我发现这个 @PostConstruct 方法被删除了:

@PostConstruct
private void More ...registerModulesWithObjectMappers() {
    Collection<Module> modules = getBeans(this.beanFactory, Module.class);
    for (ObjectMapper objectMapper : getBeans(this.beanFactory, ObjectMapper.class)) {
    objectMapper.registerModules(modules);
}

我已经实现了自己的代码来做同样的事情,但我不确定这是否是解决此问题的正确方法。

为什么JacksonAutoConfiguration 不支持这种类型的自动配置?

【问题讨论】:

    标签: java spring spring-boot jackson


    【解决方案1】:

    该方法已在提交 "Don't register Jackson Module beans with all ObjectMappers" 中针对问题 "Make Jackson auto-configuration more consistent and predictable" 删除。简而言之,这样做是为了不弄乱容器中的每个ObjectMapper bean。

    为确保您的Module bean 注册到自动配置的ObjectMapper,请确保Module 的创建不以任何方式依赖ObjectMapper 的实例。例如,不要在创建 Module bean 之前的任何位置自动装配自动配置的 ObjectMapper

    【讨论】:

      猜你喜欢
      • 2015-04-29
      • 2014-10-19
      • 2019-04-22
      • 2019-10-21
      • 2017-05-12
      • 2017-01-19
      • 2018-09-09
      • 2018-09-06
      • 1970-01-01
      相关资源
      最近更新 更多