在本教程中,我们向您展示了如何在Spring MVC框架中进行异常处理。 通常情况下,我们使用@ExceptionHandler
来决定在出现某种异常时应该返回哪个“视图”。
这个@ExceptionHandler
类自Spring 3.0起可用
1.项目结构
查看项目目录结构,一个标准的Maven项目。
2. 自定义 Exception
自定义异常,包含自定义错误代码和错误描述。
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 Controller
Spring控制器,请查看下面的执行流程:
如果用户提供 a /error 请求,它将抛出“CustomGenericException”,并且handleCustomException()方法将被触发。
如果用户提供 a /io-error 请求,它将抛出“IOException”,handleAllException()方法将被触发。
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 页面
<%@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>
<%@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个测试用例:
1. http://localhost:8080/SpringMvcExample/anything
2. http://localhost:8080/SpringMvcExample/error
3. http://localhost:8080/SpringMvcExample/io-error
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)
参考
译文地址:http://www.mkyong.com/spring-mvc/spring-mvc-exceptionhandler-example/
未经允许请勿转载:程序喵 » Spring MVC @ExceptionHandler 示例