Skip to content
Advertisement

Spring 3.1 HandlerInterceptor Not being called

I followed the documentation for HandlerInterceptors. Noting that in the new version of Spring: “the configured interceptor will apply to all requests handled with annotated controller methods”.

The following is in an xml configuration file: enter image description here

I have an annotated controller beginning like this:

enter image description here

When I request a url that executes the controller’s code, my interceptor code is never called. Can anyone please explain why?

The interceptor code is:

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public class DomainNameInterceptor extends HandlerInterceptorAdapter {
    public boolean preHandle(HttpServletRequest request,
                           HttpServletResponse response, Object handler) 
         throws Exception {
    System.out.println("Why is this not called?");
    return true;
  }
}

I was using the following documentation: Spring Core 3.1.x Documentation

I did a search for HandlerInterceptor and followed the example given within the documentation in the included link.

Advertisement

Answer

If you have configured your MVC context using <mvc:annotation-driven/>,then I think the handlerMapping created when defining beans based on this custom namespace is overriding the handlerMapping that you have defined. A better way to register your interceptors would be to use the <mvc:interceptors> subtag to define the interceptors, this way it will get registered to the correct handlerMapping:

<mvc:annotation-driven>
    <mvc:interceptors>
        <ref bean="interceptor"/>
    </mvc:interceptors>
</mvc:annotation-driven>
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement