微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在本地主机中运行 Spring Boot 应用程序时出错

如何解决在本地主机中运行 Spring Boot 应用程序时出错

我是 spring boot 的初学者,我无法在服务器上使用 spring boot 应用程序。我使用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 文件。除了认的 Web 依赖项之外,我还添加了两个依赖项

<?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.java 类

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";
    }
    
}

解决方法

只需尝试从 spring-boot-starter-web 依赖项中删除 version 标记并从 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>

,

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

更正应用程序的类路径,使其包含一个 单一的兼容版本 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>
,

在类路径资源[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注解类,IOC无法注册

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。