SpringBoot封裝響應處理超詳細講解
背景
越來越多的項目開始基于前后端分離的模式進行開發(fā),這對后端接口的報文格式便有了一定的要求。通常,我們會采用JSON格式作為前后端交換數據格式,從而減少溝通成本等。
報文基本格式
一般報文格式通常會包含狀態(tài)碼、狀態(tài)描述(或錯誤提示信息)、業(yè)務數據等信息。 在此基礎上,不同的架構師、項目搭建者可能會有所調整。 但從整體上來說,基本上都是大同小異。
在SpringBoot項目中,通常接口返回的報文中至少包含三個屬性:
code:請求接口的返回碼,成功或者異常等返回編碼,例如定義請求成功。
message:請求接口的描述,也就是對返回編碼的描述。
data:請求接口成功,返回的業(yè)務數據。
示例報文如下:
{ "code":200, "message":"SUCCESS", "data":{ "info":"測試成功" } }
在上述報文格式中,不同的設計者是會有一些分歧的,特別是code值的定義。如果完全基于RESTful API設計的話,code字段可能就不需要存在了,而是通過HTTP協(xié)議中提供的GET、POST、PUT、DELETE操作等來完成資源的訪問。
但在實踐中,不論是出于目前國內大多數程序員的習慣,還是受限于HTTP協(xié)議提供的操作方法的局限性,很少完全遵照RESTful API方式進行設計。通常都是通過自定義Code值的形式來賦予它業(yè)務意義或業(yè)務錯誤編碼。
雖然可以不用完全遵守RESTful API風格來定義Code,在Code值的自定義中,也存在兩種形式:遵循HTTP狀態(tài)碼和自主定義。
像上面的示例,用200表示返回成功,這就是遵循HTTP響應狀態(tài)碼的形式來返回,比如還有其他的400、401、404、500等。當然,還有完全自主定義的,比如用0表示成功,1表示失敗,然后再跟進通用編碼、業(yè)務分類編碼等進行定義。
在此,筆者暫不評論每種形式的好壞,只列舉了常規(guī)的幾種形式,大家了解對應的情況,做到心中有數,有所選擇即可。
創(chuàng)建枚舉類
用于定義返回的錯誤碼:
public enum ErrorCode { SUCCESS(0, "ok", ""), FAIL(500, "failed",""), PARAMS_ERROR(40000, "請求參數錯誤", ""), NULL_ERROR(40001, "請求數據為空", ""), NOT_LOGIN(40100, "未登錄", ""), NO_AUTH(40101, "無權限", ""), SYSTEM_ERROR(50000, "系統(tǒng)內部異常", ""); private final int code; /** * 狀態(tài)碼信息 */ private final String message; /** * 狀態(tài)碼描述(詳情) */ private final String description; ErrorCode(int code, String message, String description) { this.code = code; this.message = message; this.description = description; } public int getCode() { return code; } public String getMessage() { return message; } public String getDescription() { return description; } }
定義統(tǒng)一返回結果實體類
@Data public class BaseResponse<T> implements Serializable { private int code; private T data; private String message; private String description; public BaseResponse(int code, T data, String message, String description) { this.code = code; this.data = data; this.message = message; this.description = description; } public BaseResponse(int code, T data, String message) { this(code, data, message, ""); } public BaseResponse(int code, T data) { this(code, data, "", ""); } public BaseResponse(ErrorCode errorCode) { this(errorCode.getCode(), null, errorCode.getMessage(), errorCode.getDescription()); } }
在BaseResponse中運 用了泛型和公共方法、構造方法的封裝,方便在業(yè)務中使用。 示例中只提供了部分方法的封裝,根據自身業(yè)務場景和需要可進一步封裝。
定義返回工具類
public class ResultUtils { /** * 成功 * * @param data * @param <T> * @return */ public static <T> BaseResponse<T> success(T data) { return new BaseResponse<>(ErrorCode.SUCCESS.getCode(), data, "ok"); } public static <T> BaseResponse<T> success(String message) { return new BaseResponse<>(ErrorCode.SUCCESS.getCode(), null, message); } /** * 失敗 * * @param errorCode * @return */ public static BaseResponse error(ErrorCode errorCode) { return new BaseResponse<>(errorCode); } /** * 失敗 * * @param code * @param message * @param description * @return */ public static BaseResponse error(int code, String message, String description) { return new BaseResponse(code, null, message, description); } /** * 失敗 * * @param errorCode * @return */ public static BaseResponse error(ErrorCode errorCode, String message, String description) { return new BaseResponse(errorCode.getCode(), null, message, description); } /** * 失敗 * * @param errorCode * @return */ public static BaseResponse error(ErrorCode errorCode, String description) { return new BaseResponse(errorCode.getCode(), errorCode.getMessage(), description); } public static BaseResponse error(String message) { return new BaseResponse(ErrorCode.FAIL.getCode(),message); } /** * 失敗 * * @param errorCode * @return */ public static BaseResponse error(HttpStatus errorCode, String description) { return new BaseResponse(errorCode.value(), errorCode.getReasonPhrase(), description); } }
統(tǒng)一報文封裝在接口中的使用
@RestController public class TestController { @RequestMapping("/calc") public ResponseInfo<String> calc(Integer id) { try { // 模擬異常業(yè)務代碼 int num = 1 / id; log.info("計算結果num={}", num); return ResultUtils.success(); } catch (Exception e) { return ResponseInfo.error("系統(tǒng)異常,請聯(lián)系管理員!"); } } }
統(tǒng)一異常處理
在上述實例中,我們通過try…catch的形式捕獲異常,并進行處理。 在SpringBoot中,我們可以通過RestControllerAdvice注解來定義全局異常處理,這樣就無需每處都try…catch了。
/** * 全局異常處理器 * */ @RestControllerAdvice @Slf4j public class GlobalExceptionHandler { @ExceptionHandler(BusinessException.class) public BaseResponse businessExceptionHandler(BusinessException e) { log.error("businessException: " + e.getMessage(), e); return ResultUtils.error(e.getCode(), e.getMessage(), e.getDescription()); } @ExceptionHandler(RuntimeException.class) public BaseResponse runtimeExceptionHandler(RuntimeException e) { log.error("runtimeException", e); return ResultUtils.error(ErrorCode.SYSTEM_ERROR, e.getMessage(), ""); } /** * 參數格式異常處理 */ @ExceptionHandler({IllegalArgumentException.class}) @ResponseStatus(HttpStatus.BAD_REQUEST) public BaseResponse badRequestException(IllegalArgumentException ex) { log.error("參數格式不合法:{}", ex.getMessage()); return ResultUtils.error(HttpStatus.BAD_REQUEST, "參數格式不符!"); } /** * 權限不足異常處理 */ @ExceptionHandler({AccessDeniedException.class}) @ResponseStatus(HttpStatus.FORBIDDEN) public BaseResponse badRequestException(AccessDeniedException ex) { return ResultUtils.error(HttpStatus.FORBIDDEN, ex.getMessage()); } /** * 參數缺失異常處理 */ @ExceptionHandler({MissingServletRequestParameterException.class}) @ResponseStatus(HttpStatus.BAD_REQUEST) public BaseResponse badRequestException(Exception ex) { return ResultUtils.error(HttpStatus.BAD_REQUEST, "缺少必填參數!"); } /** * 空指針異常 */ @ExceptionHandler(NullPointerException.class) @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR) public BaseResponse handleTypeMismatchException(NullPointerException ex) { log.error("空指針異常,{}", ex.getMessage()); return ResultUtils.error("空指針異常"); } @ExceptionHandler(Exception.class) @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR) public BaseResponse handleUnexpectedServer(Exception ex) { log.error("系統(tǒng)異常:", ex); return ResultUtils.error("系統(tǒng)發(fā)生異常,請聯(lián)系管理員"); } /** * 系統(tǒng)異常處理 */ @ExceptionHandler(Throwable.class) @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) public BaseResponse exception(Throwable throwable) { log.error("系統(tǒng)異常", throwable); return ResultUtils.error(HttpStatus.INTERNAL_SERVER_ERROR, "系統(tǒng)異常,請聯(lián)系管理員!"); } }
在上述方法中,對一些常見的異常進行了統(tǒng)一處理。 通常情況下,根據業(yè)務需要還會定義業(yè)務異常,并對業(yè)務異常進行處理,大家可以根據自己項目中異常的使用情況進行拓展。
關于@RestControllerAdvice的幾點說明:
@RestControllerAdvice注解包含了@Component注解,會把被注解的類作為組件交給Spring來管理。
@RestControllerAdvice注解包含了@ResponseBody注解,異常處理完之后給調用方輸出一個JSON格式的封裝數據。
@RestControllerAdvice注解有一個basePackages屬性,該屬性用來攔截哪個包中的異常信息,一般不指定,攔截項目工程中的所有異常。
在方法上通過@ExceptionHandler注解來指定具體的異常,在方法中處理該異常信息,最后將結果通過統(tǒng)一的JSON結構體返回給調用者。
重構接口
@RestController public class TestController { @RequestMapping("/calc") public ResponseInfo<String> calc(Integer id) { // 模擬異常業(yè)務代碼 int num = 1 / id; log.info("計算結果num={}", num); return ResultUtils.success(); } }
在請求的時候,不傳遞id值,即在瀏覽器中訪問:
{ "code": 500, "data": "空指針異常", "message": "", "description": "" }
可以看到統(tǒng)一異常處理對空指針異常進行了攔截處理,并返回了ExceptionHandlerAdvice中定義的統(tǒng)一報文格式。
小結
在使用SpringBoot或其他項目中,統(tǒng)一的報文格式和統(tǒng)一的異常處理都是必須的。 本篇文章介紹了基于SpringBoot的實現(xiàn),如果你的項目中采用了其他的技術棧,則可考慮對應的處理方式。 同時,日常中很多類似的功能都可以統(tǒng)一進行處理,避免大量無效的硬編碼。
到此這篇關于SpringBoot封裝響應處理超詳細講解的文章就介紹到這了,更多相關SpringBoot封裝響應處理內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Spring WebFlux實現(xiàn)參數校驗的示例代碼
請求參數校驗,在實際的應用中很常見,網上的文章大部分提供的使用注解的方式做參數校驗。本文主要介紹 Spring Webflux Function Endpoint 使用 Spring Validation 來校驗請求的參數。感興趣的可以了解一下2021-08-08springboot如何解決跨域后session獲取不到sessionId不一致
這篇文章主要介紹了springboot如何解決跨域后session獲取不到sessionId不一致問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01Java中ShardingSphere 數據分片的實現(xiàn)
其實很多人對分庫分表多少都有點恐懼,我們今天用ShardingSphere 給大家演示數據分片,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-09-09基于spring實現(xiàn)websocket實時推送實例
這篇文章主要為大家詳細介紹了基于spring實現(xiàn)websocket實時推送實例,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-03-03