Java Spring启动应用程序启动后失败-尝试调用不存在的方法。尝试从以下位置进行
我是Spring Boot的新手,我正在尝试用REST Api构建一个简单的应用程序,在启动应用程序时,我立即遇到了这个错误:Java Spring启动应用程序启动后失败-尝试调用不存在的方法。尝试从以下位置进行,java,spring,spring-boot,maven,spring-mvc,Java,Spring,Spring Boot,Maven,Spring Mvc,我是Spring Boot的新手,我正在尝试用REST Api构建一个简单的应用程序,在启动应用程序时,我立即遇到了这个错误: Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. 2021-01-24 20:08:19.505 ERROR 11344 --- [ restartedMain] o.s.b.d.Loggin
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-01-24 20:08:19.505 ERROR 11344 --- [ restartedMain] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
An attempt was made to call a method that does not exist. The attempt was made from the following location:
org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$EnableWebMvcConfiguration.requestMappingHandlerAdapter(WebMvcAutoConfiguration.java:369)
The following method did not exist:
'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration.requestMappingHandlerAdapter(org.springframework.web.accept.ContentNegotiationManager, org.springframework.format.support.FormattingConversionService, org.springframework.validation.Validator)'
The method's class, org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration, is available from the following locations:
jar:file:/C:/Users/user/.m2/repository/org/springframework/spring-webmvc/5.1.3.RELEASE/spring-webmvc-5.1.3.RELEASE.jar!/org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration.class
The class hierarchy was loaded from the following locations:
org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration: file:/C:/Users/user/.m2/repository/org/springframework/spring-webmvc/5.1.3.RELEASE/spring-webmvc-5.1.3.RELEASE.jar
org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport: file:/C:/Users/user/.m2/repository/org/springframework/spring-webmvc/5.1.3.RELEASE/spring-webmvc-5.1.3.RELEASE.jar
Action:
Correct the classpath of your application so that it contains a single, compatible version of org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration
Process finished with exit code 0
我还没有完成应用程序的开发,但由于这个错误,我无法继续
到目前为止,我的课程有:
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.4.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>micro1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>micro1</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<!-- <version>1.4.200</version>-->
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
</plugin>
</plugins>
</build>
</project>
服务:
package com.example.micro1.services;
import com.example.micro1.entities.User;
import com.example.micro1.repositories.Repository;
import lombok.Data;
@org.springframework.stereotype.Service
@Data
public class Service {
private final Repository repository;
public User add(User user){
return repository.save(user);
}
}
存储库:
package com.example.micro1.repositories;
import com.example.micro1.entities.User;
import org.springframework.data.jpa.repository.JpaRepository;
@org.springframework.stereotype.Repository
public interface Repository extends JpaRepository<User, Integer> {
@Override
<S extends User> S save(S s);
}
application.properties:
#h2
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.h2.console.enabled=true
应用程序类:
package com.example.micro1;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Micro1Application {
public static void main(String[] args) {
SpringApplication.run(Micro1Application.class, args);
}
}
请告诉我,如果还有什么我应该写在这里。。。
谢谢 starter
spring boot starter web
本身包含这些依赖项
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.3.3</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.3</version>
<scope>compile</scope>
</dependency>
org.springframework
弹簧网
5.3.3
编译
org.springframework
SpringWebMVC
5.3.3
编译
此外,在pom.xml中添加以下内容:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.3.RELEASE</version>
</dependency>
org.springframework
SpringWebMVC
5.1.3.1发布
因此,在启动应用程序时,类路径不明确。我想这是你问题的原因。
Spring引导启动器省去了显式指定declare依赖项的麻烦。
尝试删除spring webmvc的依赖项。我在运行spring Web启动应用程序时遇到了同样的问题。但我收到了以下错误消息:
***************************
应用程序无法启动
***************************
说明:
试图调用不存在的方法。从以下位置进行了尝试:
org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$enablewbmvcconfiguration.configureHandlerExceptionResolver(WebMvcAutoConfiguration.java:519)
以下方法不存在:
org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$EnableWebMvcConfiguration.addDefaultHandlerExceptionResolver(Ljava/util/List;)V
该方法的类org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$EnableWebMvcConfiguration可从以下位置获得:
jar:file:/Users/itzhouq/dev/mavne_repository/org/springframework/boot/springboot autoconfigure/2.1.6.RELEASE/spring-boot-autoconfigure-2.1.6.RELEASE.jar/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class
它是从以下位置加载的:
文件:/Users/itzhouq/dev/mavne_repository/org/springframework/boot/springboot autoconfigure/2.1.6.RELEASE/spring-boot-autoconfigure-2.1.6.RELEASE.jar
行动:
请更正应用程序的类路径,使其包含org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$EnableWebMvcConfiguration的单个兼容版本
这可能是spring boot autoconfigure的异常。因此,我检查了它的依赖关系
org.springframework.boot:spring-boot-web:2.2.1.RELEASE
-->org.springframework.boot:spring-boot-starter:2.2.1.RELEASE
-->
org.springframework.boot:springboot自动配置:2.2.1.RELEASE(因与2.1.6.RELEASE冲突而省略)
。显然,自动配置
由于版本冲突而丢失。为了解决此问题,我添加了以下依赖项:
org.springframework.boot
弹簧靴自动配置
2.2.1.发布
确认此依赖项已添加到模块中,并重新运行应用程序
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.3.3</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.3</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.3.RELEASE</version>
</dependency>