Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between a spring request mapping and url mapping?

I stumbled across this question after reading the log of my spring boot applicaiton in debug mode.

At startup, the While the spring RequestMappingHandlerMapping is "Looking for request mappings in application context" and finds the resquest mappings defined on my controllers. Later the BeanNameUrlHandlerMapping is "Looking for URL mappings in application context" and fails to find one for every bean defined in my context (no URL paths identified)

My question is, what the difference between a request mapping and an url mapping, can someone link a documentation to read what the BeanNameUrlHandlerMapping is looking for?

like image 993
Konrad Lötzsch Avatar asked May 24 '17 12:05

Konrad Lötzsch


People also ask

What is difference between request mapping and mapping?

@RequestMapping is used at the class level while @GetMapping is used to connect the methods. This is also an important Spring MVC interview question to knowing how and when to use both RequestMapping and GetMapping is crucial for Java developers.

What is request mapping in spring?

One of the most important annotations in spring is the @RequestMapping Annotation which is used to map HTTP requests to handler methods of MVC and REST controllers. In Spring MVC applications, the DispatcherServlet (Front Controller) is responsible for routing incoming HTTP requests to handler methods of controllers.

What is the difference between @RequestMapping and PostMapping?

The @PostMapping is a specialized version of @RequestMapping annotation that acts as a shortcut for @RequestMapping(method = RequestMethod. POST) . The @PostMapping annotated methods in the @Controller annotated classes handle the HTTP POST requests matched with the given URI expression.

What are the differences between @RequestMapping and RestController?

@RestController annotation indicates that class is a controller where @RequestMapping methods assume @ResponseBody semantics by default. In @Controller, we need to use @ResponseBody on every handler method. In @RestController, we don't need to use @ResponseBody on every handler method.


1 Answers

RequestMappingHandlerMapping

According to the documentation, the RequestMappingHandlerMapping:

Creates RequestMappingInfo instances from type and method-level @RequestMapping annotations in @Controller classes.

A RequestMappingInfo can be instantiated with the constructor:

public RequestMappingInfo(String name,
                          PatternsRequestCondition patterns,
                          RequestMethodsRequestCondition methods,
                          ParamsRequestCondition params,
                          HeadersRequestCondition headers,
                          ConsumesRequestCondition consumes,
                          ProducesRequestCondition produces,
                          RequestCondition<?> custom)

and represents a request with a set of conditions to be matched.

BeanNameUrlHandlerMapping

The BeanNameUrlHandlerMapping is an:

Implementation of the HandlerMapping interface that map from URLs to beans with names that start with a slash ("/"),

and as a AbstractDetectingUrlHandlerMapping is:

detecting URL mappings for handler beans through introspection of all defined beans in the application context.

like image 183
Ortomala Lokni Avatar answered Nov 09 '22 05:11

Ortomala Lokni