1. 前言
一個(gè)技術(shù)需求引發(fā)的思考和實(shí)踐:
- 思考
- 用 AOP 把校驗(yàn)代碼
- 實(shí)踐
- 用 Spring MVC 的 RequestBodyAdvice 做AOP邏輯
- 繼承 RequestBodyAdviceAdapter 實(shí)現(xiàn)自己的 適配器
- 用自己的適配器讓代碼可讀性增加
- 熟悉 Spring MVC 、Java 反射的一些實(shí)踐
- 本文內(nèi)容
- 澄清一個(gè)AOP校驗(yàn)JSON內(nèi)容的思路
- 復(fù)習(xí)適配器模式
2. 怎么選擇切面?
- 一個(gè)大致的切面
- 從 Spring 4.2 開始,有新的切面出現(xiàn),可以切到
RequestBody
上,這個(gè)切面更符合技術(shù)需求的粒度。
public interface RequestBodyAdvice
3. RequestBody 的切面的描述
- RequestBodyAdvice
public interface RequestBodyAdvice {
// 什么時(shí)候切
boolean supports(MethodParameter methodParameter, Type targetType,
Class<? extends HttpMessageConverter<?>> converterType);
// 切到讀body前的位置
HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter parameter,
Type targetType, Class<? extends HttpMessageConverter<?>> converterType) throws IOException;
// 切到讀body后的位置
Object afterBodyRead(Object body, HttpInputMessage inputMessage, MethodParameter parameter,
Type targetType, Class<? extends HttpMessageConverter<?>> converterType);
// 處理空body
@Nullable
Object handleEmptyBody(@Nullable Object body, HttpInputMessage inputMessage, MethodParameter parameter,
Type targetType, Class<? extends HttpMessageConverter<?>> converterType);
}
根據(jù)接口描述,直接實(shí)現(xiàn)這個(gè)接口需要讀很多參數(shù)的含義,其實(shí)實(shí)現(xiàn)業(yè)務(wù)需求僅需要:
- 實(shí)現(xiàn)少部分方法
- 了解少部分方法的參數(shù)
4. Spring 提供的 RequestBody 的適配器
-
RequestBodyAdviceAdapter
該適配器簡(jiǎn)化了一個(gè)方法,并提供了必要的三個(gè)方法的實(shí)現(xiàn)
public abstract class RequestBodyAdviceAdapter implements RequestBodyAdvice {
@Override
public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter parameter,
Type targetType, Class<? extends HttpMessageConverter<?>> converterType)
throws IOException {
return inputMessage;
}
@Override
public Object afterBodyRead(Object body, HttpInputMessage inputMessage, MethodParameter parameter,
Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
return body;
}
@Override
@Nullable
public Object handleEmptyBody(@Nullable Object body, HttpInputMessage inputMessage,
MethodParameter parameter, Type targetType,
Class<? extends HttpMessageConverter<?>> converterType) {
return body;
}
}
根據(jù)適配器描述 afterBodyRead
方法能獲取到 body
參數(shù),我們能找到重點(diǎn)文章來源:http://www.zghlxwxcb.cn/news/detail-498978.html
-
afterBodyRead
方法 -
afterBodyRead
方法的body
參數(shù)
我們可以根據(jù)這個(gè)特點(diǎn),實(shí)現(xiàn)自己的適配器
5. 拓展 Spring 的適配器,實(shí)現(xiàn)自己的適配器
public abstract class AdviceAdapter extends RequestBodyAdviceAdapter {
// 暴露關(guān)心的參數(shù)
abstract void validate(Object body);
@Override
public boolean supports(MethodParameter methodParameter, @Nullable Type targetType, @Nullable Class<? extends HttpMessageConverter<?>> converterType) {
return Objects.nonNull(methodParameter.getMethodAnnotation(RequestValidated.class));
}
@Override
@Nonnull
public Object afterBodyRead(@Nonnull Object body, @Nullable HttpInputMessage inputMessage, MethodParameter parameter,
@Nullable Type targetType, @Nullable Class<? extends HttpMessageConverter<?>> converterType) {
// 嵌入切面
validate(body);
return input;
}
}
6. 在自己的適配器上實(shí)現(xiàn)邏輯
// 注意切 RequestBody 用的注解是 ControllerAdvice
@ControllerAdvice
public class ValidateAdvice extends AdviceAdapter {
// 具體實(shí)現(xiàn)
@Override
void validate(Object json) {
}
}
7. 后記
之前整理過適配器模式的內(nèi)容,現(xiàn)在感受更深了。
適配器可以多個(gè)配合起來工作,一個(gè)形象的圖用來比喻這種設(shè)計(jì):文章來源地址http://www.zghlxwxcb.cn/news/detail-498978.html
到了這里,關(guān)于【Spring MVC】獲取 @RequsetBody 標(biāo)識(shí)的對(duì)象,使用適配器模式增加代碼可讀性的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!