今天說一下 Spring Boot 如何實現優雅的數據響應:統一的結果響應格式、簡單的數據封裝。
前提
無論系統規模大小,大部分 Spring Boot 項目是提供 Restful + json 接口,供前端或其他服務調用,格式統一規范,是程序猿彼此善待彼此的象征,也是減少聯調挨罵的基本保障。
通常響應結果中需要包含業務狀態碼、響應描述、響應時間戳、響應內容,比如:
{
"code": 200,
"desc": "查詢成功",
"timestamp": "2020-08-12 14:37:11",
"data": {
"uid": "1597242780874",
"name": "測試 1"
}
}
對于業務狀態碼分為兩個派系:一個是推薦使用 HTTP 響應碼作為接口業務返回;另一種是 HTTP 響應碼全部返回 200,在響應體中通過單獨的字段表示響應狀態。兩種方式各有優劣,個人推薦使用第二種,因為很多 Web 服務器對 HTTP 狀態碼有攔截處理功能,而且狀態碼數量有限,不夠靈活。比如返回 200 表示接口處理成功且正常響應,現在需要有一個狀態碼表示接口處理成功且正常響應,但是請求數據狀態不對,可以返回 2001 表示。
自定義響應體
定義一個數據響應體是返回統一響應格式的第一步,無論接口正常返回,還是發生異常,返回給調用方的結構格式都應該不變。給出一個示例:
1
2
3
4
5
6
7
8
9
10
11
12
|
@ApiModel @Data public class Response<T> { @ApiModelProperty (value = "返回碼" , example = "200" ) private Integer code; @ApiModelProperty (value = "返回碼描述" , example = "ok" ) private String desc; @ApiModelProperty (value = "響應時間戳" , example = "2020-08-12 14:37:11" ) private Date timestamp = new Date(); @ApiModelProperty (value = "返回結果" ) private T data; } |
這樣,只要在 Controller 的方法返回Response
就可以了,接口響應就一致了,但是這樣會形成很多格式固定的代碼模板,比如下面這種寫法:
1
2
3
4
5
6
7
8
|
@RequestMapping ( "hello1" ) public Response<String> hello1() { final Response<String> response = new Response<>(); response.setCode( 200 ); response.setDesc( "返回成功" ); response.setData( "Hello, World!" ); return response; } |
調用接口響應結果為:
{
"code": 200,
"desc": "返回成功",
"timestamp": "2020-08-12 14:37:11","data": "Hello, World!"
}
這種重復且沒有技術含量的代碼,怎么能配得上程序猿這種優(lan)雅(duo)的生物呢?最好能在返回響應結果的前提下,減去那些重復的代碼,比如:
1
2
3
4
|
@RequestMapping ( "hello2" ) public String hello2() { return "Hello, World!" ; } |
這就需要借助 Spring 提供的ResponseBodyAdvice
來實現了。
全局處理響應數據
先上代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
/** * <br>created at 2020/8/12 * * @author www.howardliu.cn * @since 1.0.0 */ @RestControllerAdvice public class ResultResponseAdvice implements ResponseBodyAdvice<Object> { @Override public boolean supports( final MethodParameter returnType, final Class<? extends HttpMessageConverter<?>> converterType) { return !returnType.getGenericParameterType().equals(Response. class ); // 1 } @Override public Object beforeBodyWrite( final Object body, final MethodParameter returnType, final MediaType selectedContentType, final Class<? extends HttpMessageConverter<?>> selectedConverterType, final ServerHttpRequest request, final ServerHttpResponse response) { if (body == null || body instanceof Response) { return body; } final Response<Object> result = new Response<>(); result.setCode( 200 ); result.setDesc( "查詢成功" ); result.setData(body); if (returnType.getGenericParameterType().equals(String. class )) { // 2 ObjectMapper objectMapper = new ObjectMapper(); try { return objectMapper.writeValueAsString(result); } catch (JsonProcessingException e) { throw new RuntimeException( "將 Response 對象序列化為 json 字符串時發生異常" , e); } } return result; } } /** * <br>created at 2020/8/12 * * @author www.howardliu.cn * @since 1.0.0 */ @RestController public class HelloWorldController { @RequestMapping ( "hello2" ) public String hello2() { return "Hello, World!" ; } @RequestMapping ( "user1" ) public User user1() { User u = new User(); u.setUid(System.currentTimeMillis() + "" ); u.setName( "測試1" ); return u; } } |
上面代碼是實現了 Spring ResponseBodyAdvice
類的模板方式,按照 Spring 的要求實現就行。只有兩個需要特別注意的地方,也就是代碼中標注 1 和 2 的地方。
首先說 1 這一行,也就是supports
方法,這個方法是校驗是否需要調用beforeBodyWrite
方法的前置判斷,返回true
則執行beforeBodyWrite
方法,這里根據 Controller 方法返回類型來判斷是否需要執行beforeBodyWrite
,也可以一律返回true
,在后面判斷是否需要進行類型轉換。
然后重點說下 2 這一行,這行是坑,是大坑,如果對 Spring 結構不熟悉的,絕對會在這徘徊許久,不得妙法。
代碼 2 這一行是判斷Controller
的方法是否返回的是String
類型的結果,如果是,將返回的對象序列化之后返回。
這是因為Spring
對String
類型的響應類型單獨處理了,使用StringHttpMessageConverter
類進行數據轉換。在處理響應結果的時候,會在方法getContentLength
中計算響應體大小,其父類方法定義是protected Long getContentLength(T t, @Nullable MediaType contentType)
,而StringHttpMessageConverter
將方法重寫為protected Long getContentLength(String str, @Nullable MediaType contentType)
,第一個參數是響應對象,固定寫死是String
類型,如果我們強制返回Response
對象,就會報ClassCastException
。
當然,直接返回String
的場景不多,這個坑可能會在某天特殊接口中突然出現。
補充說明
上面只是展示了ResponseBodyAdvice
類最簡單的應用,我們還可以實現更多的擴展使用。比如:
-
返回請求ID:這個需要與與
RequestBodyAdvice
聯動,獲取到請求ID后,在響應是放在響應體中; -
結果數據加密:通過
ResponseBodyAdvice
實現響應數據加密,不會侵入業務代碼,而且可以通過注解方式靈活處理接口的加密等級; -
有選擇的包裝響應體:比如定義注解
IgnoreResponseWrap
,在不需要包裝響應體的接口上定義,然后在supports
方法上判斷方法的注解即可,比如:
1
2
3
4
5
|
@Override public boolean supports( final MethodParameter returnType, final Class<? extends HttpMessageConverter<?>> converterType) { final IgnoreResponseWrap[] declaredAnnotationsByType = returnType.getExecutable().getDeclaredAnnotationsByType(IgnoreResponseWrap. class ); return !(declaredAnnotationsByType.length > 0 || returnType.getGenericParameterType().equals(Response. class )); } |
很多其他玩法就不一一列舉了。
總結
上面說了正常響應的數據,只做到了一點優雅,想要完整,還需要考慮接口異常情況,總不能來個大大的try/catch/finally包住業務邏輯吧,那也太丑了。后面會再來一篇,重點說說接口如何在出現異常時,也能返回統一的結果響應。
本文只是拋出一塊磚,玉還得自己去找。
推薦閱讀
- SpringBoot 實戰:一招實現結果的優雅響應
- SpringBoot 實戰:如何優雅的處理異常
- SpringBoot 實戰:通過 BeanPostProcessor 動態注入 ID 生成器
- SpringBoot 實戰:自定義 Filter 優雅獲取請求參數和響應結果
- SpringBoot 實戰:優雅的使用枚舉參數
- SpringBoot 實戰:優雅的使用枚舉參數(原理篇)
- SpringBoot 實戰:在 RequestBody 中優雅的使用枚舉參數
- SpringBoot 實戰:在 RequestBody 中優雅的使用枚舉參數(原理篇)
到此這篇關于SpringBoot實戰之實現結果的優雅響應案例詳解的文章就介紹到這了,更多相關SpringBoot實戰之實現結果的優雅響應內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://www.howardliu.cn/springboot-action-gracefully-response/