spring mvc異常統一處理(ControllerAdvice註解)
source http://blog.csdn.net/chenaschen/article/details/51291566
spring mvc異常統一處理(ControllerAdvice註解)
1、配置
spring 版本:
- <org.springframework-version>4.1.9.RELEASE</org.springframework-version>
- <annotation-driven />
- <?xml version="1.0" encoding="UTF-8"?>
- <beans:beans xmlns="http://www.springframework.org/schema/mvc"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/context http://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>
- <context:component-scan base-package="org.as.asjee" use-default-filters="false">
- <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
- </context:component-scan>
- </beans:beans>
2、異常統一處理類
- package org.as.asjee.core.exception;
- import java.sql.SQLException;
- import javax.servlet.http.HttpServletRequest;
- import org.as.asjee.core.log.AsJEELogger;
- import org.as.asjee.core.log.AsJEELoggerFactory;
- import org.springframework.web.bind.annotation.ControllerAdvice;
- import org.springframework.web.bind.annotation.ExceptionHandler;
- import org.springframework.web.bind.annotation.ResponseBody;
- import org.springframework.web.servlet.ModelAndView;
- /**
- * 捕獲異常統一處理
- * @description TODO
- * @author chen.gs
- * @create date 2016年4月28日
- * @modified by
- * @modify date
- * @version v1.0
- */
- @ControllerAdvice
- public class GlobalExceptionHandler {
- private final static AsJEELogger LOG = AsJEELoggerFactory.getLogger(GlobalExceptionHandler.class);
- private final static String EXPTION_MSG_KEY = "message";
- @ExceptionHandler(BusinessException.class)
- @ResponseBody
- public void handleBizExp(HttpServletRequest request, Exception ex){
- LOG.info("Business exception handler " + ex.getMessage() );
- request.getSession(true).setAttribute(EXPTION_MSG_KEY, ex.getMessage());
- }
- @ExceptionHandler(SQLException.class)
- public ModelAndView handSql(Exception ex){
- LOG.info("SQL Exception " + ex.getMessage());
- ModelAndView mv = new ModelAndView();
- mv.addObject("message", ex.getMessage());
- mv.setViewName("sql_error");
- return mv;
- }
- }
- package org.as.asjee.core.exception;
- /**
- * 業務異常
- * @description TODO
- * @author chen.gs
- * @create date 2016年4月28日
- * @modified by
- * @modify date
- * @version v1.0
- */
- public class BusinessException extends Exception{
- private static final long serialVersionUID = 1L;
- //業務型別
- private String bizType;
- //業務程式碼
- private int bizCode;
- //錯誤資訊
- private String message;
- public BusinessException(String bizType, int bizCode, String message){
- super(message);
- this.bizType = bizType;
- this.bizCode = bizCode;
- this.message = message;
- }
- public BusinessException(String message){
- super(message);
- this.bizType = "";
- this.bizCode = -1;
- this.message = message;
- }
- public BusinessException(String bizType, String message){
- super(message);
- this.bizType = bizType;
- this.bizCode = -1;
- this.message = message;
- }
- public BusinessException(int bizCode, String message){
- super(message);
- this.bizType = "";
- this.bizCode = bizCode;
- this.message = message;
- }
- public String getBizType() {
- return bizType;
- }
- public void setBizType(String bizType) {
- this.bizType = bizType;
- }
- public int getBizCode() {
- return bizCode;
- }
- public void setBizCode(int bizCode) {
- this.bizCode = bizCode;
- }
- public String getMessage() {
- return message;
- }
- public void setMessage(String message) {
- this.message = message;
- }
- }
3、controller
- package org.as.asjee.core.security.web;
- import java.sql.SQLException;
- import javax.annotation.Resource;
- import org.as.asjee.core.exception.BusinessException;
- import org.as.asjee.core.security.model.User;
- import org.as.asjee.core.security.service.UserService;
- import org.as.asjee.core.service.ServiceFacade;
- import org.as.asjee.core.web.AbstractController;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- @Controller
- @RequestMapping("/security/user")
- public class UserController extends AbstractController<User>{
- @Resource
- private UserService userService;
- @Resource
- private ServiceFacade serviceFacade;
- @RequestMapping("login")
- public String login() {
- return "login";
- }
- @RequestMapping("login2")
- public String login2() throws Exception {
- throw new SQLException("出錯鳥。。。。。。。。。");
- }
- @RequestMapping("login3")
- public String login3() throws Exception {
- throw new BusinessException("業務執行異常");
- }
- //此方法丟擲的異常不是由GlobalExceptionHandler處理
- //而是在catch塊處理
- @RequestMapping("login4")
- public String login4() {
- try {
- throw new BusinessException("業務執行異常");
- } catch (BusinessException e) {
- e.printStackTrace();
- }
- return "login";
- }
- }
4、JSP頁面
sql_error.jsp
- <%@ page language="java" contentType="text/html; charset=UTF-8" %>
- <!DOCTYPE html">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>Insert title here</title>
- </head>
- <body>
- <h1>ERROR MESSAGE</h1>
- <p>${message}</p>
- </body>
- </html>
5、簡要說明
在Controller中丟擲的異常,當沒有被catch處理時,GlobalExceptionHandler中定義的處理方法可以起作用,在方法寫明註解@ExceptionHandler,並註明其異常類即可。此種方法不僅可以作用於Controller,同樣的在DAO層、service層也可,都可以由GlobalExceptionHandler進行處理。此種寫法減少程式碼的入侵,值得推薦。
異常的統一處理只是註解ControllerAdvice用處之一,有興趣瞭解更多的,請到spring官網查閱。
相關文章
- Spring MVC統一異常處理SpringMVC
- Spring @ControllerAdvice+@ExceptionHandler統一異常處理SpringControllerException
- Spring進階之@ControllerAdvice與統一異常處理SpringController
- Spring系列(七) Spring MVC 異常處理SpringMVC
- Spring MVC原始碼(四) ----- 統一異常處理原理解析SpringMVC原始碼
- Spring註解之@ExceptionHandler 統一異常處理和獲取方法名SpringException
- spring boot 統一異常處理Spring Boot
- spring中的統一異常處理Spring
- MVC使用異常過濾器處理異常MVC過濾器
- 【SpringBoot】全域性異常處理@ControllerAdviceSpring BootController
- ASP.NET MVC 異常處理ASP.NETMVC
- Spring Boot統一異常處理最佳實踐Spring Boot
- Retrofit統一異常處理
- Spring Boot實戰系列(4)統一異常處理Spring Boot
- C#自定義異常 統一異常處理C#
- Spring Boot 異常處理Spring Boot
- SpringMVC 統一異常處理SpringMVC
- Spring Boot統一異常處理以及引數校驗Spring Boot
- sprig中基於註解的異常處理
- SpringBoot統一異常處理Spring Boot
- 統一返回物件和異常處理(一)物件
- spring 全域性異常處理Spring
- 統一返回物件和異常處理(二)物件
- 深入理解Spring異常處理Spring
- 解讀Rails – 處理異常AI
- 異常篇——異常處理
- MySQL定義異常和異常處理詳解MySql
- Spring MVC ControllerAdvice深入解析SpringMVCController
- 介面異常狀態統一處理方案:優先業務端處理,再按需統一處理。
- 異常處理
- SpringBoot處理全域性統一異常Spring Boot
- 前端錯誤收集以及統一異常處理前端
- SpringBoot實現統一異常處理Spring Boot
- 簡單的全域性異常統一處理
- Spring Boot2從入門到實戰:統一異常處理Spring Boot
- C++ 異常處理機制詳解:輕鬆掌握異常處理技巧C++
- Java 傳統異常處理(二)Java
- 詳解C#異常處理C#