色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Spring Boot 捕捉全局異常 統(tǒng)一返回值的問(wèn)題

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

在前后端分離的情況下,我們經(jīng)常會(huì)定義一個(gè)統(tǒng)一的反回?cái)?shù)據(jù)格式,通常都會(huì)包含狀態(tài)碼,返回信息,返回的數(shù)據(jù),是否成功等參數(shù)。

1、ResultCode

單獨(dú)定義了一個(gè)ReturnCode枚舉類(lèi)用于存儲(chǔ)代碼和返回的Message

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

2、ResponseResult

/* 統(tǒng)一返回信息 */public class ResponseResult<T> { public int code; //返回狀態(tài)碼200成功 private String msg; //返回描述信息 private T data; //返回內(nèi)容體 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; } }

在定義一個(gè)統(tǒng)一返回類(lèi):

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

新建測(cè)試用戶(hù)接口類(lèi)

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

5、新建 UserServiceImpl

新建測(cè)試用戶(hù)信息服務(wù)類(lèi)

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調(diào)用

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

返回結(jié)果:

Spring Boot 捕捉全局異常 統(tǒng)一返回值的問(wèn)題

7、全局異常處理器

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

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

Spring Boot 捕捉全局異常 統(tǒng)一返回值的問(wèn)題

總結(jié)

到此這篇關(guān)于Spring Boot 捕捉全局異常 統(tǒng)一返回值的文章就介紹到這了,更多相關(guān)Spring Boot 捕捉全局異常 內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 欧美毛片性视频区 | 欧洲女同互慰在线视频 | 99久久成人 | 久久w5ww成w人免费不卡 | 一级毛片aaa片免费观看 | 久久精品国产一区二区三区不卡 | 国产麻豆一级在线观看 | 1769视频在线观看国产 | 国产高清第一页 | 成人精品在线 | www看片| 色婷婷激婷婷深爱五月老司机 | 久久久久久久久免费影院 | 亚洲国产精品一区二区三区在线观看 | 亚洲精品一区亚洲精品 | 国产欧美va欧美va香蕉在线 | 一级黄色美女视频 | 91久久香蕉青青草原娱乐 | 亚州免费视频 | 国产精品一区在线播放 | 国产手机精品视频 | 欧美一级xxxx俄罗斯一级 | 国产美女在线精品亚洲二区 | 久久精品国产线看观看亚洲 | 99久99久6久热在线播放 | 高清性色生活片欧美在线 | 日本欧美一区二区三区高清 | 精品欧美一区二区精品久久 | 国内视频自拍 | 在线观看欧美亚洲日本专区 | 国产精品一区二区资源 | 日韩精品在线免费观看 | 亚洲人成人毛片无遮挡 | 国产 高清 在线 | 经典香港一级a毛片免费看 精品400部自拍视频在线播放 | av18在线播放 | 一级高清毛片免费a级高清毛片 | 九九免费在线视频 | 最近免费手机中文字幕3 | 亚洲国产cao| 男人天堂网在线观看 |