pom文件新引入:
<dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.3</version> </dependency>
但启动后报错:
An attempt was made to call the method com.google.gson.GsonBuilder.setLenient()Lcom/google/gson/GsonBuilder; but it does not exist. Its class, com.google.gson.GsonBuilder, is available from the following locations:
jar:file:/home/haoyun/apache-tomcat-8.5.40/webapps/admin/WEB-INF/lib/gson-2.3.1.jar!/com/google/gson/GsonBuilder.class
It was loaded from the following location:
file:/home/haoyun/apache-tomcat-8.5.40/webapps/admin/WEB-INF/lib/gson-2.3.1.jar
Action:
Correct the classpath of your application so that it contains a single, compatible version of com.google.gson.GsonBuilder
14-Oct-2019 17:27:33.004 SEVERE [localhost-startStop-1] org.apache.catalina.core.ContainerBase.addChildInternal ContainerBase.addChild: start:
org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/admin]]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:167)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:754)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:730)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:734)
at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:980)
at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1851)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
当前引入的gson版本是2.3的,在Maven dependience中查看有2.8.5的版本依赖,可是我全文搜索都不知道项目原来在哪里引入过gson的依赖。
原来父pom中有spring-boot-starter-parent
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
点击上面的spring-boot-starter-parent,查看其源码,发现它又继承了spring-boot-dependencies:
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.0.1.RELEASE</version> <relativePath>../../spring-boot-dependencies</relativePath> </parent>
点击上面的spring-boot-dependencies,查看其源码,搜索gson,发现这里确实已经引入了gson的依赖:2.8.5
解决办法:pom.xml 里引入依赖时,删掉 <version>2.3</version>这一行,Spring Boot 会自动查找匹配最合适的版本。