天天看點

【架構----SpringBoot】【1.0】【第二十三章】 springboot + 全局異常處理一、單個controller範圍的異常處理二、全部controller範圍内起作用的異常處理(全局異常處理)

一、單個controller範圍的異常處理

package com.xxx.secondboot.web;

import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.xxx.secondboot.exception.MyExceptionResponse;

import io.swagger.annotations.Api;

@Api("測試controllerAdvice和全局異常處理")
@RestController
@RequestMapping("/advice1")
public class AdviceController {

    @RequestMapping(value = "/test1", method = RequestMethod.GET)
    public String test1() {
        throw new RuntimeException("advice1 - exception1");
    }

    @RequestMapping(value = "/test2", method = RequestMethod.GET)
    public String test2() {
        throw new RuntimeException("advice1 - exception2");
    }

    @ExceptionHandler(RuntimeException.class)
    public MyExceptionResponse exceptionHandler() {
        MyExceptionResponse resp = new MyExceptionResponse();
        resp.setCode(300);
        resp.setMsg("exception-Handler");
        return resp;
    }

}
           

說明:

  • 在controller中加入被@ExceptionHandler修飾的類即可(在該注解中指定該方法需要處理的那些異常類)
  • 該異常處理方法隻在目前的controller中起作用

二、全部controller範圍内起作用的異常處理(全局異常處理)

1、全局異常處理類

package com.xxx.secondboot.web;

import javax.servlet.http.HttpServletResponse;

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.bind.annotation.RestController;

import com.xxx.secondboot.exception.MyExceptionResponse;
import com.xxx.secondboot.exception.MyRuntimeException;

//@ControllerAdvice(annotations=RestController.class)
//@ControllerAdvice(basePackages={"com.xxx","com.ooo"})
@ControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(RuntimeException.class)
    //    @ExceptionHandler(value={RuntimeException.class,MyRuntimeException.class})
    //    @ExceptionHandler//處理所有異常
    @ResponseBody //在傳回自定義相應類的情況下必須有,這是@ControllerAdvice注解的規定
    public MyExceptionResponse exceptionHandler(RuntimeException e, HttpServletResponse response) {
        MyExceptionResponse resp = new MyExceptionResponse();
        resp.setCode(300);
        resp.setMsg("exception-Handler");
        //        response.setStatus(600);
        return resp;
    }
}
           

說明:

  • @ControllerAdvice是controller的一個輔助類,最常用的就是作為全局異常處理的切面類
  • @ControllerAdvice可以指定掃描範圍
  • @ControllerAdvice約定了幾種可行的傳回值,如果是直接傳回model類的話,需要使用@ResponseBody進行json轉換
    • 傳回String,表示跳到某個view
    • 傳回modelAndView
    • 傳回model + @ResponseBody

2、controller

package com.xxx.secondboot.web;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;

@Api("測試controllerAdvice和全局異常處理")
@RestController
@RequestMapping("/advice1")
public class AdviceController {

    @RequestMapping(value = "/test1", method = RequestMethod.GET)
    public String test1() {
        throw new RuntimeException("advice1 - exception1");
    }

    @RequestMapping(value = "/test2", method = RequestMethod.GET)
    public String test2() {
        throw new RuntimeException("advice1 - exception2");
    }

    //    @ExceptionHandler(RuntimeException.class)
    //    public MyExceptionResponse exceptionHandler() {
    //        MyExceptionResponse resp = new MyExceptionResponse();
    //        resp.setCode(300);
    //        resp.setMsg("exception-Handler");
    //        return resp;
    //    }

}
           

注意:

  • 同一個異常被局部範圍異常處理器和全局範圍異常處理器同時覆寫,會選擇小範圍的局部範圍處理器
  • 同一個異常被小範圍的異常類和大範圍的異常處理器同時覆寫,會選擇小範圍的異常處理器

參考自:

  • http://jinnianshilongnian.iteye.com/blog/1866350 開濤的@ControllerAdvice(三個作用)
  • http://www.tuicool.com/articles/fA7nuii                springboot約定的異常處理體系
  • https://spring.io/blog/2013/11/01/exception-handling-in-spring-mvc springMVC異常處理體系
  • http://www.baeldung.com/2013/01/31/exception-handling-for-rest-with-spring-3-2/ springMVC異常處理體系

繼續閱讀