Spring MVC @ExceptionHandlerの例
このチュートリアルでは、Spring MVCフレームワークで例外処理を行う方法を説明します。通常は、@ ExceptionHandlerを使用して、特定の例外が発生した場合に返される「ビュー」を決定します。
P.Sこの@ExceptionHandlerクラスは、Spring 3.0
1.プロジェクトの構成
標準のMavenプロジェクトであるプロジェクトのディレクトリ構造を見直してください。
2.カスタム例外
カスタムエラーコードとエラーの説明付きのカスタム例外。
CustomGenericException.java
package com.mkyong.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.mkyong.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.mkyong.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"%> <html> <body> <h2>Spring MVC @ExceptionHandler Example</h2> <c:if test="${not empty msg}"> <h2>${msg}</h2> </c:if> </body> </html>
pages/error/generic__error.jsp
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <html> <body> <c:if test="${not empty errCode}"> <h1>${errCode} : System Errors</h1> </c:if> <c:if test="${empty errCode}"> <h1>System Errors</h1> </c:if> <c:if test="${not empty errMsg}"> <h2>${errMsg}</h2> </c:if> </body> </html>
5.テスト
次の3つのテストケースを検討してください。
6. @ControllerAdviceの例
上記の
@ExceptionHandler`の例は、単一のコントローラにのみ適用され、グローバルに(すべてのコントローラ)適用され、
@ ControllerAdvice`でクラスに注釈が付けられます。
GlobalExceptionController.java
package com.mkyong.web.controller; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.servlet.ModelAndView; import com.mkyong.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)
参考文献
@ExceptionHandler JavaDoc]。
http://docs.spring.io/spring/docs/3.2.x/javadoc-api/org/springframework/web/bind/annotation/ControllerAdvice.html
[Spring
@ControllerAdvice JavaDoc]。リンク://spring-mvc/spring-mvc-exception-handling-example/[Spring MVC
例外処理の例]