Understanding How Spring MVC’s @RequestMapping POST Works

This is complicated, I think it is better to read the code.

In Spring 3.0 The magic is done by method public Method resolveHandlerMethod(HttpServletRequest request) of the inner class ServletHandlerMethodResolver of org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.

An instance of this class exists for every Request Controller Class, and has a field handlerMethods that contains a list of all the request methods.

But let me summarize how I understand it

  • Spring first checks if at least one handler method matches (this can contain false negatives)
  • Then it creates a map of all really matching handler methods
  • Then it sorts the map by request path: RequestSpecificMappingInfoComparator
  • and takes the first one

The sorting works this way: the RequestSpecificMappingInfoComparator first compares the path with the help of an AntPathMatcher, if two methods are equal according to this, then other metrics (like number of parameters, number of headers, etc.) are taken into account with respect to the request.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)