Skip to content

Commit f839c1f

Browse files
committed
Remove warning about empty @RequestMapping path
See gh-22543
1 parent 5b17bb2 commit f839c1f

File tree

8 files changed

+15
-66
lines changed

8 files changed

+15
-66
lines changed

spring-web/src/main/java/org/springframework/web/bind/annotation/RequestMapping.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,8 @@
9494
* <p><b>Supported at the type level as well as at the method level!</b>
9595
* When used at the type level, all method-level mappings inherit
9696
* this primary mapping, narrowing it for a specific handler method.
97-
* <p><strong>NOTE</strong>: Each handler method must be mapped to a
98-
* non-empty path, either at the type level, at the method level, or a
99-
* combination of the two. If you wish to map to all paths, please map
100-
* explicitly to {@code "/**"} or {@code "**"}.
97+
* <p><strong>NOTE</strong>: A handler method that is not mapped to any path
98+
* explicitly, is effectively mapped to an empty path.
10199
*/
102100
@AliasFor("path")
103101
String[] value() default {};
@@ -111,10 +109,8 @@
111109
* <p><b>Supported at the type level as well as at the method level!</b>
112110
* When used at the type level, all method-level mappings inherit
113111
* this primary mapping, narrowing it for a specific handler method.
114-
* <p><strong>NOTE</strong>: Each handler method must be mapped to a
115-
* non-empty path, either at the type level, at the method level, or a
116-
* combination of the two. If you wish to map to all paths, please map
117-
* explicitly to {@code "/**"} or {@code "**"}.
112+
* <p><strong>NOTE</strong>: A handler method that is not mapped to any path
113+
* explicitly, is effectively mapped to an empty path.
118114
* @since 4.2
119115
*/
120116
@AliasFor("value")

spring-webflux/src/main/java/org/springframework/web/reactive/result/method/AbstractHandlerMethodMapping.java

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,12 @@
4141
import org.springframework.lang.Nullable;
4242
import org.springframework.util.Assert;
4343
import org.springframework.util.ClassUtils;
44-
import org.springframework.util.StringUtils;
4544
import org.springframework.web.cors.CorsConfiguration;
4645
import org.springframework.web.cors.reactive.CorsUtils;
4746
import org.springframework.web.method.HandlerMethod;
4847
import org.springframework.web.reactive.HandlerMapping;
4948
import org.springframework.web.reactive.handler.AbstractHandlerMapping;
5049
import org.springframework.web.server.ServerWebExchange;
51-
import org.springframework.web.util.pattern.PathPattern;
5250

