Vivek Sinha February 2016

How to use RequestBodyAdvice

@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;
    }
}

Ideally, flow should reach these function first and then go to controller but it's not working.

Answers


Piotr Larysz March 2016

@ControllerAdvice component should be registered in Spring's context as any other component. Make sure Spring sees this component, e.q. by including it in @ComponentScan (Java Config) or in <context:component-scan/> (xml way). The preferred context to register it would be the Dispatcher Servlet's one.

Post Status

Asked in February 2016
Viewed 3,603 times
Voted 14
Answered 1 times

Search




Leave an answer