Spring

@RequestMapping 스프링 부트가 예상대로 작동하지 않습니다.

기록만이살길 2021. 3. 18. 15:05
반응형

@RequestMapping 스프링 부트가 예상대로 작동하지 않습니다.

1. 질문(문제점):

"Hello World"를 포트 8080에 인쇄하는 간단한 응용 프로그램을 만들려고합니다.

다음은 내 주요 Java 클래스입니다.

package com.iz.backend;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;

@SpringBootApplication

public class BackendApplication {

@RequestMapping("/")
public String helloWorld() {
    return "Hello World";
}

public static void main(String[] args) {
    SpringApplication.run(BackendApplication.class, args);
}
}

코드가 컴파일되고 앱이 성공적으로 빌드되지만 localhost : 8080으로 이동하면

404 오류가 발생합니다.

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Mon Jan 11 21:18:51
There was an unexpected error (type=Not Found, status=404).

내 pom.xml에 문제가 있습니까? 다음은 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.iz</groupId>
    <artifactId>backend</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>backend</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>11</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-source-plugin</artifactId>
            <version>2.4</version>
        </dependency>

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

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>compile</scope>
        </dependency>

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

    <build>
        <plugins>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>

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

</project>

2. 해결방안:

@Reimeus 답변에 추가,

계속 확인 MyController클래스와 BackendApplication, 기본 패키지의 예에서 클래스를 com.iz.backend.

 |-src/main/java
    |--com.iz.backend 
       |--controllers
          |--MyController
       |--BackendApplication

또는 basePackages를 사용하십시오.

@SpringBootApplication(scanBasePackages = {"com.iz.backend"}) 
65673496
반응형