Spring MVC @ExceptionHandlerの例
このチュートリアルでは、Spring MVCフレームワークで例外処理を行う方法を示します。 通常、@ExceptionHandlerを使用して、特定の例外が発生した場合に返される「ビュー」を決定します。
P.S This @ExceptionHandler class is available since Spring 3.0
1. プロジェクト構造
標準のMavenプロジェクトであるプロジェクトディレクトリ構造を確認します。

2. カスタム例外
カスタムエラーコードとエラーの説明を含むカスタム例外。
CustomGenericException.java
package com.example.web.exception;
public class CustomGenericException extends RuntimeException {
private static final long serialVersionUID = 1L;
private String errCode;
private String errMsg;
public String getErrCode() {
return errCode;
}
public void setErrCode(String errCode) {
this.errCode = errCode;
}
public String getErrMsg() {
return errMsg;
}
public void setErrMsg(String errMsg) {
this.errMsg = errMsg;
}
public CustomGenericException(String errCode, String errMsg) {
this.errCode = errCode;
this.errMsg = errMsg;
}
}
3. スプリングコントローラー
Springコントローラー、以下の実行フローを確認します。
-
ユーザーが
/errorリクエストを提供すると、「CustomGenericException」がスローされ、handleCustomException()メソッドが実行されます。 -
ユーザーが
/io-errorリクエストを提供すると、「IOException」がスローされ、handleAllException()メソッドが起動されます。
MainController.java
package com.example.web.controller;
import java.io.IOException;
import org.springframework.stereotype.Controller;
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.servlet.ModelAndView;
import com.example.web.exception.CustomGenericException;
@Controller
public class MainController {
@RequestMapping(value = "/{type:.+}", method = RequestMethod.GET)
public ModelAndView getPages(@PathVariable("type") String type)
throws Exception {
if ("error".equals(type)) {
// go handleCustomException
throw new CustomGenericException("E888", "This is custom message");
} else if ("io-error".equals(type)) {
// go handleAllException
throw new IOException();
} else {
return new ModelAndView("index").addObject("msg", type);
}
}
@ExceptionHandler(CustomGenericException.class)
public ModelAndView handleCustomException(CustomGenericException ex) {
ModelAndView model = new ModelAndView("error/generic_error");
model.addObject("errCode", ex.getErrCode());
model.addObject("errMsg", ex.getErrMsg());
return model;
}
@ExceptionHandler(Exception.class)
public ModelAndView handleAllException(Exception ex) {
ModelAndView model = new ModelAndView("error/generic_error");
model.addObject("errMsg", "this is Exception.class");
return model;
}
}
4. JSPページ
pages/index.jsp
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
Spring MVC @ExceptionHandler Example
${msg}
pages/error/generic_error.jsp
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
${errCode} : System Errors
System Errors
${errMsg}
5. テスト
次の3つのテストケースを確認します。



6. @ControllerAdviceの例
上記の@ExceptionHandlerの例は、単一のコントローラーにのみ適用され、グローバルに(すべてのコントローラー)適用するには、クラスに@ControllerAdviceの注釈を付けます。
GlobalExceptionController.java
package com.example.web.controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;
import com.example.web.exception.CustomGenericException;
@ControllerAdvice
public class GlobalExceptionController {
@ExceptionHandler(CustomGenericException.class)
public ModelAndView handleCustomException(CustomGenericException ex) {
ModelAndView model = new ModelAndView("error/generic_error");
model.addObject("errCode", ex.getErrCode());
model.addObject("errMsg", ex.getErrMsg());
return model;
}
@ExceptionHandler(Exception.class)
public ModelAndView handleAllException(Exception ex) {
ModelAndView model = new ModelAndView("error/generic_error");
model.addObject("errMsg", "this is Exception.class");
return model;
}
}
7. ソースコードをダウンロード
ダウンロード–SpringMvc-ExceptionHandler-Example.zip(15 KB)