@ControllerAdvice
public class RequestBodyAdviceChain implements RequestBodyAdvice {

    @Override
    public boolean supports(MethodParameter methodParameter, Type type,
            Class< ? extends HttpMessageConverter< ? >> aClass) {
        return true;
    }

    @Override
    public Object handleEmptyBody(Object o, HttpInputMessage httpInputMessage, MethodParameter methodParameter,
            Type type, Class< ? extends HttpMessageConverter< ? >> aClass) {
        return o;
    }

    @Override
    public HttpInputMessage beforeBodyRead(HttpInputMessage httpInputMessage, MethodParameter methodParameter,
            Type type, Class< ? extends HttpMessageConverter< ? >> aClass) throws IOException {
        return httpInputMessage;
    }

    @Override
    public Object afterBodyRead(Object o, HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type,
            Class< ? extends HttpMessageConverter< ? >> aClass) {
        return o;
    }
}


理想情况下,流量应首先达到这些功能,然后再进入控制器,但它不起作用。

最佳答案

@ControllerAdvice组件应该像其他任何组件一样在Spring的上下文中注册。确保Spring看到此组件,例如通过将其包含在@ComponentScan(Java Config)或<context:component-scan/>(xml方式)中。注册它的首选上下文是分派器Servlet的上下文。

07-24 13:51