嗨,我的示例中有一个简单的 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 吗?
非常感谢回复
最佳答案
您必须使用 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);
}
关于Spring Boot @RestController,发现不明确的映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28942209/