這篇文章主要為大家展示了總結Spring異常處理,內容簡而易懂,希望大家可以學習一下,學習完之后肯定會有收獲的,下面讓小編帶大家一起來看看吧。
1. 前言
統一的異常處理對于應用的重要性不言而喻。今天我們來介紹一下 Spring 如何來進行統一的 Rest 異常處理。同時我們也會簡單比較一下它們之間的優劣。
在控制器中聲明一個方法然后用 @ExceptionHandler 注解標記即可:
@Controller
@RequestMapping("/test")
public class TestController {
@RequestMapping("/err")
@ResponseBody
public Object demo1(){
int i = 1 / 0;
return new Date();
}
@ExceptionHandler({RuntimeException.class})
public ModelAndView fix(Exception ex){
System.out.println(ex.getMessage());
return new ModelAndView("error",new ModelMap("ex",ex.getMessage()));
}
}優點:
@ExceptionHandler 標記的方法返回值類型支持多種??梢允且晥D,也可以是 json 等。缺點:
Controller 中的 @ExceptionHandler 注解上的異常類型不能出現相同的,否則運行時拋異常。Controller 并不是真正意義上的全局異常。如果要想作用于全局需要將其放入所有控制器的父類中。這是 2. 的改進型,通過定義 @ControllerAdvice 類并在方法上標記 @ExceptionHandler ,達到了全局異常處理的目的:
@ControllerAdvice
public class TestController {
@ExceptionHandler({RuntimeException.class})
public ModelAndView fix(Exception ex){
System.out.println(ex.getMessage());
return new ModelAndView("error",new ModelMap("ex",ex.getMessage()));
}
}優點:
缺點:
Controller 中的 @ExceptionHandler 注解上的異常類型不能出現相同的,否則運行時拋異常。一般情況下也建議使用該方式進行異常處理。大多數情況下都是兼容的。
實現 HandlerExceptionResolver 接口,這里我們繼承其抽象實現 AbstractHandlerExceptionResolver :
@Component
public class RestResponseStatusExceptionResolver extends AbstractHandlerExceptionResolver {
@Override
protected ModelAndView doResolveException(
HttpServletRequest request,
HttpServletResponse response,
Object handler,
Exception ex) {
try {
if (ex instanceof IllegalArgumentException) {
return handleIllegalArgument((IllegalArgumentException) ex, response, handler);
}
//todo more exception
} catch (Exception handlerException) {
//todo
}
return null;
}
private ModelAndView
handleIllegalArgument(IllegalArgumentException ex, HttpServletResponse response)
throws IOException {
response.sendError(HttpServletResponse.SC_CONFLICT);
String accept = request.getHeader(HttpHeaders.ACCEPT);
//todo more response
return new ModelAndView();
}
}優點:
JSP、velocity 等模板視圖比較方便。ModelAndView 但是因為參數中有 HttpServletResponse, 我們可以利用它來進行定制響應結果。例如,如果客戶端要求輸入application / json,那么在出現錯誤情況時,我們要確保我們返回一個以application / json編碼的響應。缺點:
HttpServletResponse 交互才能實現各種形式的響應體。如果你用的框架是 Spring Boot 。 我們還可以用它獨特的處理方式。優點是屏蔽了低級的API,缺點也比較明顯,無法捕捉到具體的異常。
Spring Boot 在默認情況下,提供了 /error 映射來處理所有錯誤,在 Servlet 容器里注冊了全局的錯誤頁面(Whitelabel Error Page)并返回客戶端。
通過實現 ErrorController 接口并注冊為 Bean。這里不再舉例??蓞⒖?BasicErrorController 。
ErrorAttributes我們也可以添加 ErrorAttributes 類型的 Bean 來替換替換默認的異常處理。
@Component
public class MyCustomErrorAttributes extends DefaultErrorAttributes {
@Override
public Map<String, Object> getErrorAttributes(
WebRequest webRequest, boolean includeStackTrace) {
Map<String, Object> errorAttributes =
super.getErrorAttributes(webRequest, includeStackTrace);
errorAttributes.put("locale", webRequest.getLocale()
.toString());
errorAttributes.remove("error");
//todo your business
return errorAttributes;
}
}Spring Boot 自動配置還提供了實現 ErrorController 接口異常處理的基類 BasicErrorController,默認是處理 text/html類型請求的錯誤,可以繼承該基類自定義處理更多的請求類型,添加公共方法并使用 @RequestMapping 注解的 produce屬性指定處理類型。
@Component
public class MyErrorController extends BasicErrorController {
public MyErrorController(ErrorAttributes errorAttributes) {
super(errorAttributes, new ErrorProperties());
}
@RequestMapping(produces = MediaType.APPLICATION_XML_VALUE)
public ResponseEntity<Map<String, Object>> xmlError(HttpServletRequest request) {
//todo your business
}
}另外在最新的 Spring 5 中你還可以通過 拋出 ResponseStatusException 異常來進行處理。
好處:
缺點:
以上就是關于總結Spring異常處理的內容,如果你們有學習到知識或者技能,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。