spring項目自定義全局響應處理器統(tǒng)一處理響應結果的實現(xiàn)步驟
實現(xiàn)方式
使用@ControllerAdvice注解和實現(xiàn)ResponseBodyAdvice接口自定義全局響應處理器
實現(xiàn)步驟
自定義統(tǒng)一返回類
定義統(tǒng)一返回類,包裝返回結果
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)//過濾null,提高性能
@ApiModel(value = "返回類")
public class Wrapper<T> implements Serializable {
/**
* 成功碼
*/
public static final int SUCCESS_CODE = 200;
/**
* 成功信息
*/
public static final String SUCCESS_MESSAGE = "操作成功";
/**
* 錯誤碼
*/
public static final int ERROR_CODE = 500;
/**
* 錯誤信息
*/
public static final String ERROR_MESSAGE = "內(nèi)部異常";
/**
* 編號
*/
@ApiModelProperty("編號")
private int code;
/**
* 信息
*/
@ApiModelProperty("信息")
private String message;
/**
* 結果數(shù)據(jù)
*/
@ApiModelProperty("結果數(shù)據(jù)")
private T result;
/** 為什么使用this替代new類名
* 代碼簡潔:使用 this 可以使得代碼更加簡潔,因為你不需要每次都重復使用類的名稱。
* 明確性:通過使用 this,你清楚地表明你正在調(diào)用當前類的另一個構造函數(shù)或方法,而不是其他類的構造函數(shù)或方法。
* 減少重復:在多構造函數(shù)的類中,如果每個構造函數(shù)都需要設置相同的屬性,那么使用 this 可以避免重復代碼。
* 提高可讀性:當一個構造函數(shù)調(diào)用了另一個構造函數(shù)時,使用 this 可以讓讀者更容易理解正在發(fā)生的事情,尤其是當構造函數(shù)的參數(shù)不同,但都包含某些共同參數(shù)時。
*/
//默認構造函數(shù),它直接調(diào)用第二個構造函數(shù),并傳遞 SUCCESS_CODE 和 SUCCESS_MESSAGE 作為參數(shù)。
Wrapper() {
this(SUCCESS_CODE, SUCCESS_MESSAGE);
}
//不需要返回數(shù)據(jù) 帶有兩個參數(shù)的構造函數(shù),它調(diào)用第三個構造函數(shù),并傳遞相同的參數(shù)。
Wrapper(int code, String message) {
this(code, message, null);
}
//需要返回數(shù)據(jù)
Wrapper(int code, String message, T result) {
super();
this.code(code).message(message).result(result);
}
private Wrapper<T> code(int code) {
this.setCode(code);
return this;
}
private Wrapper<T> message(String message) {
this.setMessage(message);
return this;
}
public Wrapper<T> result(T result) {
this.setResult(result);
return this;
}
@JsonIgnore
public boolean success() {
return Wrapper.SUCCESS_CODE == this.code;
}
@JsonIgnore
public boolean error() {
return !success();
}
}定義 統(tǒng)一返回包裝類
public class WrapMapper {
/**
* 在 `WrapMapper` 類中,私有構造函數(shù)的設計是為了防止外部實例化這個類。這是一個常見的做法,有以下幾個原因:
* 1. **單一職責原則**:`WrapMapper` 類被設計為一個工具類,只提供一系列靜態(tài)方法,用于創(chuàng)建和處理 `Wrapper` 對象。它不包含任何實例變量,因此它沒有必要被實例化。
* 2. **減少依賴**:如果 `WrapMapper` 類可以被實例化,那么任何使用這個類的代碼都需要導入這個類的引用。通過將構造函數(shù)設為私有,你可以限制它的使用,只允許通過靜態(tài)方法來使用這個類。
* 3. **提高效率**:工具類通常不需要創(chuàng)建多個實例,因為它們不依賴于外部狀態(tài),而且它們的操作通??梢酝ㄟ^靜態(tài)方法完成。因此,設計為不可實例化可以提高效率。
* 4. **清晰意圖**:通過將構造函數(shù)設為私有,明確地表明這個類不應該被實例化,這有助于其他開發(fā)者理解這個類的用途和設計意圖。
* 總之,將 `WrapMapper` 類的構造函數(shù)設為私有是為了遵循單一職責原則,減少依賴,提高效率,并提高代碼的可讀性和可維護性。這樣的設計有助于確保 `WrapMapper` 類只作為工具類被使用,而不被誤用或濫用。
*/
private WrapMapper() {
}
public static <E> Wrapper<E> wrap(int code) {
return wrap(code, null);
}
public static <E> Wrapper<E> wrap(int code, String message) {
return wrap(code, message, null);
}
public static <E> Wrapper<E> wrap(int code, String message, E o) {
return new Wrapper<>(code, message, o);
}
public static <E> Wrapper<E> wrap(Exception e) {
return new Wrapper<>(Wrapper.ERROR_CODE, e.getMessage());
}
public static <E> E unWrap(Wrapper<E> wrapper) {
return wrapper.getResult();
}
public static <E> Wrapper<E> error() {
return wrap(Wrapper.ERROR_CODE, Wrapper.ERROR_MESSAGE);
}
public static <E> Wrapper<E> error(int code,String message) {
return wrap(code, message);
}
public static <E> Wrapper<E> error(String message) {
return wrap(Wrapper.ERROR_CODE, StringUtils.isBlank(message) ? Wrapper.ERROR_MESSAGE : message);
}
public static <E> Wrapper<E> ok() {
return new Wrapper<>();
}
public static <E> Wrapper<E> ok(E o) {
return new Wrapper<>(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, o);
}
}定義啟用全局響應注解
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface EnableResponseHandler {
}定義忽略全局響應注解
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface IgnoreResponseHandler {
}定義全局響應處理器
//@Component標記的類,Spring 容器會自動掃描這個類,并將它作為一個 bean 創(chuàng)建和注冊到 Spring 容器中。
//可以被其他類通過 @Autowired 注解自動裝配,你不需要顯式地定義 bean 的依賴關系,Spring 容器會自動處理。
@Component
@ControllerAdvice//全局控制器增強
public class GlobalResponseHandler implements ResponseBodyAdvice<Object> {
@Override
public boolean supports(MethodParameter methodParameter, Class<? extends HttpMessageConverter<?>> aClass) {
if (methodParameter.getMethod().isAnnotationPresent(IgnoreResponseHandler.class)) {
return false;
}
//只有EnableResponseHandler注解標識的類才會去調(diào)用beforeBodyWrite方法
if (methodParameter.getMethod().isAnnotationPresent(EnableResponseHandler.class)) {
return true;
}
if (methodParameter.getDeclaringClass().isAnnotationPresent(EnableResponseHandler.class)) {
return true;
}
return false;
}
/**
*beforeBodyWrite方法在 Spring MVC 將返回的對象寫入 HTTP 響應之前被調(diào)用,它提供了一個機會來增強或修改響應體。
* 作用
* 1.增強響應:在返回對象被寫入響應之前,允許你對響應進行增強或修改。
* 自定義格式化:如果你需要對返回的對象進行特殊的格式化,比如添加額外的頭信息,或者修改對象的屬性,beforeBodyWrite 提供了這種能力。
* 全局應用:你可以定義多個 ResponseBodyAdvice 實現(xiàn)類,并將它們應用于整個應用程序,或者針對特定的 Controller 或請求類型進行過濾。
*/
@Nullable
@Override
public Object beforeBodyWrite(@Nullable Object o,
MethodParameter methodParameter,
MediaType mediaType,
Class<? extends HttpMessageConverter<?>> aClass,
ServerHttpRequest serverHttpRequest,
ServerHttpResponse serverHttpResponse) {
//如果返回的不是自己定義的包裝類
if (o instanceof Wrapper) {
return o;
}
//使用WrapMapper包裝后返回
return WrapMapper.ok(o);
}
}到此這篇關于spring項目自定義全局響應處理器,統(tǒng)一處理響應結果的文章就介紹到這了,更多相關spring自定義全局響應處理器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
IDEA連接mysql數(shù)據(jù)庫報錯的解決方法
這篇文章主要介紹了IDEA連接mysql數(shù)據(jù)庫報錯的解決方法,文中有非常詳細的圖文示例,對出現(xiàn)Server returns invalid timezone. Go to ‘Advanced‘ tab and set ‘serverTimezone‘ prope報錯的小伙伴們很有幫助喲,需要的朋友可以參考下2021-05-05
SpringBoot實現(xiàn)識別圖片中的身份證號與營業(yè)執(zhí)照信息
這篇文章主要為大家詳細介紹了SpringBoot如何實現(xiàn)識別圖片中的身份證號與營業(yè)執(zhí)照信息,文中的示例代碼講解詳細,感興趣的小伙伴可以了解一下2024-01-01
java使用ArrayList實現(xiàn)斗地主(無序版)
這篇文章主要為大家詳細介紹了java使用ArrayList實現(xiàn)斗地主,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-03-03
解決HttpPost+json請求---服務器中文亂碼及其他問題
這篇文章主要介紹了解決HttpPost+json請求---服務器中文亂碼及其他問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01
Springboot自帶定時任務實現(xiàn)動態(tài)配置Cron參數(shù)方式
這篇文章主要介紹了Springboot自帶定時任務實現(xiàn)動態(tài)配置Cron參數(shù)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11

