springMVC--全域異常處理

來源:互聯網
上載者:User

標籤:param   OLE   jsonview   The   not   .json   advice   springmvc   equal   

全域異常主要處理controller中拋出的異常,需注意controller中不需要捕獲異常

 

一、@ControllerAdvice + @ExceptionHandler 實現全域異常處理

1、@ControllerAdvice 中申明@ExceptionHandler 方法,對所有controller中的@RequestMapping方法有效。

2、@Controller 中申明@ExceptionHandler 方法,僅對當前controller中的@RequestMapping方法有效。

全域異常處理類GlobalErrorHandler .java代碼如下(需保證該類所在路徑可以被掃描載入到spring容器):

package com.tianwen.dcdp.exception.handler;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.web.bind.annotation.ControllerAdvice;import org.springframework.web.bind.annotation.ExceptionHandler;import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.view.json.MappingJackson2JsonView;@ControllerAdvicepublic class GlobalErrorHandler {    /**     * 檢查是否ajax請求     * @param request     * @return     */    public static boolean isAjax(HttpServletRequest request) {        return request.getHeader("X-Requested-With") != null &&                "XMLHttpRequest".equalsIgnoreCase(request.getHeader("X-Requested-With"));    }        /**     * 全域異常處理     * @param request     * @param response     * @param ex     * @return     * @throws Exception     */    @ExceptionHandler(Exception.class)    public Object errorHandler(HttpServletRequest request,            HttpServletResponse response,            Exception ex) throws Exception{        if(isAjax(request)) {            //如果是ajax請求,返回json資料            ModelAndView mav = new ModelAndView(new MappingJackson2JsonView());            mav.addObject("result", 0);            mav.addObject("msg", ex.getMessage());            return mav;        } else {            //如果不是ajax請求,跳轉到錯誤頁面            ModelAndView mv = new ModelAndView();            mv.addObject("exception", ex);            mv.addObject("url", request.getRequestURL());            mv.addObject("msg", ex.getMessage());            if(request.getRequestURL().toString().contains("/m/")) {                mv.setViewName("m/error");            } else {                mv.setViewName("error");            }            return mv;        }    }    }

spring 部分配置如下:

<!-- 自動掃描 -->    <context:component-scan base-package="com.tianwen.dcdp">        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>        <context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>    </context:component-scan>

springmvc部分配置如下:

<context:component-scan base-package="com.tianwen.dcdp.*">        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>        <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>    </context:component-scan>

如此便可實現異常的統一處理

 

未完待續

springMVC--全域異常處理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.