In a Spring-mvc interceptor I want to access to the handler controller method
public class CustomInterceptor implements HandlerInterceptor {
public bool
You can cast the Object handler
to HandlerMethod
.
HandlerMethod method = (HandlerMethod) handler;
Note however that the handler
argument passed to preHandle
is not always a HandlerMethod
(careful with ClassCastException
). HandlerMethod
then has methods you can use to get annotations, etc.
HandlerInterceptors will only provide you access to the HandlerMethod IF you have registered your interceptors like so :
@EnableWebMvc
@Configuration
public class InterceptorRegistry extends WebMvcConfigurerAdapter {
@Override
public void addInterceptors(org.springframework.web.servlet.config.annotation.InterceptorRegistry registry) {
registry.addInterceptor(new InternalAccessInterceptor());
registry.addInterceptor(new AuthorizationInterceptor());
}
}
In all other cases, the handler object will point to the controller. Most documentation on the web seemed to have missed this subtle point.