服務異常處理
本節內容,RESTful API錯誤處理
- spring boot 默認的處理機制
- 自定義異常處理
場景演示
用post請求的時候(可以用postman或則視頻中講解的谷歌插件 Restlet Client - REST API Testing );
Restlet Client - REST API Testing 這個谷歌插件可以去了解下,感覺挺好用的,可以項目分類和全部執行測試Restlet Client 中請求後,可以點擊返回的狀態碼,會跳轉到規範中去,還挺不錯:https://tools.ietf.org/html/rfc7231#section-6.5.4 比如404狀態
通過瀏覽器和客戶端(postman等工具)分別訪問一個不存在的地址:比如 http://localhost:8080/xxx
瀏覽器:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Thu Aug 02 13:40:20 GMT+08:00 2018
There was an unexpected error (type=Not Found, status=404).
No message available
客戶端:
{
"timestamp": "2018-08-02 13:42:56",
"status": 404,
"error": "Not Found",
"message": "No message available",
"path": "/xxx"
}
會發現同一個地址,不同的請求頭訪問會返回不同的結果;那麼這種是怎麼實現的呢?
有一個類是處理這種情況的;(具體是怎麼發現的,我也不知道)
org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController
@Controller
@RequestMapping("${server.error.path:${error.path:/error}}")
public class BasicErrorController extends AbstractErrorController {
@RequestMapping(produces = "text/html")
public ModelAndView errorHtml(HttpServletRequest request,
HttpServletResponse response) {
@RequestMapping
@ResponseBody
public ResponseEntity<Map<String, Object>> error(HttpServletRequest request) {
RequestMapping 居然還能這麼使用,沒有看明白;但是需要知道的是:
- 可以通過
@RequestMapping(produces = "text/html")
來接受指定的Content-Type訪問 - 沒有任何指定的將接受所有的請求頭,但是優先匹配指定的路徑
剛纔訪問的路徑請求頭(request header裏面)來對比下
瀏覽器:Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
客戶端:Accept: */*
一定要注意一個思想:
restfull api 是通過http狀態碼返回不同的場景,所以這裏status是404.而且請求響應狀態也是404;自己在設計的時候可以參考這個思想;
還有一個提示信息 “No message available” 404 狀態就是沒有找到可處理的路徑,沒有可用的信息也符合解釋
但是在實際開發中,只是憑藉http狀態碼的話,是不能解決實際場景中的需求的;
比如 創建用戶信息,缺少字段,需要返回哪一個字段缺少?
在spring 默認的處理中,使用 @Valid 但是不聲明BindingResult
的時候,會把很詳細的驗證信息返回去;
自定義異常處理
在瀏覽器中發出的錯誤信息定義
在資源文件夾下創建對應狀態碼的html頁面就可以了
|- resources
|- error
|- 404.html
|- 500.html
但是在本版本中,存儲的定義之後,並沒有什麼效果;把error放在templates中也沒有效果;以後再研究了吧
補充:默認的html文件只能放在 static目錄下才有效果
有自定義邏輯的異常處理
- 自定義一種異常
@ControllerAdvice
註解標識一個異常處理器
自定義用戶不存在異常
package com.example.demo.exception;
/**
* ${desc}
* @author zhuqiang
* @version 1.0.1 2018/8/2 14:20
* @date 2018/8/2 14:20
* @since 1.0
*/
public class UserNotExistException extends RuntimeException {
private String id;
public UserNotExistException(String id) {
super("user not exist");
this.id = id;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
增加異常處理器
package com.example.demo.web.controller;
import com.example.demo.exception.UserNotExistException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;
/**
* ${desc}
* @author zhuqiang
* @version 1.0.1 2018/8/2 14:26
* @date 2018/8/2 14:26
* @since 1.0
*/
@ControllerAdvice
public class ControllerExceptionHandler {
@ExceptionHandler(UserNotExistException.class)
@ResponseBody // 用json方式返回
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) // 返回http狀態碼
public Map<String, Object> handleUserNotExistException(UserNotExistException ux) {
Map<String, Object> resullt = new HashMap<>();
resullt.put("id", ux.getId());
resullt.put("message", ux.getMessage());
return resullt;
}
}
@ExceptionHandler
中有一個異常對象,也就是爲什麼可以在自定義方式入參中能拿到異常對象的原因