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


springboot1.5.Xアップグレード2.Xの時に報告されたエラーこのエラーは、すべての共通のツールクラスをcommons-langのjarパッケージに入れたためです.そして、プロジェクトはこの共通のlangパッケージに依存するだけでいいので、新しいプロジェクトでツールタイプのjarパッケージを繰り返し導入する必要はありません.たとえば、gson、utils、commons-lang 3、xomなどのツール的なJARパッケージがあります.
以下の間違いは、私自身の公共langパッケージに導入されたためです.gson、springboot 2.xの後、彼はデフォルトで自分のgsonを導入します.2つのgsonバージョンの競合によるものです.springboot 1では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()
        at org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration$$EnhancerBySpringCGLIB$$4a2f731d$$FastClassBySpringCGLIB$$d16133e4.invoke()
        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()
        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


ソリューション:起動クラスに@EnableAutoConfiguration(exclude={org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration.clss})注記を追加します.
@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アプリケーション?