国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

Spring Boot 捕捉全局異常 統一返回值的問題

瀏覽:73日期:2023-09-01 18:58:42

在前后端分離的情況下,我們經常會定義一個統一的反回數據格式,通常都會包含狀態碼,返回信息,返回的數據,是否成功等參數。

1、ResultCode

單獨定義了一個ReturnCode枚舉類用于存儲代碼和返回的Message

public enum ResultCode { //成功 SUCCESS(200), // 失敗 FAIL(400), // 未認證(簽名錯誤) UNAUTHORIZED(401), // 接口不存在 NOT_FOUND(404), // 服務器內部錯誤 INTERNAL_SERVER_ERROR(500); public int code; ResultCode(int code) { this.code=code; }}

2、ResponseResult

/* 統一返回信息 */public class ResponseResult<T> { public int code; //返回狀態碼200成功 private String msg; //返回描述信息 private T data; //返回內容體 public ResponseResult<T> setCode(ResultCode retCode) { this.code = retCode.code; return this; } public int getCode() { return code; } public ResponseResult<T> setCode(int code) { this.code = code; return this; } public String getMsg() { return msg; } public ResponseResult<T> setMsg(String msg) { this.msg = msg; return this; } public T getData() { return data; } public ResponseResult<T> setData(T data) { this.data = data; return this; } }

在定義一個統一返回類:

3、Response

public class Response { private final static String SUCCESS = 'success'; private final static String FAIL = 'fail'; public static <T> ResponseResult<T> makeOKRsp() { return new ResponseResult<T>().setCode(ResultCode.SUCCESS).setMsg(SUCCESS); } public static <T> ResponseResult<T> makeOKRsp(String message) { return new ResponseResult<T>().setCode(ResultCode.SUCCESS).setMsg(message); } public static <T> ResponseResult<T> makeOKRsp(T data) { return new ResponseResult<T>().setCode(ResultCode.SUCCESS).setMsg(SUCCESS).setData(data); } public static <T> ResponseResult<T> makeErrRsp(String message) { return new ResponseResult<T>().setCode(ResultCode.INTERNAL_SERVER_ERROR).setMsg(message); } public static <T> ResponseResult<T> makeRsp(int code, String msg) { return new ResponseResult<T>().setCode(code).setMsg(msg); } public static <T> ResponseResult<T> makeRsp(int code, String msg, T data) { return new ResponseResult<T>().setCode(code).setMsg(msg).setData(data); }}

4、新建 IUserService

新建測試用戶接口類

package com.example.demo.service;import com.example.demo.entity.User;public interface IUserService { public User getUserInfo();}

5、新建 UserServiceImpl

新建測試用戶信息服務類

package com.example.demo.service.impl;import com.example.demo.entity.User;import com.example.demo.service.UserService;import org.springframework.stereotype.Service; import java.io.File;import java.text.SimpleDateFormat;import java.util.Date;import java.util.logging.SimpleFormatter; @Servicepublic class UserServiceImpl implements IUserService { public User getUserInfo(){ User user = new User(); user.setName('jack'); user.setPassword(12341234); return user; } }

6、在controller調用

@Autowired UserService service; @RequestMapping(value = '/getUserItem',method = RequestMethod.GET) public ResponseResult<User> getUserItem(){ try { User user = service.getUserInfo(); String[] arr= new String[]{'測試'}; return Response.makeOKRsp(user); }catch (Exception e) { return Response.makeErrRsp('查詢用戶信息異常'); } }

返回結果:

Spring Boot 捕捉全局異常 統一返回值的問題

7、全局異常處理器

/** * 全局異常處理 */@RestControllerAdvicepublic class GlobalExceptionHandler { /*============= 請求錯誤 start ==============================================*/ /** * HTTP 請求方式不支持異常 * HttpRequestMethodNotSupportedException * @return {@link ResponseResult} */ @ExceptionHandler(value = HttpRequestMethodNotSupportedException.class) public ResponseResult httpRequestMethodNotSupportException(HttpRequestMethodNotSupportedException e, HttpServletRequest request) { return Response.makeErrRsp('請求方式不支持異常'); } /*============= 請求錯誤 end ==============================================*/ }

修改一下getUserItem讓其拋出自定義查詢返回null的異常:

Spring Boot 捕捉全局異常 統一返回值的問題

總結

到此這篇關于Spring Boot 捕捉全局異常 統一返回值的文章就介紹到這了,更多相關Spring Boot 捕捉全局異常 內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Spring
相關文章:
主站蜘蛛池模板: 国产成年女一区二区三区 | 一区二区三区免费视频播放器 | 最刺激黄a大片免费网站 | 日韩中文字幕在线看 | 亚欧精品一区二区三区 | 欧美性色黄大片一级毛片视频 | 99久久精品费精品国产一区二 | 免费一级毛片在线观看 | 国产高清a毛片在线看 | 国产精选经典三级小泽玛利亚 | 玖玖在线免费视频 | 中文字幕日韩一区二区 | 欧美亚洲国产精品 | 欧美私人网站 | 国内免费自拍视频 | heyzo国产亚洲高清 | 国内一区| 亚洲综合网在线 | 亚洲视频aaa | 萌白酱香蕉白丝护士服喷浆 | 国产一区亚洲二区三区毛片 | 一级特黄特色的免费大片视频 | 色综合久久一本首久久 | 精品久久九九 | 男女视频在线观看免费 | 国产在线观看精品 | 国产一区二区三区在线观看免费 | 日韩一级大毛片欧美一级 | 91精品国产91 | 色综合天天综合网看在线影院 | 狠狠五月深爱婷婷网 | 一级做a爰片久久毛片潮喷 一级做a爰片久久毛片美女 | 久草久草视频 | 日本在线看小视频网址 | 一本久道在线 | 中国内地毛片免费高清 | 欧美人性影片免费看 | 欧美一区二区三区视频在线 | 日韩一区二区三区在线免费观看 | 国产成人综合久久亚洲精品 | 欧美精品a毛片免费观看 |