請求參數(shù)增強
- 使用@ControllerAdvice這個控制器增強注解针饥,來對所有Controller進行增強(當然也可以指定包下或帶有指定注解的Controller類)吱抚。
- 增強后實現(xiàn) RequestBodyAdvice 接口,完成請求體參數(shù)增強。在實現(xiàn)的接口中完成請求參數(shù)加工操作。
/**
* 請求參數(shù)在加工。
* 利用 Controller增強注解 + 實現(xiàn) RequestBodyAdvice 請求完成
* 繼承 RequestBodyAdviceAdapter類检激,是為了方便有些方法使用spring框架默認執(zhí)行。
* RequestBodyAdviceAdapter類中腹侣,已經(jīng)重寫了 beforeBodyRead叔收、afterBodyRead、handleEmptyBody這三個方法傲隶。
* 為了方便饺律,繼承這個類后,我只需要重寫自己需要的方法即可跺株。
*/
@ControllerAdvice
public class CheckRequestBodyAdvice extends RequestBodyAdviceAdapter implements RequestBodyAdvice {
@Override
public boolean supports(MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {
System.out.println(methodParameter.getMethod().getName());
System.out.println(type.getTypeName());
System.out.println(aClass);
return true;
}
@Override
public Object afterBodyRead(Object body, HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {
System.out.println(body);
return body;
}
}
注意
- 如果請求體沒有內(nèi)容复濒,則攔截不到。比如:GET請求乒省,請求參數(shù)在URL上巧颈,不在請求體里。