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

@@ -797,11 +797,28 @@ public class ServletAnnotationControllerHandlerMethodTests extends AbstractServl
}
@Test
public void unmappedPathMapping() {
assertThatThrownBy(() -> initServletWithControllers(UnmappedPathController.class))
.isInstanceOf(BeanCreationException.class)
.hasCauseInstanceOf(IllegalStateException.class)
.hasMessageContaining("Missing path mapping");
public void unmappedPathMapping() throws Exception {
initServletWithControllers(UnmappedPathController.class);
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/bogus-unmapped");
MockHttpServletResponse response = new MockHttpServletResponse();
getServlet().service(request, response);
assertEquals("get", response.getContentAsString());
}
@Test
public void explicitAndEmptyPathsControllerMapping() throws Exception {
initServletWithControllers(ExplicitAndEmptyPathsController.class);
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");
MockHttpServletResponse response = new MockHttpServletResponse();
getServlet().service(request, response);
assertEquals("get", response.getContentAsString());
request = new MockHttpServletRequest("GET", "");
response = new MockHttpServletResponse();
getServlet().service(request, response);
assertEquals("get", response.getContentAsString());
}
@Test
@@ -2733,11 +2750,22 @@ public class ServletAnnotationControllerHandlerMethodTests extends AbstractServl
}
@Controller
@RequestMapping // path intentionally omitted
// @RequestMapping intentionally omitted
static class UnmappedPathController {
@GetMapping // path intentionally omitted
public void get(@RequestParam(required = false) String id) {
public void get(Writer writer) throws IOException {
writer.write("get");
}
}
@Controller
// @RequestMapping intentionally omitted
static class ExplicitAndEmptyPathsController {
@GetMapping({"/", ""})
public void get(Writer writer) throws IOException {
writer.write("get");
}
}