您好我的Sample中有一个简单的RestController:
@RestController
public class PersonController {
@RequestMapping(name = "/getName", method = GET)
public String getName() {
return "MyName";
}
@RequestMapping(name = "/getNumber", method = GET)
public Double getNumber(){
return new Double(0.0);
}
}
我有启动SpringBoot的SampleController:
@SpringBootApplication
@Controller
public class SampleController {
public static void main(String[] args) throws Exception {
SpringApplication.run(SampleController.class, args);
}
}
当我尝试运行SampleCotroller时,会发生以下异常:
Caused by: java.lang.IllegalStateException: Ambiguous mapping found. Cannot map 'personController' bean method
public java.lang.Double com.web.communication.PersonController.getNumber()
to {[],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}: There is already 'personController' bean method
public java.lang.String com.web.communication.PersonController.getName() mapped.
问题出在哪里?一个RestController中不能有更多的RequestMappings吗?
非常感谢回复
答案 0 :(得分:26)
您已使用value
属性来定义映射。您现在使用了name
,它只为映射提供了名称,但根本没有定义任何映射。所以目前你的方法都是未映射的(在这种情况下,两者都映射到相同的路径)。将方法更改为:
@RequestMapping(value = "/getName", method = GET)
public String getName() {
return "MyName";
}
@RequestMapping(value = "/getNumber", method = GET)
public Double getNumber(){
return new Double(0.0);
}
答案 1 :(得分:2)
或者你可以使用,
@GetMapping("/getName")
与value的方法相同,是指定方法的新版本=" POST"请求映射值。
答案 2 :(得分:0)
在 RequestMapping(value =“ / name”)中,始终将value用作路径而不是name。 您也可以明智地使用方法 GETMapping(“ / getname”) PostMapping(“ / addname”)