Log warning if @RequestMapping method has no explicit mapping

Commit c0b52d09f5 introduced support for
throwing an exception if a @RequestMapping handler method in a Spring
MVC controller was mapped to an empty path. This had negative side
effects for applications that intentionally mapped to an empty path,
potentially alongside a mapping to an explicit path for the same
handler method.

This commit addresses this by logging a warning (instead of throwing an
exception) if a @RequestMapping method is mapped only to empty paths.

This commit also introduces the same support for WebFlux-based
@RequestMapping handler methods.

Closes gh-22543
This commit is contained in:
Sam Brannen
2019-04-05 19:48:30 +02:00
parent e4525cf4c1
commit 72027b1746
7 changed files with 104 additions and 29 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,7 +17,9 @@
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;
@@ -44,6 +46,7 @@ import static org.junit.Assert.assertThat;
* Unit tests for {@link AbstractHandlerMethodMapping}.
*
* @author Rossen Stoyanchev
* @author Sam Brannen
*/
public class HandlerMethodMappingTests {
@@ -155,6 +158,11 @@ public class HandlerMethodMappingTests {
return methodName.startsWith("handler") ? methodName : null;
}
@Override
protected Set<String> getMappingPathPatterns(String mapping) {
return Collections.emptySet();
}
@Override
protected String getMatchingMapping(String pattern, ServerWebExchange exchange) {
PathContainer lookupPath = exchange.getRequest().getPath().pathWithinApplication();
@@ -182,4 +190,5 @@ public class HandlerMethodMappingTests {
public void handlerMethod2() {
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -26,6 +26,7 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import org.junit.Before;
import org.junit.Test;
@@ -69,7 +70,9 @@ import static org.springframework.web.reactive.result.method.RequestMappingInfo.
/**
* Unit tests for {@link RequestMappingInfoHandlerMapping}.
*
* @author Rossen Stoyanchev
* @author Sam Brannen
*/
public class RequestMappingInfoHandlerMappingTests {
@@ -475,6 +478,11 @@ public class RequestMappingInfoHandlerMappingTests {
return null;
}
}
@Override
protected Set<String> getMappingPathPatterns(RequestMappingInfo info) {
return info.getPatternsCondition().getPatterns().stream().map(PathPattern::getPatternString).collect(Collectors.toSet());
}
}
}