I have a Spring Boot application where I am using @RequestMapping at the class level and @GetMapping at the method level. Here is a highly simplified version of my controller:
@RestController
@RequestMapping("/hello")
class HelloController {
@GetMapping("")
fun test(@RequestParam("name", required = false) name: String?): String {
return "hello " + (name ?: "world")
}
}
When I call /hello
without name
, it works fine and returns "hello world".
However, when I call /hello?name=banana
, it does not work as expected, giving me the following error:
No static resource hello.
I have tried using @GetMapping("/")
instead of @GetMapping("")
, but that does not allow me to call /hello
without adding the optional parameters (i.e. works with ?name=batman
). Is there a way to handle both cases (with and without the request parameter) correctly in Spring Boot?
Additional info: This is a regression failure, from an upgrade of Java from 17 to 21 and Spring Boot from 2.7.5 to 3.2.5. This used to work before this upgrade.
I have noted that the Spring Boot instructions state that
@RequestMapping cannot be used in conjunction with other @RequestMapping annotations that are declared on the same element (class, interface, or method). If multiple @RequestMapping annotations are detected on the same element, a warning will be logged, and only the first mapping will be used. This also applies to composed @RequestMapping annotations such as @GetMapping, @PostMapping, etc
But I am not sure how to avoid it for my use-case
I have a Spring Boot application where I am using @RequestMapping at the class level and @GetMapping at the method level. Here is a highly simplified version of my controller:
@RestController
@RequestMapping("/hello")
class HelloController {
@GetMapping("")
fun test(@RequestParam("name", required = false) name: String?): String {
return "hello " + (name ?: "world")
}
}
When I call /hello
without name
, it works fine and returns "hello world".
However, when I call /hello?name=banana
, it does not work as expected, giving me the following error:
No static resource hello.
I have tried using @GetMapping("/")
instead of @GetMapping("")
, but that does not allow me to call /hello
without adding the optional parameters (i.e. works with ?name=batman
). Is there a way to handle both cases (with and without the request parameter) correctly in Spring Boot?
Additional info: This is a regression failure, from an upgrade of Java from 17 to 21 and Spring Boot from 2.7.5 to 3.2.5. This used to work before this upgrade.
I have noted that the Spring Boot instructions state that
@RequestMapping cannot be used in conjunction with other @RequestMapping annotations that are declared on the same element (class, interface, or method). If multiple @RequestMapping annotations are detected on the same element, a warning will be logged, and only the first mapping will be used. This also applies to composed @RequestMapping annotations such as @GetMapping, @PostMapping, etc
But I am not sure how to avoid it for my use-case
Share Improve this question edited Nov 21, 2024 at 14:16 Robin Sving asked Nov 19, 2024 at 12:57 Robin SvingRobin Sving 3,4201 gold badge14 silver badges10 bronze badges 5 |1 Answer
Reset to default 1After some investigation I have found that the issue was not present in other places than my tests using RestAssured with an empty endpoint.
These were my tests originally
RestAssured.given().basePath("myapi/hello").`when`().get("") //<- succeeded
RestAssured.given().basePath("myapi/hello").`when`().get("?name=banana") //<- failed
After changing the tests this is the result
RestAssured.given().basePath("myapi").`when`().get("/hello") //<- succeeded
RestAssured.given().basePath("myapi").`when`().get("/hello?name=banana") //<- succeeded
Notably, this worked before upgrading to RestAssured 5.3.2, but not after
@GetMapping("/")
I can match thename
parameter, so I would guess that means that the parameter is retained properly – Robin Sving Commented Nov 19, 2024 at 13:26@RequestMapping
. However using a/
or not will lead to a different path as/hello
and/hello/
are different paths and mappings. – M. Deinum Commented Nov 19, 2024 at 13:31http://localhost:8080/hello?name=banana
, I've got a successful 200 response withhello banana
as body. The error you mention suggests that somewhere you've got a@Controller
or@ControllerAdvice
that have intercepted your request, as it mentions resource fetching. Have you checked that you do not have such annotated components ? – amanin Commented Nov 20, 2024 at 17:20