启动Spring项目失败,日志提示Error creating bean with name ‘gsonBuilder’ defined in class path resource

这里是在提醒创建在类路径资源中定义了名为“gsonBuilder”的bean时出错,通常是gsonBuilder这个依赖包有冲突,比如你pom导入的依赖包下,有子依赖包与Spring的子依赖包重复时,就会抛出这个错误,根据类路径资源导入bean时抛出了这个错误。

笔者完整抛错如下:

2021-04-14 23:33:38.479  WARN 17528 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'gsonBuilder' defined in class path resource [org/springframework/boot/autoconfigure/gson/GsonAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.google.gson.GsonBuilder]: Factory method 'gsonBuilder' threw exception; nested exception is java.lang.BootstrapMethodError: java.lang.NoSuchMethodError: com.google.gson.GsonBuilder.setLenient()Lcom/google/gson/GsonBuilder;Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-04-14 23:33:38.730 ERROR 17528 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : ***************************
APPLICATION FAILED TO START
***************************Description:An attempt was made to call a method that does not exist. The attempt was made from the following location:java.lang.invoke.MethodHandleNatives.resolve(Native Method)The following method did not exist:com.google.gson.GsonBuilder.setLenient()Lcom/google/gson/GsonBuilder;

这时有两种解决办法

一、删除冲突的依赖包

比如笔者是因为com.tencentcloudapi下的子依赖包.code.gson与spring下的.code.gson冲突

<dependency><groupId>com.tencentcloudapi</groupId><artifactId>tencentcloud-sdk-java</artifactId><version>4.0.11</version><exclusions><exclusion><artifactId>gson</artifactId><groupId>com.google.code.gson</groupId></exclusion></exclusions>
</dependency>

如此项目启动时不会再冲突,但存在隐患,如果在使用tencentcloudapi时需要用到gson中的方法时,就会抛错找不到依赖包。

二、替换依赖包

当出现第一中办法所面临的问题时,可以将冲突的依赖包直接在pom中maven方式导入,如此项目既能正常启动又不影响tencentcloudapi的功能。

        <dependency><groupId>com.google.code.gson</groupId><artifactId>gson</artifactId><version>2.8.5</version></dependency>

这两种方法都有它们的局限性,第一个方法容易影响你引入的依赖的功能,而第二种方法要求冲突的包可以通过maven导入。且冲突的包是功能相同的包,例如笔者冲突的gson,如果spring不能兼容使用com.google.code.gson,或tencentcloudapi下导入的是其他com…gson,那么这个办法也不可行。

如果对于以上问题您有和妙招或想法,期待能与您交流学习。

依赖包冲突,Error creating bean相关推荐

  1. springboot加入com.fasterxml.jackson.core-jackson-databind依赖后报错Error creating bean with name ‘requestM

    springboot在pom.xml中加入依赖com.fasterxml.jackson.core-jackson-databind后报错,报错如下:Error creating bean with ...

  2. Error creating bean with name ‘requestMappingHandlerAdapter‘ OR Failed to load ApplicationContext

    Error creating bean with name 'requestMappingHandlerAdapter' OR java.lang.IllegalStateException: Fai ...

  3. BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [

    现场情景: 初次搭建springboot工程,原本要搭建一个springboot+mybatis的maven工程,听说springboot会把一切给配置好,天真的在执行完mybatis自动生成mapp ...

  4. Error creating bean with name ‘redisConnectionFactory‘ defined in class path resource

    1.问题描述 在redis整合springcache的时候一直报错. 按照信息找的发生问题的部分: @Configuration @EnableCaching public class RedisCo ...

  5. Error creating bean with name ‘cn.cyjt.shoot.service.UserServiceTests‘: Unsatisfied dependency expre

    springboot报错: 创建名为""的bean时出错"cn.cyjt.shoot.service.UserServiceTests':通过字段"servic ...

  6. Error creating bean with name ‘fastJsonpResponseBodyAdvice‘ defined in URL xxx

    错误信息:不满足依赖构造参数 Error starting ApplicationContext. To display the conditions report re-run your appli ...

  7. 解决Error creating bean with name ‘redisConnectionFactory‘ defined in class path resource...问题

    文章目录 1. 复现问题 2. 分析问题 3. 解决问题 1. 复现问题 今天在启动spring boot项目时,出现如下错误: org.springframework.beans.factory.B ...

  8. 轻松解决 “Error creating bean with name 'indentDao' defined in file”的错误(一)

      呈上错误的代码信息(核心错误代码),尝尝错误的信息源.你们可以找到具体错在哪里吗? org.springframework.beans.factory.BeanCreationException: ...

  9. 异常记录---Error creating bean with name 'sqlSessionFactory'

    记录几种异常的处理方法: 1. Error creating bean with name 'sqlSessionFactory' defined in class path resource [ap ...

最新文章

  1. linux下使用rdesktop连接远程windows
  2. python decode unicode encode
  3. git项目根据不同需求进行独立开发
  4. cmd命令行启动、停止IIS,打开iis net start w3svc
  5. vue 文件导入服务器,Vue 如何import服务器上的js配置文件
  6. Visual Assist插件使用的一些相关文章
  7. php socket邮箱,phpsocket.io php版本的socket.io
  8. 机器人学导论 二、正运动学,MDH法
  9. AFNetworking 文件断点下载详细解读
  10. 计算机桌面任务栏过宽怎么处理,任务栏变宽了怎么办 还原变宽任务栏的方法【图文教程】...
  11. 微信小程序抓包-夜神模拟器结合BurpSuite抓包(可用于现在最新版本微信)
  12. 联想 缺少计算机所需的介质驱动程序,u盘安装win10显示缺少介质驱动最佳解决方法...
  13. Sentinel流量卫兵
  14. 今天找压缩算法,看到一段陈年往事
  15. c++头文件:stdio.h ,cstdio ,iostream
  16. warning #179-D:variable XXX was declared but never referenced
  17. 史上最全的标题党标题
  18. 二见钟情——设计模式
  19. 交互式系统中采用的调度算法
  20. keystore生成证书实例

热门文章

  1. express4.13.4之microblog学习笔记
  2. mysql除去一段字符串_mysql如何除去一段字符串
  3. 马来西亚驻华大使馆公使衔参赞许达维一行到访清微智能
  4. 使用mpl_finance画股票K线图
  5. 2022-2027年中国工业气体市场规模现状及投资规划建议报告
  6. html如何批量替换图片,教大家word中图片如何进行批量替换
  7. 软件测试术语英文名称
  8. codeforces problem 140E New Year Garland
  9. 彩色图片转手绘线稿的原理简述与Python实现
  10. C# 设计模式----抽象工厂模式