5351
/**
5452
* Abstract base class for {@link HandlerMapping} implementations that define
@@ -415,12 +413,6 @@ protected CorsConfiguration getCorsConfiguration(Object handler, ServerWebExchan
415413
@Nullable
416414
protected abstract T getMappingForMethod(Method method, Class<?> handlerType);
417415

418-
/**
419-
* Extract and return the URL path patterns contained in the supplied mapping.
420-
* @since 5.2
421-
*/
422-
protected abstract Set<PathPattern> getMappingPathPatterns(T mapping);
423-
424416
/**
425417
* Check if a mapping matches the current request and return a (potentially
426418
* new) mapping with conditions relevant to the current request.
@@ -507,16 +499,6 @@ public void register(T mapping, Object handler, Method method) {
507499
}
508500

509501
private void validateMethodMapping(HandlerMethod handlerMethod, T mapping) {
510-
// Log a warning if the supplied mapping maps the supplied HandlerMethod
511-
// only to empty paths.
512-
if (logger.isWarnEnabled() && getMappingPathPatterns(mapping).stream()
513-
.map(PathPattern::getPatternString).noneMatch(StringUtils::hasText)) {
514-
logger.warn(String.format(
515-
"Handler method '%s' in bean '%s' is not mapped to an explicit path. " +
516-
"If you wish to map to all paths, please map explicitly to \"/**\" or \"**\".",
517-
handlerMethod, handlerMethod.getBean()));
518-
}
519-
520502
// Assert that the supplied mapping is unique.
521503
HandlerMethod existingHandlerMethod = this.mappingLookup.get(mapping);
522504
if (existingHandlerMethod != null && !existingHandlerMethod.equals(handlerMethod)) {

spring-webflux/src/main/java/org/springframework/web/reactive/result/method/RequestMappingInfoHandlerMapping.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,6 @@ public abstract class RequestMappingInfoHandlerMapping extends AbstractHandlerMe
6868
}
6969

7070

71-
/**
72-
* Get the URL path patterns associated with the supplied {@link RequestMappingInfo}.
73-
* @since 5.2
74-
*/
75-
@Override
76-
protected Set<PathPattern> getMappingPathPatterns(RequestMappingInfo info) {
77-
return info.getPatternsCondition().getPatterns();
78-
}
79-
8071
/**
8172
* Check if the given RequestMappingInfo matches the current request and
8273
* return a (potentially new) instance with conditions that match the

spring-webflux/src/test/java/org/springframework/web/reactive/result/method/HandlerMethodMappingTests.java

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@
1717
package org.springframework.web.reactive.result.method;
1818

1919
import java.lang.reflect.Method;
20-
import java.util.Collections;
2120
import java.util.Comparator;
22-
import java.util.Set;
2321

2422
import org.hamcrest.Matchers;
2523
import org.junit.Before;
@@ -37,10 +35,7 @@
3735
import org.springframework.web.util.pattern.PathPattern;
3836
import org.springframework.web.util.pattern.PathPatternParser;
3937

40-
import static org.junit.Assert.assertEquals;
41-
import static org.junit.Assert.assertNotNull;
42-
import static org.junit.Assert.assertNull;
43-
import static org.junit.Assert.assertThat;
38+
import static org.junit.Assert.*;
4439

4540
/**
4641
* Unit tests for {@link AbstractHandlerMethodMapping}.
@@ -158,11 +153,6 @@ protected String getMappingForMethod(Method method, Class<?> handlerType) {
158153
return methodName.startsWith("handler") ? methodName : null;
159154
}
160155

161-
@Override
162-
protected Set<PathPattern> getMappingPathPatterns(String mapping) {
163-
return Collections.emptySet();
164-
}
165-
166156
@Override
167157
protected String getMatchingMapping(String pattern, ServerWebExchange exchange) {
168158
PathContainer lookupPath = exchange.getRequest().getPath().pathWithinApplication();

spring-webmvc/src/main/java/org/springframework/web/servlet/handler/AbstractHandlerMethodMapping.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
import org.springframework.util.ClassUtils;
4444
import org.springframework.util.LinkedMultiValueMap;
4545
import org.springframework.util.MultiValueMap;
46-
import org.springframework.util.StringUtils;
4746
import org.springframework.web.cors.CorsConfiguration;
4847
import org.springframework.web.cors.CorsUtils;
4948
import org.springframework.web.method.HandlerMethod;
@@ -616,15 +615,6 @@ public void register(T mapping, Object handler, Method method) {
616615
}
617616

618617
private void validateMethodMapping(HandlerMethod handlerMethod, T mapping) {
619-
// Log a warning if the supplied mapping maps the supplied HandlerMethod
620-
// only to empty paths.
621-
if (logger.isWarnEnabled() && getMappingPathPatterns(mapping).stream().noneMatch(StringUtils::hasText)) {
622-
logger.warn(String.format(
623-
"Handler method '%s' in bean '%s' is not mapped to an explicit path. " +
624-
"If you wish to map to all paths, please map explicitly to \"/**\" or \"**\".",
625-
handlerMethod, handlerMethod.getBean()));
626-
}
627-
628618
// Assert that the supplied mapping is unique.
629619
HandlerMethod existingHandlerMethod = this.mappingLookup.get(mapping);
630620
if (existingHandlerMethod != null && !existingHandlerMethod.equals(handlerMethod)) {

spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/RequestMappingInfoHandlerMappingTests.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public class RequestMappingInfoHandlerMappingTests {
7878

7979
private HandlerMethod barMethod;
8080

81-
private HandlerMethod rootMethod;
81+
private HandlerMethod emptyMethod;
8282

8383

8484
@Before
@@ -88,7 +88,7 @@ public void setup() throws Exception {
8888
this.fooMethod = new HandlerMethod(testController, "foo");
8989
this.fooParamMethod = new HandlerMethod(testController, "fooParam");
9090
this.barMethod = new HandlerMethod(testController, "bar");
91-
this.rootMethod = new HandlerMethod(testController, "root");
91+
this.emptyMethod = new HandlerMethod(testController, "empty");
9292

9393
this.handlerMapping = new TestRequestMappingInfoHandlerMapping();
9494
this.handlerMapping.registerHandler(testController);
@@ -125,12 +125,12 @@ public void getHandlerEmptyPathMatch() throws Exception {
125125
MockHttpServletRequest request = new MockHttpServletRequest("GET", "");
126126
HandlerMethod handlerMethod = getHandler(request);
127127

128-
assertEquals(this.rootMethod.getMethod(), handlerMethod.getMethod());
128+
assertEquals(this.emptyMethod.getMethod(), handlerMethod.getMethod());
129129

130130
request = new MockHttpServletRequest("GET", "/");
131131
handlerMethod = getHandler(request);
132132

133-
assertEquals(this.rootMethod.getMethod(), handlerMethod.getMethod());
133+
assertEquals(this.emptyMethod.getMethod(), handlerMethod.getMethod());
134134
}
135135

136136
@Test
@@ -465,8 +465,8 @@ public void fooParam() {
465465
public void bar() {
466466
}
467467

468-
@RequestMapping("/")
469-
public void root() {
468+
@RequestMapping("")
469+
public void empty() {
470470
}
471471

472472
@RequestMapping(value = "/person/{id}", method = RequestMethod.PUT, consumes="application/xml")

spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerMappingTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ private RequestMappingInfo assertComposedAnnotationMapping(String methodName, St
228228
@RequestMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
229229
static class ComposedAnnotationController {
230230

231-
@RequestMapping("/**")
231+
@RequestMapping
232232
public void handle() {
233233
}
234234

spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ServletAnnotationControllerHandlerMethodTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2016,7 +2016,7 @@ public void dataClassBindingWithLocalDate() throws Exception {
20162016
@Controller
20172017
static class ControllerWithEmptyValueMapping {
20182018

2019-
@RequestMapping("/**")
2019+
@RequestMapping("")
20202020
public void myPath2(HttpServletResponse response) throws IOException {
20212021
throw new IllegalStateException("test");
20222022
}
@@ -2035,7 +2035,7 @@ public void myPath2(Exception ex, HttpServletResponse response) throws IOExcepti
20352035
@Controller
20362036
private static class ControllerWithErrorThrown {
20372037

2038-
@RequestMapping("/**")
2038+
@RequestMapping("")
20392039
public void myPath2(HttpServletResponse response) throws IOException {
20402040
throw new AssertionError("test");
20412041
}
@@ -3629,7 +3629,7 @@ public String home() {
36293629
@Controller
36303630
static class HttpHeadersResponseController {
36313631

3632-
@RequestMapping(value = "/*", method = RequestMethod.POST)
3632+
@RequestMapping(value = "", method = RequestMethod.POST)
36333633
@ResponseStatus(HttpStatus.CREATED)
36343634
public HttpHeaders create() throws URISyntaxException {
36353635
HttpHeaders headers = new HttpHeaders();

0 commit comments

Comments
 (0)