Although https://github.com/spring-projects/spring-framework/issues/33444 has been fixed for root URLs, it looks like there is still a problem with the UrlHandlerFilter
when server.servlet.contextPath
is specified.
Here is a minimal application to reproduce it: https://github.com/bdshadow/SpringTrailingSlashTestProject
server.servlet.contextPath=/myApp
And use the following UrlHandlerFilter
:
UrlHandlerFilter filter = UrlHandlerFilter
.trailingSlashHandler("/**").wrapRequest()
.build();
And in this case, both http://localhost:8080/myApp/greeting
and http://localhost:8080/myApp/greeting
work fine, however,
http://localhost:8080/myApp
and http://localhost:8080/myApp/
don't and return 404.
Controller for the root looks like this:
@RestController
@RequestMapping("/")
public class MyController {
@GetMapping(value = {"/", "greeting"})
public String greeting() {
return "Hello world!";
}
}
Comment From: rstoyanchev
You can map with "" which handles "/" as well. For example this works:
@RestController
public class MyController {
@GetMapping(value = {"", "greeting"})
public String greeting() {
return "Hello world!";
}
}
However, if you configure UrlHandlerFilter
to redirect, it would send "/myApp/" to "/myApp", which would work for the request mappings, but Tomcat redirects "/myApp" back to "/myApp/". And in any case, I think the UrlHandlerFilter
should makes checks on the pathWithinApplication
, excluding the contextPath.