SpringBoot BeanCreationException: Error creating bean with name 'requestMappingHandlerAdapter' Caused by: java.lang.NoSuchFieldError: defaultInstance

huangapple go评论82阅读模式
英文:

SpringBoot BeanCreationException: Error creating bean with name 'requestMappingHandlerAdapter' Caused by: java.lang.NoSuchFieldError: defaultInstance

问题

我有一个使用SpringBoot和Maven的项目,其中涉及到了JPA。在尝试运行应用程序时,出现了以下错误堆栈。

> org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerAdapter' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Invocation of init method failed; nested exception is java.lang.NoSuchFieldError: defaultInstance
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1794) ~[spring-beans-5.2.9.RELEASE.jar:5.2.9.RELEASE]
    ...
Caused by: java.lang.NoSuchFieldError: defaultInstance
	at org.springframework.web.servlet.mvc.method.annotation.ServletCookieValueMethodArgumentResolver.<init>(ServletCookieValueMethodArgumentResolver.java:40) ~[spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE]
    ...
Process finished with exit code 1

以下是我的pom.xml文件。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.3.4.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.demo</groupId>
	<artifactId>docker-example</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>docker-example</name>
	<description>Docker Example</description>

	<properties>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<!-- Spring Boot Starters and other dependencies -->
	</dependencies>

	<build>
		<!-- Build configuration -->
	</build>
</project>

你有如何解决这个问题的想法吗?

英文:

I have a SpringBoot + Maven project with JPA. When tring to run the application I got the following error stack.

