Spring Boot @RestController,发现不明确的映射

标签 spring rest controller spring-boot ambiguous-call

嗨,我的示例中有一个简单的 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/

相关文章:

java - 替换 Controller 中的文本并将其放入属性文件中

java - 在 @Transactional 中提交 try-catch 中的更改

java - java spring mvc 如何显示不按 id 的另一列?

java - lwjgl 2 Controller 未初始化错误?

ruby-on-rails - 如果我调用 Factory.build 以使我的 Controller 测试快速,我怎样才能让 Factory Girl 永远不会访问数据库?

asp.net-mvc-3 - MEFContrib.MVC3:导出具有基类的 Controller

java - Ajax、Java 和 Hibernate

javascript - 需要确认我对 Servlet 与 RESTful 网页差异的理解

azure - 如何使用 Azure REST API 将文件部署到 Azure Web 应用服务?

css - 为我的主页设置一个与 rails 站点的其余部分不同的导航栏的最佳方法是什么?