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:
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.
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.
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.
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.
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.
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; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With