引入第三方的工具类,重新启动项目就会奇奇怪怪的报错。加载不到gson类。一开始以为是没有加载到,各种引入,清理问题依然存在。直到仔细看报错信息,里面是spring的类报错,才意识到可能是gson版本冲突了,仔细排查是工具类的版本过低导致的。简单点的办法把工具类里的去掉,但是改动很大,只能想其他办法。

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; at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:658) ~[spring-beans-5.3.21.jar:5.3.21] at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:638) ~[spring-beans-5.3.21.jar:5.3.21] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1352) ~[spring-beans-5.3.21.jar:5.3.21] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1195) ~[spring-beans-5.3.21.jar:5.3.21] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582) ~[spring-beans-5.3.21.jar:5.3.21] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.21.jar:5.3.21] at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.21.jar:5.3.21] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.21.jar:5.3.21] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.21.jar:5.3.21] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.21.jar:5.3.21] at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:955) ~[spring-beans-5.3.21.jar:5.3.21] at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918) ~[spring-context-5.3.21.jar:5.3.21] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.21.jar:5.3.21] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:734) [spring-boot-2.7.1.jar:2.7.1] at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:408) [spring-boot-2.7.1.jar:2.7.1] at org.springframework.boot.SpringApplication.run(SpringApplication.java:308) [spring-boot-2.7.1.jar:2.7.1] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1306) [spring-boot-2.7.1.jar:2.7.1] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1295) [spring-boot-2.7.1.jar:2.7.1] at com.example.aoa.AoaApplication.main(AoaApplication.java:12) [classes/:na] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_144] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_144] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_144] at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_144] at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-2.7.1.jar:2.7.1] Caused by: 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; at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.3.21.jar:5.3.21] at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:653) ~[spring-beans-5.3.21.jar:5.3.21] ... 23 common frames omitted Caused by: java.lang.BootstrapMethodError: java.lang.NoSuchMethodError: com.google.gson.GsonBuilder.setLenient()Lcom/google/gson/GsonBuilder; at org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration$StandardGsonBuilderCustomizer.customize(GsonAutoConfiguration.java:90) ~[spring-boot-autoconfigure-2.7.1.jar:2.7.1] at org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration.lambda$gsonBuilder$0(GsonAutoConfiguration.java:49) ~[spring-boot-autoconfigure-2.7.1.jar:2.7.1] at java.util.ArrayList.forEach(ArrayList.java:1249) ~[na:1.8.0_144] at org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration.gsonBuilder(GsonAutoConfiguration.java:49) ~[spring-boot-autoconfigure-2.7.1.jar:2.7.1] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_144] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_144] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_144] at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_144] at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.3.21.jar:5.3.21] ... 24 common frames omitted Caused by: java.lang.NoSuchMethodError: com.google.gson.GsonBuilder.setLenient()Lcom/google/gson/GsonBuilder; at java.lang.invoke.MethodHandleNatives.resolve(Native Method) ~[na:1.8.0_144] at java.lang.invoke.MemberName$Factory.resolve(MemberName.java:975) ~[na:1.8.0_144] at java.lang.invoke.MemberName$Factory.resolveOrFail(MemberName.java:1000) ~[na:1.8.0_144] at java.lang.invoke.MethodHandles$Lookup.resolveOrFail(MethodHandles.java:1394) ~[na:1.8.0_144] at java.lang.invoke.MethodHandles$Lookup.linkMethodHandleConstant(MethodHandles.java:1750) ~[na:1.8.0_144] at java.lang.invoke.MethodHandleNatives.linkMethodHandleConstant(MethodHandleNatives.java:477) ~[na:1.8.0_144] ... 33 common frames omitted

解决方法是添加注解@EnableAutoConfiguration,告诉SpringBoot程序跳过GSON的自动配置

