Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I lookup the method being called on a Handler from a Spring HandlerInterceptor?

I have a Spring HandlerInterceptor intercepting the frontend URL's in my application (/app/*). I want to determine which action method in the Handler is about to be invoked from within the HandlerInterceptor. Is there a way to look that up, do I need to inject something into the interceptor that can look that up based on the requested path?

The Interceptor is like this:

public class PageCacheInterceptor implements HandlerInterceptor {...}

It is mapped like this:

<mvc:interceptors>
    <bean class="com.example.web.interceptors.PageCacheInterceptor" />
</mvc:interceptors>

Background (because I know you'll ask!). I am adding simple page caching to my app and want to use an annotation like @Cacheable on each suitable method in the controller. The interceptor can then determine whether to cache a response based on the action that created it.

For example:

@RequestMapping(value = "", method = RequestMethod.GET)
@Cacheable(events={Events.NEW_ORDER,Events.NEW_STAT})
public String home(Model model) {...}

The events are the ones that cause the cache to be invalidated. For example /widget/list action would have it's cached response invalidated by a new widget being saved.

Edit: I've upgraded to the latest Spring 3.1 M2, as this blog post hinted at features I need, but it's not clear whether injecting these new classes or sub-classing them will be required. Has any one used them to retrieve the HandlerMethod in an interceptor?

like image 577
Ashley Schroder Avatar asked Sep 03 '11 00:09

Ashley Schroder


People also ask

Which intercepts all the requests from the client?

Spring Interceptor are used to intercept client requests and process them.

What are handler methods in spring?

Class HandlerMethod. Encapsulates information about a handler method consisting of a method and a bean. Provides convenient access to method parameters, the method return value, method annotations, etc. The class may be created with a bean instance or with a bean name (e.g. lazy-init bean, prototype bean).

How do you intercept HTTP requests in spring boot?

To work with interceptor, you need to create @Component class that supports it and it should implement the HandlerInterceptor interface. preHandle() method − This is used to perform operations before sending the request to the controller. This method should return true to return the response to the client.

Which annotation is used for handler method?

4. Handler method annotation. Explanation: In order to do so, a controller class's methods are decorated with the @RequestMapping annotation, making them handler methods.


1 Answers

Ok so the solution was actually really easy:

1) Upgrade to Spring 3.1

2) RTFM (properly)

For example a HandlerInterceptor can cast the handler from Object to HandlerMethod and get access to the target controller method, its annotations, etc

3) Cast the handler object to HandlerMethod in the Interceptor.

Then you can do this sort of thing:

    HandlerMethod method = (HandlerMethod) handler;
    Cacheable methodAnnotation = method.getMethodAnnotation(Cacheable.class);
    if (methodAnnotation != null) {
        System.out.println("cacheable request");
    }
like image 108
Ashley Schroder Avatar answered Oct 14 '22 07:10

Ashley Schroder