运行 spring 本地主机中的引导应用程序出错

Error in running spring boot application in localhost

我是 spring 引导的初学者,我无法在服务器上 spring 引导应用程序。我使用 tomcat 服务器和 IDE 作为 eclipse。我想在浏览器中 运行 我的应用程序。 我的控制台在这里

 :: Spring Boot ::                (v2.4.1)

2021-01-14 09:36:34.144  INFO 14508 --- [           main] com.example.demo.FirstApp1Application    : Starting FirstApp1Application using Java 15.0.1 on DESKTOP-TCH1T7S with PID 14508 (E:\springboot\FirstApp1\target\classes started by Samanthika in E:\springboot\FirstApp1)
2021-01-14 09:36:34.160  INFO 14508 --- [           main] com.example.demo.FirstApp1Application    : No active profile set, falling back to default profiles: default
2021-01-14 09:36:35.384  INFO 14508 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2021-01-14 09:36:35.397  INFO 14508 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2021-01-14 09:36:35.397  INFO 14508 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.41]
2021-01-14 09:36:35.507  INFO 14508 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2021-01-14 09:36:35.508  INFO 14508 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1283 ms
2021-01-14 09:36:35.730  INFO 14508 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2021-01-14 09:36:35.842  WARN 14508 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'resourceHandlerMapping' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.HandlerMapping]: Factory method 'resourceHandlerMapping' threw exception; nested exception is java.lang.NoSuchMethodError: 'org.springframework.web.servlet.config.annotation.ResourceHandlerRegistration org.springframework.web.servlet.config.annotation.ResourceHandlerRegistration.setUseLastModified(boolean)'
2021-01-14 09:36:35.843  INFO 14508 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'applicationTaskExecutor'
2021-01-14 09:36:35.846  INFO 14508 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2021-01-14 09:36:35.864  INFO 14508 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-01-14 09:36:35.879 ERROR 14508 --- [           main] 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$WebMvcAutoConfigurationAdapter.addResourceHandlers(WebMvcAutoConfiguration.java:339)

The following method did not exist:

    'org.springframework.web.servlet.config.annotation.ResourceHandlerRegistration org.springframework.web.servlet.config.annotation.ResourceHandlerRegistration.setUseLastModified(boolean)'
The method's class, org.springframework.web.servlet.config.annotation.ResourceHandlerRegistration, is available from the following locations:

    jar:file:/C:/Users/Samanthika/.m2/repository/org/springframework/spring-webmvc/5.2.12.RELEASE/spring-webmvc-5.2.12.RELEASE.jar!/org/springframework/web/servlet/config/annotation/ResourceHandlerRegistration.class

The class hierarchy was loaded from the following locations:

    org.springframework.web.servlet.config.annotation.ResourceHandlerRegistration: file:/C:/Users/Samanthika/.m2/repository/org/springframework/spring-webmvc/5.2.12.RELEASE/spring-webmvc-5.2.12.RELEASE.jar

Action:

Correct the classpath of your application so that it contains a single, compatible version of org.springframework.web.servlet.config.annotation.ResourceHandlerRegistration

这是我的 pom.xml 文件。 hera 我添加了两个依赖项,除了默认的网络依赖项

<?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.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>FirstApp1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>FirstApp1</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>
        
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.3.7.RELEASE</version>
        </dependency>
        
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.12.RELEASE</version>
        </dependency>       

        
        
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

这里是FirstApp1Application.javaclass

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Component;

@SpringBootApplication
public class FirstApp1Application {

    public static void main(String[] args) {
        SpringApplication.run(FirstApp1Application.class, args);
        System.out.println("Welcome");
    }

}


package com.example.demo;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HomeController {
    
    @RequestMapping(value="/welcome")
    public static String Welcome(){
        return "Welcome to spring boot";
    }
    
}

在 class 路径资源 [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class] 中创建名称为 'resourceHandlerMapping' 的 bean 时出错:通过工厂方法实例化 Bean 失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:无法实例化 [org.springframework.web.servlet.HandlerMapping]:工厂方法 'resourceHandlerMapping' 抛出异常;嵌套异常是 java.lang.NoSuchMethodError: 'org.springframework.web.servlet.config.annotation.ResourceHandlerRegistration org.springframework.web.servlet.config.annotation.ResourceHandlerRegistration.setUseLastModified(boolean)'

根据上述行,你没有用@Component注解class,IOC无法注册

很可能您正在使用不兼容的模块。这也是错误消息所说的:

Correct the classpath of your application so that it contains a single, compatible version of org.springframework.web.servlet.config.annotation.ResourceHandlerRegistration

为了解决这个问题,请转到 https://start.spring.io/ 并根据您的需要生成一个项目。对于“MVC 项目”,您只需要 spring-boot-starter-parentspring-boot-starter-web;你绝对不应该引入 webmvc,这里有一个示例 pom:

<?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.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>15</java.version>
    </properties>

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

只需尝试从 spring-boot-starter-web 依赖项中删除版本标记并从 pom.xml 文件中删除 spring-webmvc 依赖项。

正在从此依赖项中删除版本标记。

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>