日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

SpringBoot按照restful风格统一异常返回

發(fā)布時間:2023/12/15 javascript 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SpringBoot按照restful风格统一异常返回 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

?

小白一枚,最近使用restful風(fēng)格給前端返回數(shù)據(jù),但有時候后臺出異常了,返回的內(nèi)容不是封裝好的返回結(jié)果。導(dǎo)致前端渲染過程中出現(xiàn)連鎖問題。搞得人頭疼,而且看網(wǎng)上的很多人也比較繁瑣,不清爽。既然自己解決了,所以在這里記錄一下。希望幫到更多的小白。(我今天才知道這里居然可以上傳代碼段,以前寫的都是截圖+復(fù)制,樣式太丑了)

在實(shí)際項(xiàng)目中,正常情況下前端拿到的數(shù)據(jù)格式為為下圖:

正常格式

不想要的格式:當(dāng)某段代碼出異常,如果不做任何處理,返回的內(nèi)容是springboot自帶的返回格式。如圖:

不想要的格式

那么在這種情況下,如果前端的容錯不夠的話,有可能會使頁面“爆炸”。

所以我們想,如果代碼出異常時,返回給前端的格式然后message中加入實(shí)際的錯誤信息,定是極好的。如圖:

希望的格式

所以這篇博客的作用就是從零開始教大家如何達(dá)成上述的目的

1.首先建立統(tǒng)一的異常處理類CommonException,注意這里一定要繼承RuntimeException

package com.exception.exception;/*** @author Shiyx* @date 2019/9/4*/ public class CommonException extends RuntimeException {String message ;@Overridepublic String getMessage() {return message;}public void setMessage(String message) {this.message = message;}public CommonException(String message) {this.message = message;} }

其次建立異常返回格式的CommonExceptionHandler

package com.exception.exception;import com.exception.constant.StatusCode; import com.exception.entity.ResponseModel; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseBody;/*** @author Shiyx* @date 2019/9/4*/@ResponseBody @ControllerAdvice public class CommonExceptionHandler {@ExceptionHandlerprivate ResponseModel commonExceptionResult(CommonException commonException){return ResponseModel.fail(StatusCode.NOTAVAILABLE,commonException.getMessage());}}

以上兩個文件是最主要的,當(dāng)我們建立好上述的類之后,就可以來驗(yàn)證了

package com.exception.service.Impl;import com.exception.exception.CommonException; import com.exception.service.CommonService; import org.springframework.stereotype.Service;/*** @author Shiyx* @date 2019/9/4*/ @Service public class CommonServiceImpl implements CommonService {/*** 正常情況* @return*/@Overridepublic Integer numberCurrent() {int i = 1;Integer j = 10;Integer m = null;try {m = j / i;} catch (Exception e) {throw e;}return m;}/*** 異常不處理情況* @return*/@Overridepublic Integer number() {int i = 0;Integer j = 10;Integer m = j / i;return m;}/*** 自定義異常攔截* @return*/@Overridepublic Integer numberThrowCommonException() {int i = 0;Integer j = 10;Integer m = null;try {m = j / i;} catch (Exception e) {//咱們可以再其他出異常的地方同樣使用此段代碼。比如: // throw new CommonException("密碼錯誤"); // throw new CommonException("查詢條件錯誤"); // throw new CommonException("系統(tǒng)異常");throw new CommonException("除數(shù)不能為0");}return m;} }

至此,就搞定了,然后加上其他的一些返回實(shí)體類,Controller方法等,就能夠?qū)崿F(xiàn)我最上面截圖返回格式

其他的代碼不知道有沒有用,反正我都穿一下。

項(xiàng)目目錄結(jié)構(gòu)如下圖,標(biāo)準(zhǔn)的spring的開發(fā)目錄:

package com.exception.constant;/*** ProjectName: AlphaZ* PackageName: com.AlphaZ.constant* User: C0dEr* Date: 2016-11-10* Time: 14:51* Description:*/ public class StatusCode {public static final int AVAILABLE = 0;//成功public static final int NOTAVAILABLE = 1;//失敗} package com.exception.controller;import com.exception.constant.StatusCode; import com.exception.entity.ResponseModel; import com.exception.service.CommonService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController;/*** @author Shiyx* @date 2019/9/4*/@RestController public class CommonController {@Autowiredprivate CommonService commonService;@GetMapping("/number")public ResponseModel number(){Integer number = commonService.number();return ResponseModel.sucess(StatusCode.AVAILABLE,"操作成功",number);}@GetMapping("/numberCurrent")public ResponseModel numberCurrent(){Integer number = commonService.numberCurrent();return ResponseModel.sucess(StatusCode.AVAILABLE,"操作成功",number);}@GetMapping("/numberThrowCommonException")public ResponseModel numberThrowCommonException(){Integer number = commonService.numberThrowCommonException();return ResponseModel.sucess(StatusCode.AVAILABLE,"操作成功",number);}} package com.exception.entity;import com.exception.constant.StatusCode;/*** ProjectName: YouChi* PackageName: com.AlphaZ.entity* User: C0dEr* Date: 2016-11-04* Time: 10:53* Description:*/ public class ResponseModel<T> {public Integer statusCode = StatusCode.NOTAVAILABLE;public String httpStatus = "200";public String message;public T data;public ResponseModel(Integer statusCode, String httpStatus, String message, T data) {this.statusCode = statusCode;this.httpStatus = httpStatus;this.message = message;this.data = data;}public ResponseModel(String message, Integer statusCode, T data) {this.statusCode = statusCode;this.message = message;this.data = data;}public ResponseModel(Integer statusCode, String message) {this.statusCode = statusCode;this.message = message;this.data = null;}public static <T> ResponseModel fail(Integer statusCode,String message){return new ResponseModel(statusCode,message);}public static <T> ResponseModel sucess(Integer statusCode,String message,T data){return new ResponseModel(message,statusCode,data);}public ResponseModel() {}public String getMessage() {return message;}public void setMessage(String message) {this.message = message;}public Integer getStatusCode() {return statusCode;}public void setStatusCode(Integer statusCode) {this.statusCode = statusCode;}public T getData() {return data;}public void setData(T data) {this.data = data;}public String getHttpStatus() {return httpStatus;}public void setHttpStatus(String httpStatus) {this.httpStatus = httpStatus;}@Overridepublic String toString() {return "ResponseModel{" +"statusCode=" + statusCode +", httpStatus='" + httpStatus + '\'' +", message='" + message + '\'' +", data=" + data +'}';}} package com.exception.service;/*** @author Shiyx* @date 2019/9/4*/ public interface CommonService {Integer number();Integer numberCurrent();Integer numberThrowCommonException();}

?

總結(jié)

以上是生活随笔為你收集整理的SpringBoot按照restful风格统一异常返回的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。