這篇文章主要介紹了SpringBoot統(tǒng)一響應體解決方案的示例分析,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

創(chuàng)新互聯(lián)從2013年創(chuàng)立,是專業(yè)互聯(lián)網(wǎng)技術服務公司,擁有項目成都網(wǎng)站建設、網(wǎng)站制作網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元象山做網(wǎng)站,已為上家服務,為象山各地企業(yè)和個人服務,聯(lián)系電話:13518219792
最近在優(yōu)化自己之前基于Spring AOP的統(tǒng)一響應體的實現(xiàn)方案。
什么是統(tǒng)一響應體呢?在目前的前后端分離架構下,后端主要是一個RESTful API的數(shù)據(jù)接口。
但是HTTP的狀態(tài)碼數(shù)量有限,而隨著業(yè)務的增長,HTTP狀態(tài)碼無法很好地表示業(yè)務中遇到的異常情況。
那么可以通過修改響應返回的JSON數(shù)據(jù),讓其帶上一些固有的字段,例如以下這樣的
{
"code": 10000,
"msg": "success",
"data": {
"id": 2,
"name": "test"
}
}其中關鍵屬性的用途如下:
code為返回結果的狀態(tài)碼
msg為返回結果的消息
data為返回的業(yè)務數(shù)據(jù)
這3個屬性為固有屬性,每次響應結果都會有帶有它們。
需求
希望實現(xiàn)一個能夠代替基于AOP的實現(xiàn)方案,需要滿足以下幾點:
原有的基于AOP的實現(xiàn)方案需要Controller的返回類型為Object,需要新方案不限制返回類型
原有的基于AOP的實現(xiàn)方案需要通過切面表達式+注解控制切點的Controller(注解的包名修改會導致切面表達式的修改,即需要修改兩處地方),需要新方案能夠基于注解,而不需要修改切面表達式
方案思路
基于上述的需求,選擇使用Spring的Controller增強機制,其中關鍵的類為以下3個:
@ControllerAdvice:類注解,用于指定Controller增強處理器類。
ResponseBodyAdvice:接口,實現(xiàn)后beforeBodyWrite()方法后可以對響應的body進行修改,需要結合@ControllerAdvice使用。
@ExceptionHandler:方法注解,用于指定異常處理方法,需要結合@ControllerAdvice和@ResponseBody使用。
示例關鍵代碼
本示例使用的Spring Boot版本為2.1.6.RELEASE,同時需要開發(fā)工具安裝lombok插件
引入依賴
<dependencies> <!--web-starter--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <!--test-starter--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
統(tǒng)一響應體
Controller增強后統(tǒng)一響應體對應的對象
import lombok.AllArgsConstructor;
import lombok.Data;
import java.io.Serializable;
/**
* 統(tǒng)一的公共響應體
* @author NULL
* @date 2019-07-16
*/
@Data
@AllArgsConstructor
public class ResponseResult implements Serializable {
/**
* 返回狀態(tài)碼
*/
private Integer code;
/**
* 返回信息
*/
private String msg;
/**
* 數(shù)據(jù)
*/
private Object data;
}統(tǒng)一響應注解
統(tǒng)一響應注解是一個標記是否開啟統(tǒng)一響應增強的注解
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 統(tǒng)一響應注解<br/>
* 添加注解后,統(tǒng)一響應體才能生效
* @author NULL
* @date 2019-07-16
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface BaseResponse {
}狀態(tài)碼枚舉
統(tǒng)一響應體中返回的狀態(tài)碼code和狀態(tài)信息msg對應的枚舉類
/**
* 返回狀態(tài)碼
*
* @author NULL
* @date 2019-07-16
*/
public enum ResponseCode {
/**
* 成功返回的狀態(tài)碼
*/
SUCCESS(10000, "success"),
/**
* 資源不存在的狀態(tài)碼
*/
RESOURCES_NOT_EXIST(10001, "資源不存在"),
/**
* 所有無法識別的異常默認的返回狀態(tài)碼
*/
SERVICE_ERROR(50000, "服務器異常");
/**
* 狀態(tài)碼
*/
private int code;
/**
* 返回信息
*/
private String msg;
ResponseCode(int code, String msg) {
this.code = code;
this.msg = msg;
}
public int getCode() {
return code;
}
public String getMsg() {
return msg;
}
}業(yè)務異常類
業(yè)務異常類是用于識別業(yè)務相關的異常,需要注意這個異常類強制需要以ResponseCode作為構造方法入?yún)ⅲ@樣可以通過捕獲異常獲得返回的狀態(tài)碼信息
import com.rjh.web.response.ResponseCode;
import lombok.Data;
import lombok.EqualsAndHashCode;
/**
* 業(yè)務異常類,繼承運行時異常,確保事務正常回滾
*
* @author NULL
* @since 2019-07-16
*/
@Data
@EqualsAndHashCode(callSuper = false)
public class BaseException extends RuntimeException{
private ResponseCode code;
public BaseException(ResponseCode code) {
this.code = code;
}
public BaseException(Throwable cause, ResponseCode code) {
super(cause);
this.code = code;
}
}異常處理類
用于處理Controller運行時未捕獲的異常的處理類。
import com.rjh.web.exception.BaseException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* 異常處理器
*
* @author NULL
* @since 2019-07-16
*/
@ControllerAdvice(annotations = BaseResponse.class)
@ResponseBody
@Slf4j
public class ExceptionHandlerAdvice {
/**
* 處理未捕獲的Exception
* @param e 異常
* @return 統(tǒng)一響應體
*/
@ExceptionHandler(Exception.class)
public ResponseResult handleException(Exception e){
log.error(e.getMessage(),e);
return new ResponseResult(ResponseCode.SERVICE_ERROR.getCode(),ResponseCode.SERVICE_ERROR.getMsg(),null);
}
/**
* 處理未捕獲的RuntimeException
* @param e 運行時異常
* @return 統(tǒng)一響應體
*/
@ExceptionHandler(RuntimeException.class)
public ResponseResult handleRuntimeException(RuntimeException e){
log.error(e.getMessage(),e);
return new ResponseResult(ResponseCode.SERVICE_ERROR.getCode(),ResponseCode.SERVICE_ERROR.getMsg(),null);
}
/**
* 處理業(yè)務異常BaseException
* @param e 業(yè)務異常
* @return 統(tǒng)一響應體
*/
@ExceptionHandler(BaseException.class)
public ResponseResult handleBaseException(BaseException e){
log.error(e.getMessage(),e);
ResponseCode code=e.getCode();
return new ResponseResult(code.getCode(),code.getMsg(),null);
}
}響應增強類
Conrtoller增強的統(tǒng)一響應體處理類,需要注意異常處理類已經(jīng)進行了增強,所以需要判斷一下返回的對象是否為統(tǒng)一響應體對象。
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
/**
* 統(tǒng)一響應體處理器
* @author NULL
* @date 2019-07-16
*/
@ControllerAdvice(annotations = BaseResponse.class)
@Slf4j
public class ResponseResultHandlerAdvice implements ResponseBodyAdvice {
@Override
public boolean supports(MethodParameter returnType, Class converterType) {
log.info("returnType:"+returnType);
log.info("converterType:"+converterType);
return true;
}
@Override
public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
if(MediaType.APPLICATION_JSON.equals(selectedContentType) || MediaType.APPLICATION_JSON_UTF8.equals(selectedContentType)){ // 判斷響應的Content-Type為JSON格式的body
if(body instanceof ResponseResult){ // 如果響應返回的對象為統(tǒng)一響應體,則直接返回body
return body;
}else{
// 只有正常返回的結果才會進入這個判斷流程,所以返回正常成功的狀態(tài)碼
ResponseResult responseResult =new ResponseResult(ResponseCode.SUCCESS.getCode(),ResponseCode.SUCCESS.getMsg(),body);
return responseResult;
}
}
// 非JSON格式body直接返回即可
return body;
}
}使用示例
首先準備一個User對象
import lombok.Data;
import lombok.EqualsAndHashCode;
import java.io.Serializable;
/**
* 用戶類
* @author NULL
* @date 2019-07-16
*/
@Data
@EqualsAndHashCode
public class User implements Serializable {
private Integer id;
private String name;
}然后是準備一個簡單的UserController即可
import com.rjh.web.entity.User;
import com.rjh.web.exception.BaseException;
import com.rjh.web.response.BaseResponse;
import com.rjh.web.response.ResponseCode;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 測試用的Controller
*
* @author NULL
* @date 2019-07-16
*/
@BaseResponse
@RestController
@RequestMapping("users")
public class UserController {
@GetMapping("/{userId}")
public User getUserById(@PathVariable Integer userId){
if(userId.equals(0)){
throw new BaseException(ResponseCode.RESOURCES_NOT_EXIST);
}
if(userId.equals(1)){
throw new RuntimeException();
}
User user=new User();
user.setId(userId);
user.setName("test");
return user;
}
}運行結果
在瀏覽器直接訪問http://127.0.0.1:8080/users/0,則返回結果如下(結果經(jīng)過格式化處理):
{
"code": 10001,
"msg": "資源不存在",
"data": null
}在瀏覽器直接訪問http://127.0.0.1:8080/users/1,則返回結果如下(結果經(jīng)過格式化處理):
{
"code": 50000,
"msg": "服務器異常",
"data": null
}在瀏覽器直接訪問http://127.0.0.1:8080/users/2,則返回結果如下(結果經(jīng)過格式化處理):
{
"code": 10000,
"msg": "success",
"data": {
"id": 2,
"name": "test"
}
}由運行結果可以得知統(tǒng)一響應增強其實已經(jīng)生效了,而且能夠很好的處理異常。
感謝你能夠認真閱讀完這篇文章,希望小編分享的“SpringBoot統(tǒng)一響應體解決方案的示例分析”這篇文章對大家有幫助,同時也希望大家多多支持創(chuàng)新互聯(lián),關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關知識等著你來學習!
網(wǎng)頁標題:SpringBoot統(tǒng)一響應體解決方案的示例分析
標題路徑:http://www.chinadenli.net/article0/isjeoo.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供標簽優(yōu)化、網(wǎng)站維護、虛擬主機、營銷型網(wǎng)站建設、云服務器、網(wǎng)站導航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)