@EnableAutoConfiguration(exclude = {org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration.class})
@EnableAutoConfiguration(exclude = {org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration.class})
@SpringBootApplication
public class TestApplication {
    public static void main(String[] args) {
        SpringApplication.run(AoaApplication.class, args);

其他类似的问题,也可以这样解决。只需要将每个此类冲突类的名称添加到EnableCautoconfiguration的exclude属性中。

感谢菠萝Y,解决了困扰我半天的问题
参考文章

java.lang.NoSuchMethodError: com.google.gson.GsonBuilder.setLenient()Lcom/google/gson/GsonBuilder 引入第三方的工具类,重新启动项目就会奇奇怪怪的报错,加载不到gson类。Error creating bean with name 'gsonBuilder' defined in class path resource 2018-10-12 11:06:23.345 ERROR 9947 --- [ main] o.s.boot.SpringApplication : Application run failed org.springframework.beans.factory.BeanCreationException: Error creating bea...
最上面是无法嵌入tomcat容器错误,造成原因如标题所示,解决办法加入依赖 <!-- 持久层框架 类似 hibernate mapper 只是 mybatis springboot  mybatis 可以不用 xml 可用 全注解 --> <dependency> <groupId>org.sitemesh&l...
今天做项目,用到了el表达式和jstl,在tomcat6.0上跑得好好的,没想到copy到tomcat5.5后,服务器起不来了,是tomcat版本的问题。查阅了不少资料,现在总结如下: 错误信息: java.lang.NoClassDefFoundError: javax/el/ExpressionFactory 分析:少包,上网查阅后发现解决方法 解决方法:将javaee.jar包再copy一份放在tomcat目录的common\lib下 如上完成后即解决了该异常,大以为大功告成,没想到又出现了新异常 错误信息:java.lang.NoSuchMethodError: javax.
项目中完成短信发送验证码,用到某平台的SDK接口,结果启动模块时报错,抛出 java.lang.NoSuchMethodError: com.google.gson.JsonParser.parseString(Ljava/lang/String;)Lcom/google/gson/sonElement; 异常. 开始感觉也没多大毛病.可能就是用它SDK本身用的某个jar包升级了,已经不用或没有这个方法. 第一想到的就是找下项目中 jar 版本入手. 找到是GosnJa 在项目依赖比较复杂或者 Java 运行的环境有问题时,或者同一类型的 jar 包有不同版本存在,都可能触发该错误。本质上说是 JVM 找不到某个类的特定方法,也就是说 JVM 加载了错误版本的类。说白了,就是 JVM 找不到真正想要调用的方法啦!出现该错误的情形主要有以下两个种: 导入了不匹配的包版本;开发环境和运行环境不一致。 2 解决方法 查看“External Libraries”,看报错的方法到底存不存在,如果不存在,说明这个包一定有问题啦,更新包就可以啦;如果存在,说明包已...
java.lang.nosuchmethoderror: com.google.common.base.preconditions.checkargument
java.lang.NoSuchMethodError: com.google.gson.GsonBuilder.setLenient()Lcom/google/gson/GsonBuilder java.lang.NoSuchMethodError: com.google.gson.GsonBuilder.setLenient()Lcom/google/gson/GsonBuilder d294083434: 经过排查代码发现,@SpringBootApplication注解中已经包含@EnableAutoConfiguration注解,不需要重复定义@EnableAutoConfiguration注解。 所以直接下面这样就可以 (pring-boot.version 2.7.3) @SpringBootApplication(exclude = {org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration.class}) 服务器缺少字体文件导致下载文件乱码解决方案 鬼灭之刃生了锈: 很好,解决了jfreeChar乱码问题,明明添加了 (1.创建主题样式 解决乱码) StandardChartTheme standardChartTheme = new StandardChartTheme("CN");结果还是乱码, (2.升级jfreechart的pom文件依赖版本),3.服务器缺少中文字体,最后定位到这一步才解决乱码问题,真的是要细心慢慢排查才能解决问题。 微信公众号网页调用扫一扫功能,后台生成参数 一个英文名字很难念的人: 确实有点坑,也是找了好久,记录一下 微信公众号网页调用扫一扫功能,后台生成参数 Ricky-chen: 我说前端怎么没看到请求接口 微信公众号网页调用扫一扫功能,后台生成参数 Ricky-chen: 得后端出个接口