🍃 Spring

Spring Boot 404 에러 해결: @GetMapping를 붙여보자

보배 진 2026. 1. 16. 15:08

서버 연결 안해도 됨

Spring Boot 기준 

Spring Boot는 내장 웹 서버(Tomcat) 포함

❌ 그래서 외부 톰캣 설치, 서버 따로 띄울 필요 ❌

 

main( ) 실행하면 바로 서버 기동

그래서 내장 웹 서버 사용으로 톰캣이 없어도 잘 돌아간다

 

 

포트 충돌 이슈

포트 충돌 이슈 발생한다? 그럼 포트 재설정 하면 됨

기본 포트 : 8080

포트 설정 하는 곳

 

 

 

사용자의 요청 = command

localhost:8088/index

localhost:8088/index 는 사용자의 요청 >> command

/index ==  요청 경로 command

Spring MVC에서 이걸 기준으로 HandlerMapping이 동작함

 

 

 

 

index가 있는데 Error Page(404)가 뜬다

index가 있는데 Error Page가 뜨는 이유

prefix/suffix 설정 문제? 때문인가?

 

 

 

 

prefix, suffix는 Controller가 반환하는 viewName에 붙는 설정

이렇게 설정을 하고 서버 재시작을 하면

 

 

 

 

 

 

안나옴

 

 

 


진짜 원인

문제 원인 : 왜냐하면 컨트롤러가 없기 때문이다

지금 내장 웹 서버 사용으로 톰캣이 없어도 잘 돌아간다

근데 왜 404가 뜰까? 내장 되어 있는 프론트 컨트롤러가 command를 분리할 건데

command에 대해 수행할 Controller가 없다(ex: login..)

 

prefix, suffix 이전 단계가 없다

 

 

 

package com.example.demo;
import org.springframework.stereotype.Controller;

@Controller
public class TestController {
	public String test() {
		System.out.println("로그");
		return "index"; // "/WEB-INF/views/ + index + .jsp
		// index : 내가 갈 페이지의 이름
	}
}

테스트용 TestController를 만들었다

여전히 안 됨 : HandlerMapping

 

 

 

 

package com.example.demo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class TestController {
	
	@GetMapping("/index")
	// GET 요청 중에 index가 들어오면 너가 반응해~
	public String test() {
		System.out.println("로그");
		return "index"; // "/WEB-INF/views/ + index + .jsp
		// index : 내가 갈 페이지의 이름
	}
}

@GetMapping("/index")

 


웹 동작 순서

localhost:8088/index를 했는데 

TestController랑 매핑 설정을 안했기 때문이다

@GetMapping("/index") 얘가 handller 매핑 역할을 한다

 

 

내가 TestController를 하나 만든 상태이지만 TestController가 2, 3, 4개 있으면 누가 누구인지 어떻게 아는지?

@GetMapping("/index")   → TestController
@GetMapping("/login")   → LoginController
@GetMapping("/board")   → BoardController

➡ URL 기준으로 구분한다!

 

 

 

 

 

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>4.0.1</version>
      <relativePath/> <!-- lookup parent from repository -->
   </parent>
   <groupId>com.example</groupId>
   <artifactId>demo</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <packaging>war</packaging>
   <name>demo</name>
   <description>Demo project for Spring Boot</description>
   <url/>
   <licenses>
      <license/>
   </licenses>
   <developers>
      <developer/>
   </developers>
   <scm>
      <connection/>
      <developerConnection/>
      <tag/>
      <url/>
   </scm>
   <properties>
      <java.version>17</java.version>
   </properties>
   <dependencies>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-webmvc</artifactId>
      </dependency>

      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-tomcat</artifactId>
         <scope>provided</scope>
      </dependency>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-test</artifactId>
         <scope>test</scope>
      </dependency>
      
      <dependency>
          <groupId>com.mysql</groupId>
          <artifactId>mysql-connector-j</artifactId>
      </dependency>
      
      <dependency>
           <groupId>org.apache.tomcat.embed</groupId>
           <artifactId>tomcat-embed-jasper</artifactId>
       </dependency>
       <dependency>
          <groupId>org.glassfish.web</groupId>
          <artifactId>jakarta.servlet.jsp.jstl</artifactId>
      </dependency>
   </dependencies>

   <build>
      <plugins>
         <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
         </plugin>
      </plugins>
   </build>
</project>
<dependency>
   <groupId>org.apache.tomcat.embed</groupId>
   <artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
  <groupId>org.glassfish.web</groupId>
  <artifactId>jakarta.servlet.jsp.jstl</artifactId>
</dependency>

🔼 pom.xml에 추가해야 됨

spring이 jsp를 읽고 해석할 수 있도록 하는 역할의 코드이다

 

 

 

그래서 여기까지하면 에러가 안보인다

 

 

 

 

 

 

 

 

 

추가))) 컨트롤러 하나에서 여러 페이지를 이동할 수 있게 됨

예를 들어 MEMBER 컨트롤러, BOARD 컨트롤러 이렇게 나눌 수 있다

 

 


 

@Component를 상속 받는 3가지

어노테이션 역할
@Repository DAO (DB 접근 계층)
@Service 비즈니스 로직 계층
@Controller 요청 처리 (Action 역할)

 

 

웹 동작 순서 정리

1. 브라우저 : localhost:8088/index  요청

2. DispatcherServlet (프론트 컨트롤러) : 요청 수신

3. HandlerMapping : "/index"에 매핑된 메서드 탐색

4. TestController.test ( ) 실행

5. return "index"

6. ViewResolver /WEB-INF/vuews/index.jsp

7. JSP 렌더링