You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

从Spring 3.x迁移至Spring Boot 4.0后,无法@Autowired注入ObjectMapper的问题求助

从Spring 3.x迁移至Spring Boot 4.0后,无法@Autowired注入ObjectMapper的问题求助

大家好,我最近把项目从Spring 3.x迁移到Spring Boot 4.0时踩了个坑,项目启动直接抛异常,提示找不到com.fasterxml.jackson.databind.ObjectMapper类型的Bean,具体报错内容是:

required a bean of type 'com.fasterxml.jackson.databind.ObjectMapper' that could not be found

下面是我的相关配置和依赖信息,麻烦各位帮忙排查下问题出在哪:

1. pom.xml 依赖片段

<dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-hibernate5-jakarta</artifactId>
</dependency>

2. 自定义配置类 AppConfig

@Configuration
public class AppConfig {
    @Autowired
    void configureAndStoreObjectMapper(ObjectMapper objectMapper) {
        objectMapper.registerModule(new Hibernate7Module());
        objectMapper.addMixIn(ProblemDetail.class, MixIn.class);
        // 其他ObjectMapper相关配置...
    }
}

3. application.yaml 中的Jackson配置

spring:
  jackson:
    visibility:
      field: any
      getter: none
      setter: none
      is-getter: none

我自己琢磨了几个可能的疑点,但拿不准是不是根因:

  • 依赖用的是jackson-datatype-hibernate5-jakarta,但代码里注册的是Hibernate7Module,会不会是Hibernate模块版本和依赖不匹配?
  • Spring Boot 4.0对Jackson的自动配置是不是有变动,导致默认的ObjectMapper Bean没被创建?
  • 我用@Autowired直接注入的方式是不是过时了,应该换其他方式获取ObjectMapper?

麻烦各位大佬帮忙分析下,谢谢啦!

火山引擎 最新活动