springmvc报Ambiguous handler methods mapped for HTTP path

    技术2022-09-01  96

    一、问题:映射重复导致的错误

    java代码如下:

          @RequestMapping(value = "/info/{remove}/{id}", method = RequestMethod.GET)         public String removeNewsById(@PathVariable("id") long id) {

    @RequestMapping(value = "/info/{fav}/{id}", method = RequestMethod.GET)     public String increaseFavoriteById(@PathVariable("id") long id) {

    报以下错误:

    message Request processing failed; nested exception is java.lang.IllegalStateException: Ambiguous handler methods mapped for HTTP path 'http://localhost:8080/zc-beauty2/news/info/fav/6':  {public java.lang.String com.zc.beauty.controller.NewsController.removeNewsById(long), public java.lang.String com.zc.beauty.controller.NewsController.increaseFavoriteById(long)}

    我的意图是分别访问 /info/remove/id 和/info/fav/id

    上述写法有问题:用{} 包裹起来的变量好像会被模糊化:

    /info/{remove}/{id}  和

    /info/{fav}/{id}

    的映射是重复的

    二、解决办法:

    写成如下格式:

    /info/remove/{id}

    对于确定的路径参数不要加{}

    问题解决。 这个问题还导致eclipse和idea的debug不能使用

    Processed: 0.013, SQL: 9