Sunday, September 10, 2017

Interceptor for Spring Rest controller

Create a java class which extends HandlerInterceptorAdapter.
You can override preHandle and postHandle methods as per your requirement.

public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("In Prehandle");
}
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
    System.out.println("In postHandle");
}

In spring-mvc-context.xml file, define class as interceptor as

 
  

Now your interceptor class should intercept all requests towards Rest controller. 

No comments: