Michael
Michael

Reputation: 2634

Spring's @RequestMapping internals

I'm building an app with multiple maven artifact. Because of design restrictions, one of the artifacts are meant to be backend in nature with no access to request (hence not allowed to use @RequestMapping). The reality tho is that there are requests that will need to be wired directly to some of those class methods.

I did some deep diving in Spring and came across half of what I needed to make it work. The easy part is to write a HandlerMapping myself and configure it correctly. That mapper would have knowledge of the backend entities to map the necessary urls manually.

The part that I couldn't find tho is where (what) in Spring manages the invocation ? When you have for instance:

@Component
@RequestMapping("/mypath")
public class MyStuff
{
   @RequestMapping(value = "/dothis", method = RequestMethod.GET)
   public ResponseEntity<String> doThis(HttpServletRequest request)
   {
   }
}

When a request is made to /mypath/dothis, which object intercepts that request and invokes the correct method within the correct bean ? As far as I can tell, the mapper only returns a string array of all urls mapped to a specific bean.

Upvotes: 0

Views: 440

Answers (2)

Biju Kunjummen
Biju Kunjummen

Reputation: 49915

At a high level this is the flow - Spring's DispatcherServlet handles the request first - this is the one with an entry in the web.xml file.

DispatcherServlet maintains a list of HandlerAdapters and HandlerMappings, for the request, it asks each handlerMapping for a handler

With Spring 3.1, one of the main handlerMapping implementation is the RequestMappingHandlerMapping which maintains the mapping of the request uri to the handler (which is a HandlerMethod pointing ultimately to a @RequestMapping mapped method).

Once a handler is obtained from the HandlerMapping, DispatcherServlet asks each handlerAdapter whether it can handle the request(HandlerAdapter.supports api), if it does the request is dispatched to the appropriate HandlerAdapter, which ultimately invokes the handlerMethod. With Spring 3.1, the main HandlerAdapter registered with <mvc:annotation-driven/> is RequestMappingHandlerAdapter

I think this should be sufficient start for a deeper investigation. A good way to follow the flow is to put a breakpoint in DispatcherServlet and follow the flow through the stack.

Upvotes: 3

Matt
Matt

Reputation: 11805

http://static.springsource.org/spring/docs/current/spring-framework-reference/html/mvc.html

See section 16.4 about request interceptors. You should be able to use that to map to your class.

Upvotes: 0

Related Questions