【问题标题】:SpringBoot @RestController, Ambiguous mapping foundSpringBoot @RestController,发现不明确的映射
【发布时间】:2015-05-10 14:54:34
【问题描述】:

您好,我的示例中有一个简单的 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 中不能有多个 RequestMapping 吗?

非常感谢回复

【问题讨论】:

    标签: spring rest controller spring-boot ambiguous-call


    【解决方案1】:

    您必须使用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);
    }
    

    【讨论】:

    • @JurajKubica 不客气。然后你就可以接受这个答案了。
    【解决方案2】:

    或者你可以使用,

    @GetMapping("/getName")
    

    与带值的方法用法相同,是新版本用请求映射值指定方法="POST"。

    【讨论】:

      【解决方案3】:

      RequestMapping(value="/name") 中,始终使用路径值而不是名称。 您也可以明智地使用方法 GETMapping("/getname") PostMapping("/addname")

      【讨论】:

        猜你喜欢
        • 2018-03-18
        • 1970-01-01
        • 1970-01-01
        • 2019-08-03
        • 2012-02-13
        • 2014-01-11
        • 2016-12-25
        • 2016-02-24
        • 2015-07-09
        相关资源
        最近更新 更多