본문 바로가기
[study]이론정리/Spring Boot

Spring MVC - 예외처리

by yoon9i 2024. 7. 3.

14> 예외처리

  - Controller에서 예외발생시 예외처리하는 방법임.

  (1) 기본
  @GetMapping("/errorPage")
public String errorPage() {
logger.info("logger:MainController:{}", "/errorPage 요청");
throw new IllegalArgumentException("IllegalArgumentException 발생됨");
}

  => SpringBoot는 자동으로  Whitelabel Error Page를 제공한다.
     이것이 boot의 특징인 auto configuration 이다.

     spring-boot-autoconfigure-2.7.18.jar 담당함.

  (2) 발생된 예외를 auto configuration 처리하지 말고 커스터마이징하자.

  가. 지역 예외 처리
  -  예외가 발생된 특정 Controller 내에서 예외처리
  - @ExceptionHandler
  # Controller 안에서 
  @ExceptionHandler(value = {NullPointerException.class,
                      IllegalArgumentException.class})
public String errorPage(Exception e, Model m) {
logger.info("logger:errorPage:{}", e.getMessage());
m.addAttribute("error_message",  e.getMessage());
return "errorPage";  // errorPage.jsp
}

package com.exam;

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


@SpringBootApplication
public class Application {

	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}
}
package com.exam.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class MainController {
	
	Logger logger = LoggerFactory.getLogger(getClass());
	
	@GetMapping("/errorPage")
	public String errorExam() {
		logger.info("logger:MainController:{}", "/errorPage 요청");	
		
		throw new IllegalArgumentException("IllegalArgumentException 발생됨");
	}
	
	@GetMapping("/nullPoint")
	public String nullPoint() {
		logger.info("logger:MainController:{}", "/nullPoint 요청");	
		
		throw new NullPointerException("NullPointerException 발생됨");
	}
	@GetMapping("/cast")
	public String cast() {
		logger.info("logger:MainController:{}", "/cast 요청");	
		
		throw new ClassCastException("ClassCastException 발생됨");
	}
	
	
	
	@ExceptionHandler(value = {NullPointerException.class,
			                   IllegalArgumentException.class})
	public String errorPage(Exception e, Model m) {
		logger.info("logger:errorPage:{}", e.getMessage());
		m.addAttribute("error_message",  e.getMessage());
		return "errorPage";  // errorPage.jsp
	}
}
package com.exam.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class MainController2 {
	
	Logger logger = LoggerFactory.getLogger(getClass());
	
	@GetMapping("/errorPage2")
	public String errorPage() {
		logger.info("logger:MainController:{}", "/errorPage 요청");	
		
		throw new IllegalArgumentException("IllegalArgumentException 발생됨");
	}
	
	@GetMapping("/arith")
	public String arith() {
		logger.info("logger:MainController2:{}", "/arith 요청");	
		
		throw new ArithmeticException("ArithmeticException 발생됨");
	}
}




나. 글로벌 예외처리
  - 모든 Controller 예외처리
  - @ControlAdvice 
    # 새로운 빈에서
    @ControllerAdvice
  public class GlobalExceptionHandler {
Logger logger = LoggerFactory.getLogger(getClass());

    @ExceptionHandler(value = {ClassCastException.class,
                          ArithmeticException.class})
    public String errorPage(Exception e, Model m) {
      logger.info("logger:errorPage:{}", e.getMessage());
      m.addAttribute("error_message",  e.getMessage());
      return "errorPage";  // errorPage.jsp
    }
  }

package com.exam.exception;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice
public class GlobalExceptionHandler {
	Logger logger = LoggerFactory.getLogger(getClass());
	
	@ExceptionHandler(value = {ClassCastException.class,
			                  ArithmeticException.class})
	public String errorPage(Exception e, Model m) {
		logger.info("logger:errorPage:{}", e.getMessage());
		m.addAttribute("error_message",  e.getMessage());
		return "errorPage";  // errorPage.jsp
	}
}

 

 

<%@ page 
         contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"        
%>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>errorPage.jsp</h2>
${error_message}
</body>
</html>
# application.properties
logging.level.org.springframework=info

# tomcat port 번호 변경
server.port=8090

# context 명 변경
server.servlet.context-path=/app

# jsp의 경로와 확장자 지정
spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp
<?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 
	http://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.7.18</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.exam</groupId>
	<artifactId>demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>demo</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-web</artifactId>
		</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools -->
		<dependency>
		    <groupId>org.springframework.boot</groupId>
		    <artifactId>spring-boot-devtools</artifactId>
		</dependency>
	<dependency>
		<groupId>org.apache.tomcat.embed</groupId>
		<artifactId>tomcat-embed-jasper</artifactId>
		<scope>provided</scope>
	</dependency> 
	<dependency>
		<groupId>javax.servlet</groupId>
		<artifactId>jstl</artifactId>
	</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

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

</project>