404 error occurs with RequestMapping(path="")

It looks like the contents of this issue are not fixed. I checked with SpringBoot 3.0.5 and the same issue occurred.

spring-projects/spring-framework#29625 404 occurred when using @GetMapping as well as the above Controller.

See below for reproduction procedure

  1. create SampleController
1
2
3
4
5
6
7
8
@RestController
public class SampleController {

    @RequestMapping(path = "")
    public String demo(){
        return "test";
    }
}
  1. request the following URL
  1. confirmation result
  • Expected value
    • The string “test” is displayed.
    • Status:200
  • Actual result
    • Status:404

Supplementation

Occurrence Conditions

case SpringBoot ver2.7.10

request mapping expected actual OK/NG
localhost @RequestMapping 200 200 OK
localhost/ @RequestMapping 200 200 OK
localhost @RequestMapping(path = “”) 200 200 OK
localhost/ @RequestMapping(path = “”) 200 200 OK
localhost @RequestMapping(path = “/”) 200 200 OK
localhost/ @RequestMapping(path = “/”) 200 200 OK

case SpringBoot ver3.0.5

request mapping expected actual OK/NG
localhost @RequestMapping 200 404 NG
localhost/ @RequestMapping 200 404 NG
localhost @RequestMapping(path = “”) 200 404 NG
localhost/ @RequestMapping(path = “”) 200 404 NG
localhost @RequestMapping(path = “/”) 200 200 OK
localhost/ @RequestMapping(path = “/”) 200 200 OK

※The same phenomenon occurs with @GetMapping

Versions confirmed to work

  • Confirmed with the following version
SpringBoot version Result
2.7.6 OK
2.7.10 OK
3.0.0 M3 OK
3.0.0 M4 NG
3.0.0 GA NG
3.0.2 NG
3.0.3 NG
3.0.4 NG
3.0.5 NG

解决办法:

在 controller 上追加 requestMapping

解决 issue 为:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
See spring-projects/spring-boot#33499 for initial report. It looks like the enhancement in #29625 was incomplete.

The goal was to make the following work for both http://localhost:8080 and http://localhost:8080/:

@RestController
public class HomeController {

    @GetMapping
    public String home() {
        return "Hello, World!";
    }

}
But this use case doesn't work still, as the fix was focusing on the combining of @RequestMapping annotations. In the code snippet above, org.springframework.web.servlet.mvc.condition.PathPatternsRequestCondition#combine is never called as org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#getMappingForMethod will only call it if the RequestMappingInfo is available on the type. Here, it's null so the combination never happens.

#29625 did change the following, when the controller type itself is annotated with @RequestMapping.

@RestController
@RequestMapping
public class HomeController {

    @GetMapping
    public String home() {
        return "Hello, World!";
    }

}
Licensed under CC BY-NC-SA 4.0
最后更新于 Jan 06, 2025 05:52 UTC
comments powered by Disqus
Built with Hugo
主题 StackJimmy 设计
Caret Up