加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

SpringBoot @RestController,找到了不明确的映射

发布时间:2020-12-15 01:26:18 所属栏目:大数据 来源:网络整理
导读:嗨,我的Sample中有一个简单的RestController: @RestControllerpublic class PersonController { @RequestMapping(name = "/getName",method = GET) public String getName() { return "MyName"; } @RequestMapping(name = "/getNumber",method = GET) publi

嗨,我的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);
    }
}

我有SampleController用于启动SpringBoot:

@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属性来定义映射.您现在使用了名称,它只为映射提供了一个名称,但根本没有定义任何映射.所以目前你的方法都是未映射的(在这种情况下,两者都映射到相同的路径).将方法更改为:

@RequestMapping(value = "/getName",method = GET)
public String getName() {
    return "MyName";
}

@RequestMapping(value = "/getNumber",method = GET)
public Double getNumber(){
    return new Double(0.0);
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读