SpringMVC 請求體參數(shù)增強

請求參數(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上,不在請求體里。
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容