Spring Boot 捕捉全局異常 統(tǒng)一返回值的問題
在前后端分離的情況下,我們經(jīng)常會定義一個統(tǒng)一的反回數(shù)據(jù)格式,通常都會包含狀態(tài)碼,返回信息,返回的數(shù)據(jù),是否成功等參數(shù)。
1、ResultCode
單獨定義了一個ReturnCode枚舉類用于存儲代碼和返回的Message
public enum ResultCode {
//成功
SUCCESS(200),
// 失敗
FAIL(400),
// 未認證(簽名錯誤)
UNAUTHORIZED(401),
// 接口不存在
NOT_FOUND(404),
// 服務器內(nèi)部錯誤
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;
}
}
在定義一個統(tǒng)一返回類:
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;
@Service
public 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[]{"測試"};
return Response.makeOKRsp(user);
}catch (Exception e)
{
return Response.makeErrRsp("查詢用戶信息異常");
}
}
返回結果:

7、全局異常處理器
/**
* 全局異常處理
*/
@RestControllerAdvice
public 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 捕捉全局異常 統(tǒng)一返回值的文章就介紹到這了,更多相關Spring Boot 捕捉全局異常 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
圖解Java經(jīng)典算法冒泡排序的原理與實現(xiàn)
冒泡排序是一種簡單的排序算法,它也是一種穩(wěn)定排序算法。其實現(xiàn)原理是重復掃描待排序序列,并比較每一對相鄰的元素,當該對元素順序不正確時進行交換。一直重復這個過程,直到?jīng)]有任何兩個相鄰元素可以交換,就表明完成了排序2022-09-09
Java之SpringBoot集成ActiveMQ消息中間件案例講解
這篇文章主要介紹了Java之SpringBoot集成ActiveMQ消息中間件案例講解,本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-07-07
Java ArrayList與LinkedList使用方法詳解
Java中容器對象主要用來存儲其他對象,根據(jù)實現(xiàn)原理不同,主要有3類常用的容器對象:ArrayList使用數(shù)組結構存儲容器中的元素、LinkedList使用鏈表結構存儲容器中的元素2022-11-11
springboot?vue項目管理后端實現(xiàn)接口新增
這篇文章主要為大家介紹了springboot?vue項目管理后端實現(xiàn)接口新增,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-05-05

