Spring

localhost에서 스프링 부트 응용 프로그램을 실행하는 동안 오류가 발생했습니다.

기록만이살길 2021. 3. 12. 15:07
반응형

localhost에서 스프링 부트 응용 프로그램을 실행하는 동안 오류가 발생했습니다.

1. 질문(문제점):

나는 스프링 부트의 초보자이고 서버에서 스프링 부트 응용 프로그램을 사용할 수 없습니다. 나는 tomcat 서버와 IDE를 이클립스로 사용했습니다. 브라우저에서 앱을 실행하고 싶습니다. 내 콘솔이 여기 있습니다

 :: 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 파일입니다. 헤라 기본 웹 의존성과 별도로 두 가지 의존성을 추가했습니다.

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

2. 해결방안:

대부분 호환되지 않는 모듈을 사용하고있을 것입니다. 이것은 또한 오류 메시지의 내용입니다.

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>
65713978
반응형