两个具有相同 URL 但参数不同的 @GetMapping
Two @GetMapping with same URL but different parameters
我正在学习 Spring MVC,但在学习过程中我遇到了这个问题:
// http://localhost:8080/todo-list/welcomeWithParam?user=Stefan
@GetMapping("welcomeWithParam")
public String welcome91(@RequestParam String user, Model model) {
model.addAttribute("helloThroughParam", demoService.getHelloMessage(user));
return "welcome-with-model";
}
// http://localhost:8080/todo-list/welcomeWithParam?user=Stefan&age=31
@GetMapping("welcomeWithParam")
public String welcome92(@RequestParam String user, @RequestParam int age, Model model) {
model.addAttribute("helloThroughParam", demoService.getHelloMessage(user));
model.addAttribute("age", age);
return "welcome-with-model";
}
我收到这个错误:
原因:java.lang.IllegalStateException:映射不明确。无法映射 'demoController' 方法
academy.learnprogramming.controller.DemoController#welcome92(字符串、整数、模型)
to {GET [/welcomeWithParam]}: 已经有'demoController'个bean方法
academy.learnprogramming.controller.DemoController#welcome91(String, Model) 已映射。
所以,Spring 告诉我我们不能有两个具有相同 URL 但不同 number/type 参数的 GET 映射?
如果我更改其中一个 @GetMapping("") 值的值,它工作正常。
像这样:
@RequestMapping(value = "/welcomeWithParam", params = "user")
public String welcome91(@RequestParam String user, Model model) {
// ...
}
@RequestMapping(value = "/welcomeWithParam", params = {"user","age"})
public ModelAndView welcome92(@RequestParam String user, @RequestParam int age, Model model) {
// ...
}
我正在学习 Spring MVC,但在学习过程中我遇到了这个问题:
// http://localhost:8080/todo-list/welcomeWithParam?user=Stefan
@GetMapping("welcomeWithParam")
public String welcome91(@RequestParam String user, Model model) {
model.addAttribute("helloThroughParam", demoService.getHelloMessage(user));
return "welcome-with-model";
}
// http://localhost:8080/todo-list/welcomeWithParam?user=Stefan&age=31
@GetMapping("welcomeWithParam")
public String welcome92(@RequestParam String user, @RequestParam int age, Model model) {
model.addAttribute("helloThroughParam", demoService.getHelloMessage(user));
model.addAttribute("age", age);
return "welcome-with-model";
}
我收到这个错误:
原因:java.lang.IllegalStateException:映射不明确。无法映射 'demoController' 方法 academy.learnprogramming.controller.DemoController#welcome92(字符串、整数、模型) to {GET [/welcomeWithParam]}: 已经有'demoController'个bean方法 academy.learnprogramming.controller.DemoController#welcome91(String, Model) 已映射。
所以,Spring 告诉我我们不能有两个具有相同 URL 但不同 number/type 参数的 GET 映射?
如果我更改其中一个 @GetMapping("") 值的值,它工作正常。
像这样:
@RequestMapping(value = "/welcomeWithParam", params = "user")
public String welcome91(@RequestParam String user, Model model) {
// ...
}
@RequestMapping(value = "/welcomeWithParam", params = {"user","age"})
public ModelAndView welcome92(@RequestParam String user, @RequestParam int age, Model model) {
// ...
}