Ambiguous handler methods mapped for 'xxx'的解决办法

Ambiguous handler methods mapped for '/book/es'

一. 问题描述

我们在创建Restful风格的url的时候,可以直接将参数变量值放入到url中,然后传递到后台,后台自动识别对应的方法,非常的方便.

但是如果我们的接口方法中出现了方法重载的情况,则有可能会出现一些问题,如下:

package com.syc.boot.web;

import com.syc.boot.domain.Book;
import com.syc.boot.service.BookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Optional;

@Slf4j
@RestController
public class ElasticController {

    @Autowired
    private BookService bookService;

    @RequestMapping("/book/{id}")
    public Book getBookById(@PathVariable String id) {
        Optional<Book> opt = bookService.findById(id);
        Book book = opt.get();
        log.warn(book.toString());
        return book;
    }

    @RequestMapping("/book/{title}")
    public Page<Book> getBookByTitleHighLight(@PathVariable("title") String title) {
        Page<Book> books = bookService.findByTitle(title, 1, 10);
        log.warn(books.toString());
        return books;
    }

}

此时在浏览器地址栏访问 http://localhost:8080/book/1 或 http://localhost:8080/book/es教程 时均会出现如下错误.

java.lang.IllegalStateException: Ambiguous handler methods mapped for '/book/es': {public com.syc.boot.domain.Book com.syc.boot.web.ElasticController.getBookById(java.lang.String), public org.springframework.data.domain.Page com.syc.boot.web.ElasticController.getBookByTitleHighLight(java.lang.String)}

意思是说我们的Controller中,有两个模棱两可的处理方法,这两个方法有歧义,无法分清谁是谁.因为Spring无法根据传参的类型自动匹配到可以处理的方法.

此时如果我们依然想使用Restful编程风格,就必须改变请求得url格式,保证url对应的方法不会产生歧义.

二. 解决办法

针对上面的例子,只要保证一个类中多个方法请求的接口方法不一样就可以了,可以是请求方法,请求参数,url等不同,这样就可以匹配到不同的方法.

package com.syc.boot.web;

import com.syc.boot.domain.Book;
import com.syc.boot.service.BookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Optional;

@Slf4j
@RestController
public class ElasticController {

    @Autowired
    private BookService bookService;

    @RequestMapping("/book/id/{id}")
    public Book getBookById(@PathVariable String id) {
        Optional<Book> opt = bookService.findById(id);
        Book book = opt.get();
        log.warn(book.toString());
        return book;
    }

    @RequestMapping("/book/title/{title}")
    public Page<Book> getBookByTitleHighLight(@PathVariable("title") String title) {
        Page<Book> books = bookService.findByTitle(title, 1, 10);
        log.warn(books.toString());
        return books;
    }

}

此时再到浏览器地址栏通过 http://localhost:8080/user/id/1 或 http://localhost:8080/user/title/es 来请求.

就可以可以正常访问到后台的对象内容了.

  • 36
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一一哥Sun

您的鼓励是我继续创作的动力哦

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值