'gsonBuilder' defined in class path resource [org/springframework/boot/autoconfigure/gson/GsonAutoC

20 篇文章 0 订阅
1 篇文章 0 订阅

springboot1.5.X升级2.X 的时候报的错误
这个错误是由于我们把所有公共的工具类都放到了 一个 commons-lang的jar包里面,然后项目只需要依赖这个公共的lang包就可以,不需要在新项目种重复引入一些工具类型的 jar包。例如:gson,utils,commons-lang3,xom等一些工具性质的JAR包。

以下错误就是因为,我自己公共lang包里面引入了,gson,而 springboot2.x之后,他会默认引入一个自己的gson.导致两个gson版本冲突导致的。 而在springboot1.X里面他们是不会默认引入gson包的。

有人会说,那你把自己lang包里面的gson删除了就好了。 但是因为我们这是一个工具类型的模块包,改动或者删除影响太大,有很多项目都在用 相关的JAR包, 所以只能用其他方案。

错误日志:

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.instantiateUsingFactoryMethod(ConstructorResolver.java:587)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1250)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1099)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:541)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:501)
        at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:760)
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:869)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550)
        at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140)
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759)
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:395)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:327)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1255)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1243)
        at com.caiyouxi.Application.main(Application.java:34)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48)
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:50)
        at org.springframework.boot.loader.WarLauncher.main(WarLauncher.java:58)
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)
        at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:579)
        ... 26 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:96)
        at org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration.lambda$gsonBuilder$0(GsonAutoConfiguration.java:49)
        at java.util.ArrayList.forEach(ArrayList.java:1255)
        at org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration.gsonBuilder(GsonAutoConfiguration.java:49)
        at org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration$$EnhancerBySpringCGLIB$$4a2f731d.CGLIB$gsonBuilder$0(<generated>)
        at org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration$$EnhancerBySpringCGLIB$$4a2f731d$$FastClassBySpringCGLIB$$d16133e4.invoke(<generated>)
        at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
        at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:361)
        at org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration$$EnhancerBySpringCGLIB$$4a2f731d.gsonBuilder(<generated>)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
        ... 27 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)
        at java.lang.invoke.MemberName$Factory.resolve(MemberName.java:975)
        at java.lang.invoke.MemberName$Factory.resolveOrFail(MemberName.java:1000)
        at java.lang.invoke.MethodHandles$Lookup.resolveOrFail(MethodHandles.java:1394)
        at java.lang.invoke.MethodHandles$Lookup.linkMethodHandleConstant(MethodHandles.java:1750)
        at java.lang.invoke.MethodHandleNatives.linkMethodHandleConstant(MethodHandleNatives.java:477)
        ... 41 common frames omitted

解决方案:
启动类(Application)中添加
@EnableAutoConfiguration(exclude = {org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration.class})
注解。

@EnableAutoConfiguration(exclude = {org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration.class})
@SpringBootApplication
//开启事务
@EnableTransactionManagement
@EnableScheduling
public class Application extends SpringBootServletInitializer {
    private static Logger logger = Logger.getLogger(Application.class);

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }

    /**
     * Start
     */
    public static void main(String[] args) {
    	logger.info("service Server Start ---");
        SpringApplication.run(Application.class, args);
        logger.info("service Server Start Success---");
    }

}

这个问题是由于GSON库的版本与项目中已经包含的现有依赖项与SpringBoot的默认依赖项的版本不匹配而产生的。
它基本上告诉SpringBoot应用程序跳过GSON的自动配置。
这个解决方案也适用于任何其他可能产生相同问题的类。您只需要将每个此类冲突类的名称添加到EnableCautoconfiguration的exclude属性中。

I faced the same problem and had to waste a lot of time trying to fix this.

The problem arises due to the version mismatch of the Gson library from existing dependencies already included in your project with that of Spring Boot’s default one.

The easiest fix of this problem (that worked for me) is to replace each occurrence of the

@EnableAutoConfiguration

tag with

@EnableAutoConfiguration(exclude = {org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration.class})

which basically tells the Spring boot application to skip auto configuration for Gson.

This solution also applies to any other class that might create the same problem. You just need to add the name of each such conflicting class to the exclude attribute of EnableAutoConfiguration.

原答案路径:
Why do I get Gson builder error when starting a Spring Boot application?

  • 7
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
java.lang.NoClassDefFoundError是Java中的一个异常,表示在行时找不到某个类的定义。在这个特定的情况下,出现了NoClassDefFoundError com/google/gson/GsonBuilder异常,意味着在运行时找不到com.google.gson.GsonBuilder类的定义。 com.google.gson.GsonBuilder是Google Gson库中的一个类,用于构建Gson对象。Gson是一个Java库,用于将Java对象转换为JSON格式的字符串,以及将JSON字符串转换为Java对象。 要解决这个异常,你需要确保在你的项目中包含了Gson库的正确版本,并且该类的定义可以被正确加载。以下是一些可能的解决方法: 1. 确保你已经正确地导入了Gson库。你可以通过在项目的构建路径中添加Gson库的JAR文件或者使用构建工具(如Maven或Gradle)来添加依赖项来实现。 2. 检查你的代码中是否正确地引用了com.google.gson.GsonBuilder类。确保你使用了正确的包名和类名,并且没有拼写错误。 3. 如果你使用的是IDE(如Eclipse或IntelliJ IDEA),尝试重新构建项目并清除编译缓存。有时候编译错误或缓存问题可能导致类无法正确加载。 4. 如果你使用的是Web应用程序,确保将Gson库的JAR文件放置在正确的位置(如WEB-INF/lib目录下)。 如果以上方法都没有解决问题,可能是由于其他原因导致类无法加载。在这种情况下,你可以尝试查看详细的错误日志或调试信息,以便更好地理解问题所在。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值