From f839c1f9cde435f35c0289d4e7639b55c98259e9 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Tue, 9 Apr 2019 13:12:35 -0400 Subject: [PATCH] Remove warning about empty @RequestMapping path See gh-22543 --- .../web/bind/annotation/RequestMapping.java | 12 ++++-------- .../method/AbstractHandlerMethodMapping.java | 18 ------------------ .../RequestMappingInfoHandlerMapping.java | 9 --------- .../method/HandlerMethodMappingTests.java | 12 +----------- .../handler/AbstractHandlerMethodMapping.java | 10 ---------- .../RequestMappingInfoHandlerMappingTests.java | 12 ++++++------ .../RequestMappingHandlerMappingTests.java | 2 +- ...AnnotationControllerHandlerMethodTests.java | 6 +++--- 8 files changed, 15 insertions(+), 66 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/web/bind/annotation/RequestMapping.java b/spring-web/src/main/java/org/springframework/web/bind/annotation/RequestMapping.java index 0b2f1487d4..accd88bd89 100644 --- a/spring-web/src/main/java/org/springframework/web/bind/annotation/RequestMapping.java +++ b/spring-web/src/main/java/org/springframework/web/bind/annotation/RequestMapping.java @@ -94,10 +94,8 @@ public @interface RequestMapping { *

Supported at the type level as well as at the method level! * When used at the type level, all method-level mappings inherit * this primary mapping, narrowing it for a specific handler method. - *

NOTE: Each handler method must be mapped to a - * non-empty path, either at the type level, at the method level, or a - * combination of the two. If you wish to map to all paths, please map - * explicitly to {@code "/**"} or {@code "**"}. + *

NOTE: A handler method that is not mapped to any path + * explicitly, is effectively mapped to an empty path. */ @AliasFor("path") String[] value() default {}; @@ -111,10 +109,8 @@ public @interface RequestMapping { *

Supported at the type level as well as at the method level! * When used at the type level, all method-level mappings inherit * this primary mapping, narrowing it for a specific handler method. - *

NOTE: Each handler method must be mapped to a - * non-empty path, either at the type level, at the method level, or a - * combination of the two. If you wish to map to all paths, please map - * explicitly to {@code "/**"} or {@code "**"}. + *

NOTE: A handler method that is not mapped to any path + * explicitly, is effectively mapped to an empty path. * @since 4.2 */ @AliasFor("value") diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/AbstractHandlerMethodMapping.java b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/AbstractHandlerMethodMapping.java index fc893f8f58..bc7b46e416 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/AbstractHandlerMethodMapping.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/AbstractHandlerMethodMapping.java @@ -41,14 +41,12 @@ import org.springframework.http.server.RequestPath; import org.springframework.lang.Nullable; import org.springframework.util.Assert; import org.springframework.util.ClassUtils; -import org.springframework.util.StringUtils; import org.springframework.web.cors.CorsConfiguration; import org.springframework.web.cors.reactive.CorsUtils; import org.springframework.web.method.HandlerMethod; import org.springframework.web.reactive.HandlerMapping; import org.springframework.web.reactive.handler.AbstractHandlerMapping; import org.springframework.web.server.ServerWebExchange; -import org.springframework.web.util.pattern.PathPattern; /** * Abstract base class for {@link HandlerMapping} implementations that define @@ -415,12 +413,6 @@ public abstract class AbstractHandlerMethodMapping extends AbstractHandlerMap @Nullable protected abstract T getMappingForMethod(Method method, Class handlerType); - /** - * Extract and return the URL path patterns contained in the supplied mapping. - * @since 5.2 - */ - protected abstract Set getMappingPathPatterns(T mapping); - /** * Check if a mapping matches the current request and return a (potentially * new) mapping with conditions relevant to the current request. @@ -507,16 +499,6 @@ public abstract class AbstractHandlerMethodMapping extends AbstractHandlerMap } private void validateMethodMapping(HandlerMethod handlerMethod, T mapping) { - // Log a warning if the supplied mapping maps the supplied HandlerMethod - // only to empty paths. - if (logger.isWarnEnabled() && getMappingPathPatterns(mapping).stream() - .map(PathPattern::getPatternString).noneMatch(StringUtils::hasText)) { - logger.warn(String.format( - "Handler method '%s' in bean '%s' is not mapped to an explicit path. " + - "If you wish to map to all paths, please map explicitly to \"/**\" or \"**\".", - handlerMethod, handlerMethod.getBean())); - } - // Assert that the supplied mapping is unique. HandlerMethod existingHandlerMethod = this.mappingLookup.get(mapping); if (existingHandlerMethod != null && !existingHandlerMethod.equals(handlerMethod)) { diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/RequestMappingInfoHandlerMapping.java b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/RequestMappingInfoHandlerMapping.java index 66ca1c8533..3e66ed8c48 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/RequestMappingInfoHandlerMapping.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/RequestMappingInfoHandlerMapping.java @@ -68,15 +68,6 @@ public abstract class RequestMappingInfoHandlerMapping extends AbstractHandlerMe } - /** - * Get the URL path patterns associated with the supplied {@link RequestMappingInfo}. - * @since 5.2 - */ - @Override - protected Set getMappingPathPatterns(RequestMappingInfo info) { - return info.getPatternsCondition().getPatterns(); - } - /** * Check if the given RequestMappingInfo matches the current request and * return a (potentially new) instance with conditions that match the diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/result/method/HandlerMethodMappingTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/result/method/HandlerMethodMappingTests.java index ad9c8a5286..d0c0074afb 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/result/method/HandlerMethodMappingTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/result/method/HandlerMethodMappingTests.java @@ -17,9 +17,7 @@ package org.springframework.web.reactive.result.method; import java.lang.reflect.Method; -import java.util.Collections; import java.util.Comparator; -import java.util.Set; import org.hamcrest.Matchers; import org.junit.Before; @@ -37,10 +35,7 @@ import org.springframework.web.server.ServerWebExchange; import org.springframework.web.util.pattern.PathPattern; import org.springframework.web.util.pattern.PathPatternParser; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertThat; +import static org.junit.Assert.*; /** * Unit tests for {@link AbstractHandlerMethodMapping}. @@ -158,11 +153,6 @@ public class HandlerMethodMappingTests { return methodName.startsWith("handler") ? methodName : null; } - @Override - protected Set getMappingPathPatterns(String mapping) { - return Collections.emptySet(); - } - @Override protected String getMatchingMapping(String pattern, ServerWebExchange exchange) { PathContainer lookupPath = exchange.getRequest().getPath().pathWithinApplication(); diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/handler/AbstractHandlerMethodMapping.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/handler/AbstractHandlerMethodMapping.java index f75eb4c318..b28c7655b0 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/handler/AbstractHandlerMethodMapping.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/handler/AbstractHandlerMethodMapping.java @@ -43,7 +43,6 @@ import org.springframework.util.Assert; import org.springframework.util.ClassUtils; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; -import org.springframework.util.StringUtils; import org.springframework.web.cors.CorsConfiguration; import org.springframework.web.cors.CorsUtils; import org.springframework.web.method.HandlerMethod; @@ -616,15 +615,6 @@ public abstract class AbstractHandlerMethodMapping extends AbstractHandlerMap } private void validateMethodMapping(HandlerMethod handlerMethod, T mapping) { - // Log a warning if the supplied mapping maps the supplied HandlerMethod - // only to empty paths. - if (logger.isWarnEnabled() && getMappingPathPatterns(mapping).stream().noneMatch(StringUtils::hasText)) { - logger.warn(String.format( - "Handler method '%s' in bean '%s' is not mapped to an explicit path. " + - "If you wish to map to all paths, please map explicitly to \"/**\" or \"**\".", - handlerMethod, handlerMethod.getBean())); - } - // Assert that the supplied mapping is unique. HandlerMethod existingHandlerMethod = this.mappingLookup.get(mapping); if (existingHandlerMethod != null && !existingHandlerMethod.equals(handlerMethod)) { diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/RequestMappingInfoHandlerMappingTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/RequestMappingInfoHandlerMappingTests.java index 9f428c03ec..324ed59475 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/RequestMappingInfoHandlerMappingTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/RequestMappingInfoHandlerMappingTests.java @@ -78,7 +78,7 @@ public class RequestMappingInfoHandlerMappingTests { private HandlerMethod barMethod; - private HandlerMethod rootMethod; + private HandlerMethod emptyMethod; @Before @@ -88,7 +88,7 @@ public class RequestMappingInfoHandlerMappingTests { this.fooMethod = new HandlerMethod(testController, "foo"); this.fooParamMethod = new HandlerMethod(testController, "fooParam"); this.barMethod = new HandlerMethod(testController, "bar"); - this.rootMethod = new HandlerMethod(testController, "root"); + this.emptyMethod = new HandlerMethod(testController, "empty"); this.handlerMapping = new TestRequestMappingInfoHandlerMapping(); this.handlerMapping.registerHandler(testController); @@ -125,12 +125,12 @@ public class RequestMappingInfoHandlerMappingTests { MockHttpServletRequest request = new MockHttpServletRequest("GET", ""); HandlerMethod handlerMethod = getHandler(request); - assertEquals(this.rootMethod.getMethod(), handlerMethod.getMethod()); + assertEquals(this.emptyMethod.getMethod(), handlerMethod.getMethod()); request = new MockHttpServletRequest("GET", "/"); handlerMethod = getHandler(request); - assertEquals(this.rootMethod.getMethod(), handlerMethod.getMethod()); + assertEquals(this.emptyMethod.getMethod(), handlerMethod.getMethod()); } @Test @@ -465,8 +465,8 @@ public class RequestMappingInfoHandlerMappingTests { public void bar() { } - @RequestMapping("/") - public void root() { + @RequestMapping("") + public void empty() { } @RequestMapping(value = "/person/{id}", method = RequestMethod.PUT, consumes="application/xml") diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerMappingTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerMappingTests.java index e88640cfff..d0f227b533 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerMappingTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerMappingTests.java @@ -228,7 +228,7 @@ public class RequestMappingHandlerMappingTests { @RequestMapping(consumes = MediaType.APPLICATION_JSON_VALUE) static class ComposedAnnotationController { - @RequestMapping("/**") + @RequestMapping public void handle() { } diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ServletAnnotationControllerHandlerMethodTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ServletAnnotationControllerHandlerMethodTests.java index 6930a06442..5383b85220 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ServletAnnotationControllerHandlerMethodTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ServletAnnotationControllerHandlerMethodTests.java @@ -2016,7 +2016,7 @@ public class ServletAnnotationControllerHandlerMethodTests extends AbstractServl @Controller static class ControllerWithEmptyValueMapping { - @RequestMapping("/**") + @RequestMapping("") public void myPath2(HttpServletResponse response) throws IOException { throw new IllegalStateException("test"); } @@ -2035,7 +2035,7 @@ public class ServletAnnotationControllerHandlerMethodTests extends AbstractServl @Controller private static class ControllerWithErrorThrown { - @RequestMapping("/**") + @RequestMapping("") public void myPath2(HttpServletResponse response) throws IOException { throw new AssertionError("test"); } @@ -3629,7 +3629,7 @@ public class ServletAnnotationControllerHandlerMethodTests extends AbstractServl @Controller static class HttpHeadersResponseController { - @RequestMapping(value = "/*", method = RequestMethod.POST) + @RequestMapping(value = "", method = RequestMethod.POST) @ResponseStatus(HttpStatus.CREATED) public HttpHeaders create() throws URISyntaxException { HttpHeaders headers = new HttpHeaders();