Spring MVC Exception Handling은 서버 예외를 클라이언트에게 보내지 않도록하는 데 매우 중요합니다. 오늘은 @ExceptionHandler, @ControllerAdvice 및 HandlerExceptionResolver를 사용한 Spring Exception Handling을 살펴볼 것입니다. 모든 웹 응용 프로그램은 우리의 응용 프로그램에서 처리되지 않은 예외가 발생할 때 컨테이너에서 생성된 페이지를 제공하고 싶지 않기 때문에 예외 처리를 위한 좋은 설계가 필요합니다.
Spring Exception Handling
잘 정의된 예외 처리 접근 방식을 가지고 있는 것은 어떤 웹 응용 프로그램 프레임워크에 있어서 큰 장점입니다. 그럼에도 불구하고 Spring MVC 프레임워크는 웹 응용 프로그램에서 예외와 오류 처리에 대해 잘 처리합니다. Spring MVC 프레임워크는 강력한 예외 처리를 달성하는 데 도움이 되는 다음과 같은 방법을 제공합니다.
- Controller Based – 우리는 컨트롤러 클래스에 예외 처리기 메서드를 정의할 수 있습니다. 우리가 해야 할 일은 이러한 메서드에
@ExceptionHandler
주석을 붙이는 것뿐입니다. 이 주석은 예외 클래스를 인수로 취합니다. 따라서 우리가 Exception 클래스를 위해 하나를 정의했다면, 요청 핸들러 메서드에서 던져지는 모든 예외가 처리될 것입니다. 이 예외 처리기 메서드는 다른 요청 핸들러 메서드와 마찬가지로 작동하며 오류 응답을 빌드하고 다른 오류 페이지로 응답할 수 있습니다. 나중에 예제에서 살펴볼 JSON 오류 응답을 보낼 수도 있습니다. 여러 예외 처리기 메서드가 정의된 경우 Exception 클래스에 가장 가까운 처리기 메서드가 사용됩니다. 예를 들어, IOException과 Exception에 대해 두 개의 처리기 메서드가 정의되어 있고 요청 핸들러 메서드가 IOException을 throw한다면, IOException에 대한 처리기 메서드가 실행됩니다. - Global Exception Handler – 예외 처리는 관점을 가로지르는 관심사이며, 응용 프로그램의 모든 포인트 컷에 대해 수행되어야 합니다. 우리는 이미 Spring AOP를 살펴보았기 때문에 Spring은 어떤 클래스에도 사용할 수 있는
@ControllerAdvice
주석을 제공합니다. 전역 예외 처리기를 정의하기 위해 사용할 수 있습니다. 전역 컨트롤러 어드바이스의 처리기 메서드는 컨트롤러 기반 예외 처리기 메서드와 동일하며, 컨트롤러 클래스가 예외를 처리할 수 없을 때 사용됩니다. - HandlerExceptionResolver – 일반적인 예외에 대해 대부분 정적 페이지를 제공합니다. Spring Framework는 전역 예외 처리기를 만들기 위해 구현할 수 있는
HandlerExceptionResolver
인터페이스를 제공합니다. 전역 예외 처리기를 추가로 정의하는 이유는 Spring 프레임워크가 또한 우리가 spring 빈 구성 파일에 정의할 수 있는 기본 구현 클래스를 제공하기 때문입니다.SimpleMappingExceptionResolver
는 기본 구현 클래스이며, 여기서 특정 예외에 대해 어떤 리소스를 사용할지를 지정할 수 있는 exceptionMappings를 구성할 수 있습니다. 또한 예외 메시지의 로깅과 같이 응용 프로그램별 변경 사항을 가진 우리만의 전역 핸들러를 만들기 위해 이를 재정의할 수도 있습니다.
Spring MVC 프로젝트를 생성하여 Controller 기반, AOP 기반 및 Exception Resolver 기반 예외 및 오류 처리 접근 방법을 자세히 살펴보겠습니다. 또한 JSON 응답을 반환하는 예외 처리기 메서드를 작성할 것입니다. Spring에서 JSON이 처음이라면 Spring Restful JSON Tutorial을 읽어보세요. 최종 프로젝트는 아래 이미지처럼 보일 것이며, 응용 프로그램의 모든 구성 요소를 하나씩 살펴보겠습니다.
Spring 예외 처리 Maven 종속성
표준 Spring MVC 종속성 외에도 JSON 지원을 위해 Jackson JSON 종속성이 필요합니다. 최종 pom.xml 파일은 아래와 같습니다.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.journaldev.spring</groupId>
<artifactId>SpringExceptionHandling</artifactId>
<name>SpringExceptionHandling</name>
<packaging>war</packaging>
<version>1.0.0-BUILD-SNAPSHOT</version>
<properties>
<java-version>1.6</java-version>
<org.springframework-version>4.0.2.RELEASE</org.springframework-version>
<org.aspectj-version>1.7.4</org.aspectj-version>
<org.slf4j-version>1.7.5</org.slf4j-version>
<jackson.databind-version>2.2.3</jackson.databind-version>
</properties>
<dependencies>
<!-- Jackson -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.databind-version}</version>
</dependency>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework-version}</version>
<exclusions>
<!-- Exclude Commons Logging in favor of SLF4j -->
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<!-- AspectJ -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${org.aspectj-version}</version>
</dependency>
<!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${org.slf4j-version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${org.slf4j-version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${org.slf4j-version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.15</version>
<exclusions>
<exclusion>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
</exclusion>
<exclusion>
<groupId>javax.jms</groupId>
<artifactId>jms</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jdmk</groupId>
<artifactId>jmxtools</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jmx</groupId>
<artifactId>jmxri</artifactId>
</exclusion>
</exclusions>
<scope>runtime</scope>
</dependency>
<!-- @Inject -->
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
<!-- Servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- Test -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
<configuration>
<additionalProjectnatures>
<projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
</additionalProjectnatures>
<additionalBuildcommands>
<buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>
</additionalBuildcommands>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<compilerArgument>-Xlint:all</compilerArgument>
<showWarnings>true</showWarnings>
<showDeprecation>true</showDeprecation>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<mainClass>org.test.int1.Main</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
I have updated Spring Framework, AspectJ, Jackson and slf4j versions to use the latest one.
Spring MVC 예외 처리 배포 서술자
우리의 web.xml 파일은 아래와 같습니다.
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="https://java.sun.com/xml/ns/javaee"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://java.sun.com/xml/ns/javaee https://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/spring.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<error-page>
<error-code>404</error-code>
<location>/resources/404.jsp</location>
</error-page>
</web-app>
웹 애플리케이션에 Spring Framework를 플러그인하는 대부분의 부분은 404 오류에 대해 정의된 error-page를 제외하고 있습니다. 따라서 애플리케이션이 404 오류를 발생시킬 때 이 페이지가 응답으로 사용됩니다. 이 구성은 우리의 스프링 웹 애플리케이션이 404 오류 코드를 throw할 때 컨테이너에 의해 사용됩니다.
Spring 예외 처리 – 모델 클래스
I have defined Employee bean as model class, however we will be using it in our application just to return valid response in specific scenario. We will be deliberately throwing different types of exceptions in most of the cases.
package com.journaldev.spring.model;
public class Employee {
private String name;
private int id;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
Since we will be returning JSON response too, let’s create a java bean with exception details that will be sent as response.
package com.journaldev.spring.model;
public class ExceptionJSONInfo {
private String url;
private String message;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
Spring 예외 처리 – 사용자 지정 예외 클래스
사용할 사용자 지정 예외 클래스를 만들어 봅시다.
package com.journaldev.spring.exceptions;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
@ResponseStatus(value=HttpStatus.NOT_FOUND, reason="Employee Not Found") //404
public class EmployeeNotFoundException extends Exception {
private static final long serialVersionUID = -3332292346834265371L;
public EmployeeNotFoundException(int id){
super("EmployeeNotFoundException with id="+id);
}
}
@ResponseStatus
주석을 예외 클래스에 사용할 수 있으며, 이를 통해 응용 프로그램이 해당 유형의 예외를 throw하고 응용 프로그램이 예외 처리 구현에 의해 처리될 때 응용 프로그램에서 전송될 HTTP 코드를 정의할 수 있습니다. 제가 HTTP 상태를 404로 설정하고 이에 대한 오류 페이지가 정의되어 있음을 주목하세요. 따라서 응용 프로그램은 이러한 유형의 예외에 대해 어떤 보기도 반환하지 않는 경우 오류 페이지를 사용해야 합니다. 우리는 또한 예외 처리기 메서드에서 상태 코드를 재정의할 수 있으며, 이는 예외 처리기 메서드가 응답으로 어떤 보기 페이지도 반환하지 않을 때의 기본 HTTP 상태 코드로 생각할 수 있습니다.
Spring MVC 예외 처리 컨트롤러 클래스 예외 처리기
여러 유형의 예외를 throw할 컨트롤러 클래스를 살펴봅시다.
package com.journaldev.spring.controllers;
import java.io.IOException;
import java.sql.SQLException;
import javax.servlet.http.HttpServletRequest;
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.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.journaldev.spring.exceptions.EmployeeNotFoundException;
import com.journaldev.spring.model.Employee;
import com.journaldev.spring.model.ExceptionJSONInfo;
@Controller
public class EmployeeController {
private static final Logger logger = LoggerFactory.getLogger(EmployeeController.class);
@RequestMapping(value="/emp/{id}", method=RequestMethod.GET)
public String getEmployee(@PathVariable("id") int id, Model model) throws Exception{
//다른 유형의 예외를 의도적으로 발생시킴
if(id==1){
throw new EmployeeNotFoundException(id);
}else if(id==2){
throw new SQLException("SQLException, id="+id);
}else if(id==3){
throw new IOException("IOException, id="+id);
}else if(id==10){
Employee emp = new Employee();
emp.setName("Pankaj");
emp.setId(id);
model.addAttribute("employee", emp);
return "home";
}else {
throw new Exception("Generic Exception, id="+id);
}
}
@ExceptionHandler(EmployeeNotFoundException.class)
public ModelAndView handleEmployeeNotFoundException(HttpServletRequest request, Exception ex){
logger.error("Requested URL="+request.getRequestURL());
logger.error("Exception Raised="+ex);
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("exception", ex);
modelAndView.addObject("url", request.getRequestURL());
modelAndView.setViewName("error");
return modelAndView;
}
}
EmployeeNotFoundException 핸들러에 대해 주의하세요. ModelAndView를 반환하고 있으므로 http 상태 코드가 OK(200)로 전송됩니다. 만약 void를 반환한다면 http 상태 코드는 404로 전송되었을 것입니다. 전역 예외 핸들러 구현에서 이러한 유형의 구현을 살펴보겠습니다. 컨트롤러에서는 EmployeeNotFoundException만 처리하므로 컨트롤러에서 발생한 모든 다른 예외는 전역 예외 핸들러에 의해 처리됩니다.
@ControllerAdvice 및 @ExceptionHandler
여기는 전역 예외 핸들러 컨트롤러 클래스입니다. 클래스가 @ControllerAdvice 어노테이션으로 주석이 달려 있음을 주의하십시오. 또한 메서드에는 @ExceptionHandler 어노테이션이 달려 있습니다.
package com.journaldev.spring.controllers;
import java.io.IOException;
import java.sql.SQLException;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
@ControllerAdvice
public class GlobalExceptionHandler {
private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);
@ExceptionHandler(SQLException.class)
public String handleSQLException(HttpServletRequest request, Exception ex){
logger.info("SQLException Occured:: URL="+request.getRequestURL());
return "database_error";
}
@ResponseStatus(value=HttpStatus.NOT_FOUND, reason="IOException occured")
@ExceptionHandler(IOException.class)
public void handleIOException(){
logger.error("IOException handler executed");
//404 오류 코드 반환
}
}
SQLException의 경우, http 상태 코드를 200으로 설정하여 database_error.jsp를 응답 페이지로 반환합니다. IOException의 경우, 상태 코드를 404로 설정하고 반환 값으로 void를 사용하므로 이 경우에는 error-page가 사용됩니다. 여기서 다른 종류의 예외 처리는 하지 않았으며, 이 부분은 HandlerExceptionResolver 구현에 맡기고 있습니다.
HandlerExceptionResolver
우리는 단순한 SimpleMappingExceptionResolver를 확장하고 그 중 하나의 메서드를 재정의하고 있지만, 이 메서드 중 가장 중요한 메서드인 resolveException
을 재정의하여 로깅 및 다른 유형의 뷰 페이지를 전송할 수도 있습니다. 하지만 이는 ControllerAdvice 구현을 사용하는 것과 동일하므로 이를 남겨두었습니다. 우리는 이를 사용하여 우리가 처리하지 않은 모든 다른 예외에 대한 일반적인 오류 페이지로 응답하도록 뷰 페이지를 구성할 것입니다.
스프링 예외 처리 구성 파일
우리의 스프링 빈 구성 파일은 아래와 같습니다. spring.xml 코드:
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="https://www.springframework.org/schema/mvc"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="https://www.springframework.org/schema/beans"
xmlns:context="https://www.springframework.org/schema/context"
xsi:schemaLocation="https://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
https://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<beans:bean id="simpleMappingExceptionResolver" class="com.journaldev.spring.resolver.MySimpleMappingExceptionResolver">
<beans:property name="exceptionMappings">
<beans:map>
<beans:entry key="Exception" value="generic_error"></beans:entry>
</beans:map>
</beans:property>
<beans:property name="defaultErrorView" value="generic_error"/>
</beans:bean>
<!-- Configure to plugin JSON as request and response in method handler -->
<beans:bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<beans:property name="messageConverters">
<beans:list>
<beans:ref bean="jsonMessageConverter"/>
</beans:list>
</beans:property>
</beans:bean>
<!-- Configure bean to convert JSON to POJO and vice versa -->
<beans:bean id="jsonMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
</beans:bean>
<context:component-scan base-package="com.journaldev.spring" />
</beans:beans>
웹 애플리케이션에서 JSON 지원을 위해 구성된 빈들에 대한 공지입니다. 예외 처리와 관련된 유일한 부분은 simpleMappingExceptionResolver 빈 정의입니다. 여기서는 Exception 클래스를 위한 뷰 페이지로 generic_error.jsp를 정의하고 있습니다. 이렇게 함으로써 애플리케이션에서 처리되지 않는 예외는 서버에서 생성된 오류 페이지를 응답으로 보내지 않도록 합니다.
Spring MVC 예외 처리 JSP 뷰 페이지
이제 애플리케이션의 마지막 부분인 뷰 페이지를 살펴보겠습니다. 이 뷰 페이지들은 애플리케이션에서 사용될 것입니다. home.jsp 코드:
<%@ taglib uri="https://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<html>
<head>
<title>Home</title>
</head>
<body>
<h3>Hello ${employee.name}!</h3><br>
<h4>Your ID is ${employee.id}</h4>
</body>
</html>
home.jsp는 유효한 데이터로 응답하기 위해 사용됩니다. 즉, 클라이언트 요청에서 id가 10인 경우입니다. 404.jsp 코드:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "https://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>404 Error Page</title>
</head>
<body>
<h2>Resource Not Found Error Occured, please contact support.</h2>
</body>
</html>
404.jsp는 404 http 상태 코드에 대한 뷰를 생성하는 데 사용됩니다. 우리의 구현에서는 클라이언트 요청에서 id가 3인 경우에 해당합니다. error.jsp 코드:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "https://www.w3.org/TR/html4/loose.dtd">
<%@ taglib uri="https://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Error Page</title>
</head>
<body>
<h2>Application Error, please contact support.</h2>
<h3>Debug Information:</h3>
Requested URL= ${url}<br><br>
Exception= ${exception.message}<br><br>
<strong>Exception Stack Trace</strong><br>
<c:forEach items="${exception.stackTrace}" var="ste">
${ste}
</c:forEach>
</body>
</html>
error.jsp는 컨트롤러 클래스의 요청 처리기 메소드에서 EmployeeNotFoundException을 던질 때 사용됩니다. 클라이언트 요청에서 id 값이 1인 경우 이 페이지를 응답으로 받아야 합니다. database_error.jsp 코드:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "https://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Database Error Page</title>
</head>
<body>
<h2>Database Error, please contact support.</h2>
</body>
</html>
database_error.jsp는 애플리케이션이 SQLException을 던질 때 사용됩니다. 이는 GlobalExceptionHandler 클래스에서 구성되어 있습니다. 클라이언트 요청에서 id 값이 2인 경우 이 페이지를 응답으로 받아야 합니다. generic_error.jsp 코드:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "https://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Generic Error Page</title>
</head>
<body>
<h2>Unknown Error Occured, please contact support.</h2>
</body>
</html>
이 페이지는 우리 애플리케이션 코드에서 처리되지 않는 모든 예외가 발생할 때 simpleMappingExceptionResolver 빈이 처리하는 경우의 응답 페이지여야 합니다. 클라이언트 요청의 id 값이 1, 2, 3 또는 10이 아닌 경우 이 페이지를 응답으로 받아야 합니다.
Spring MVC 예외 처리 응용 프로그램 실행
어플리케이션을 배포하려면 사용 중인 서블릿 컨테이너에만 설치하면 됩니다. 이 예제에서는 Apache Tomcat 7을 사용합니다. 아래 이미지는 id 값에 따라 우리의 어플리케이션이 반환하는 다른 응답 페이지를 보여줍니다. ID=10, 유효한 응답. ID=1, 컨트롤러 기반 예외 처리기 사용됨
ID=2, 뷰로 사용되는 전역 예외 처리기 사용됨
ID=3, 404 오류 페이지 사용됨
ID=4, 응답 뷰에 사용되는 simpleMappingExceptionResolver 사용됨
모든 경우에 우리가 기대한 응답을 얻었다는 것을 확인할 수 있습니다.
스프링 예외 처리기 JSON 응답
우리는 거의 자습서를 마쳤습니다. 마지막 부분에서는 예외 처리기 메서드에서 JSON 응답을 보내는 방법을 설명하겠습니다. 우리의 애플리케이션에는 모든 JSON 종속성이 있으며 jsonMessageConverter가 구성되어 있습니다. 예외 처리기 메서드를 구현하기만 하면 됩니다. 간단하게 하기 위해 EmployeeController handleEmployeeNotFoundException() 메서드를 JSON 응답을 반환하도록 다시 작성하겠습니다. 아래 코드로 EmployeeController 예외 처리기 메서드를 업데이트하고 애플리케이션을 다시 배포하십시오.
@ExceptionHandler(EmployeeNotFoundException.class)
public @ResponseBody ExceptionJSONInfo handleEmployeeNotFoundException(HttpServletRequest request, Exception ex){
ExceptionJSONInfo response = new ExceptionJSONInfo();
response.setUrl(request.getRequestURL().toString());
response.setMessage(ex.getMessage());
return response;
}
이제 클라이언트 요청에서 id를 1로 사용할 때, 아래 이미지에 표시된대로 다음과 같은 JSON 응답을 받게 됩니다. 이것으로 스프링 예외 처리와 스프링 MVC 예외 처리에 관한 설명을 마칩니다. 아래 URL에서 애플리케이션을 다운로드하여 더 많은 학습을 진행하십시오.