4

I am looking for a way to intercept in spring webmvc an incoming http request and response. I need to rewrite the entire request and response.

In concrete, I need to receive a request, extract the request body and process the request body as if it were a brand new request (thus rewriting the entire request before it gets handled further). Thereafter, I need to rewrite the response that is generated and wrap it somehow.

Can anyone help and provide some pointers?

Thanks

user1052080
  • 430
  • 5
  • 12

1 Answers1

4

You can write interceptors in Spring MVC by implementing HandlerInterceptor interface. There are three methods that need to be implemented.

preHandle(..) is called before the actual handler is executed;

postHandle(..) is called after the handler is executed;

afterCompletion(..) is called after the complete request has finished.

These three methods should provide enough flexibility to do all kinds of preprocessing and postprocessing.

Learn more about how to place a filter in SpringMVC: http://viralpatel.net/blogs/spring-mvc-interceptor-example/

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
  • I am sorry, that's not the right way to rewrite the request. The right way is to not use SpringWebMVC, but to rely on the plain servlet api. After days, I found the right answer at http://stackoverflow.com/questions/1046721/accessing-the-raw-body-of-a-put-or-post-request/1048123#1048123 – user1052080 Sep 02 '13 at 16:24