Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use or not leading slash in value for @RequestMapping. Need official docs or point to Spring source?

I involved in project where I found a mix of:

 @RequestMapping(value = "events/..."); @RequestMapping(value = "/events/..."); 

(with and without slash before method level annotation).

I perform search:

 site:http://static.springsource.org/spring/docs/3.1.x  slash 

and read these links:

  • http://forum.springsource.org/showthread.php?130753-Various-Spring-MVC-RequestMapping-configuration-questions
  • Various Spring MVC RequestMapping configuration questions
  • Handling of pre-slash in @RequestMapping
  • https://www.rfc-editor.org/rfc/rfc6570

But none of these sources answer why skipping slash allowed. Official Spring docs always shown examples with slashes...

Need point to official docs or to Spring sources.

like image 817
gavenkoa Avatar asked Oct 05 '12 08:10

gavenkoa


People also ask

What is the use of @RequestMapping annotation in spring?

RequestMapping annotation is used to map web requests onto specific handler classes and/or handler methods. @RequestMapping can be applied to the controller class as well as methods.

What is true about @RequestMapping annotation in Spring MVC?

The @RequestMapping annotation can be applied to class-level and/or method-level in a controller. The class-level annotation maps a specific request path or pattern onto a controller. You can then apply additional method-level annotations to make mappings more specific to handler methods.

What are all the HTTP methods that @RequestMapping can use?

Using @RequestMapping With HTTP Methods The Spring MVC @RequestMapping annotation is capable of handling HTTP request methods, such as GET, PUT, POST, DELETE, and PATCH. By default, all requests are assumed to be of HTTP GET type.

Can we use getmapping without RequestMapping?

It is always advised to be specific while declaring @RequestMapping on the controller methods as in most mapping handler classes, @Getmapping is not used. This feature distinguishes the @Getmapping and @RequestMapping annotations. It's an annotation that serves as a shortcut for the @Requestmapping annotation.


1 Answers

It does not matter: If the path does not start with an / then Spring (DefaultAnnotationHandlerMapping) will add it.

See method String[] determineUrlsForHandler(String beanName) of Class DefaultAnnotationHandlerMapping line 122 (Spring 3.1.2) (that is for the class level)

String[] methodLevelPatterns = determineUrlsForHandlerMethods(handlerType, true); for (String typeLevelPattern : typeLevelPatterns) {     if (!typeLevelPattern.startsWith("/")) {             typeLevelPattern = "/" + typeLevelPattern;     } 

See method String[] determineUrlsForHandler(Class<?> handlerType, final boolean hasTypeLevelMapping)) of Class DefaultAnnotationHandlerMapping line 182 (Spring 3.1.2) (that is for the method level)

String[] mappedPatterns = mapping.value(); if (mappedPatterns.length > 0) { for (String mappedPattern : mappedPatterns) {     if (!hasTypeLevelMapping && !mappedPattern.startsWith("/")) {         mappedPattern = "/" + mappedPattern;     }    
like image 147
Ralph Avatar answered Oct 05 '22 22:10

Ralph