> org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerAdapter' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Invocation of init method failed; nested exception is java.lang.NoSuchFieldError: defaultInstance
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1794) ~[spring-beans-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:594) ~[spring-beans-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:516) ~[spring-beans-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:324) ~[spring-beans-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322) ~[spring-beans-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:897) ~[spring-beans-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:879) ~[spring-context-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:551) ~[spring-context-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:143) ~[spring-boot-2.3.4.RELEASE.jar:2.3.4.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758) [spring-boot-2.3.4.RELEASE.jar:2.3.4.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750) [spring-boot-2.3.4.RELEASE.jar:2.3.4.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.3.4.RELEASE.jar:2.3.4.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-2.3.4.RELEASE.jar:2.3.4.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237) [spring-boot-2.3.4.RELEASE.jar:2.3.4.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.3.4.RELEASE.jar:2.3.4.RELEASE]
at com.pasindu.dockerexample.DockerExampleApplication.main(DockerExampleApplication.java:14) [classes/:na]
Caused by: java.lang.NoSuchFieldError: defaultInstance
at org.springframework.web.servlet.mvc.method.annotation.ServletCookieValueMethodArgumentResolver.<init>(ServletCookieValueMethodArgumentResolver.java:40) ~[spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.getDefaultArgumentResolvers(RequestMappingHandlerAdapter.java:648) ~[spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.afterPropertiesSet(RequestMappingHandlerAdapter.java:561) ~[spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1853) ~[spring-beans-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1790) ~[spring-beans-5.2.9.RELEASE.jar:5.2.9.RELEASE]
... 17 common frames omitted
Process finished with exit code 1

Below is my pom.xml file.

 &lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
 xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
 &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
 &lt;parent&gt;
 	&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
 	&lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
 	&lt;version&gt;2.3.4.RELEASE&lt;/version&gt;
 	&lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
 &lt;/parent&gt;
 &lt;groupId&gt;com.demo&lt;/groupId&gt;
 &lt;artifactId&gt;docker-example&lt;/artifactId&gt;
 &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
 &lt;name&gt;docker-example&lt;/name&gt;
 &lt;description&gt;Docker Example&lt;/description&gt;

 &lt;properties&gt;
 	&lt;java.version&gt;1.8&lt;/java.version&gt;
 &lt;/properties&gt;

 &lt;dependencies&gt;
 	&lt;dependency&gt;
 		&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
 		&lt;artifactId&gt;spring-boot-starter&lt;/artifactId&gt;
 	&lt;/dependency&gt;
 	&lt;dependency&gt;
 		&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
 		&lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
 	&lt;/dependency&gt;
 	&lt;dependency&gt;
 		&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
 		&lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
 		&lt;scope&gt;test&lt;/scope&gt;
 		&lt;exclusions&gt;
 			&lt;exclusion&gt;
 				&lt;groupId&gt;org.junit.vintage&lt;/groupId&gt;
 				&lt;artifactId&gt;junit-vintage-engine&lt;/artifactId&gt;
 			&lt;/exclusion&gt;
 		&lt;/exclusions&gt;
 	&lt;/dependency&gt;
 	&lt;dependency&gt;
 		&lt;groupId&gt;org.springframework&lt;/groupId&gt;
 		&lt;artifactId&gt;spring-web&lt;/artifactId&gt;
 		&lt;version&gt;5.2.8.RELEASE&lt;/version&gt;
 	&lt;/dependency&gt;
 	&lt;!-- https://mvnrepository.com/artifact/org.projectlombok/lombok --&gt;
 	&lt;dependency&gt;
 		&lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
 		&lt;artifactId&gt;lombok&lt;/artifactId&gt;
 		&lt;version&gt;1.18.12&lt;/version&gt;
 		&lt;scope&gt;provided&lt;/scope&gt;
 	&lt;/dependency&gt;
 	&lt;dependency&gt;
 		&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
 		&lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
 	&lt;/dependency&gt;
 	&lt;dependency&gt;
 		&lt;groupId&gt;mysql&lt;/groupId&gt;
 		&lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
 		&lt;version&gt;8.0.18&lt;/version&gt;
 	&lt;/dependency&gt;
 &lt;/dependencies&gt;

 &lt;build&gt;
 	&lt;finalName&gt;app&lt;/finalName&gt;
 	&lt;plugins&gt;
 		&lt;plugin&gt;
 			&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
 			&lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
 		&lt;/plugin&gt;
 	&lt;/plugins&gt;
 &lt;/build&gt;
&lt;/project&gt;

Any idea of how to fix this ?

答案1

得分: 5

描述的问题与不同Maven依赖项之间的冲突有关。在尝试使用最新版本的Spring Boot与Apache Camel时,我遇到了相同的问题。

在分析依赖树之后,发现spring-webmvc的版本为5.2.9,而所有其他spring依赖项的版本为5.2.8。

在我的情况下,我通过使用更新的camel-spring-boot-dependencies版本(3.6.0,而不是之前使用的3.5.0)来解决了这个问题。

英文:

The problem described is related to conflicts between different Maven dependencies. I've had the same problem while trying to use the latest version of Spring Boot with Apache Camel.

After analyzing the dependency tree, it turned out that the version of spring-webmvc was 5.2.9, while all other spring dependencies were of version 5.2.8.

In my case, I have solved the problem by using a newer version of camel-spring-boot-dependencies (3.6.0, unlike previously used 3.5.0).

答案2

得分: 3

已找到解决方案。

在我的 pom.xml 文件中有两个依赖项,如下所示:

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-web</artifactId>
	<version>5.2.8.RELEASE</version>
</dependency>

我所做的是移除了 spring-web 依赖,然后重新加载所有的 Maven 插件,尝试再次运行项目。

这次运行成功了。

英文:

Found the solution.

In my pom.xml there were two dependencies like

&lt;dependency&gt;
		&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
		&lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
&lt;/dependency&gt;

&lt;dependency&gt;
		&lt;groupId&gt;org.springframework&lt;/groupId&gt;
		&lt;artifactId&gt;spring-web&lt;/artifactId&gt;
		&lt;version&gt;5.2.8.RELEASE&lt;/version&gt;
&lt;/dependency&gt;

What I did was removed the spring-web dependency and reloaded all Maven Plugins and tried running the project again.

It was successful.

huangapple
  • 本文由 发表于 2020年9月22日 17:29:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/64006882.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定