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

您所在的位置:网站首页 专科毕业可以自考本科吗 Ambiguous handler methods mapped for 'xxx'的解决办法

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

2023-09-07 11:03| 来源: 网络整理| 查看: 265

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 opt = bookService.findById(id); Book book = opt.get(); log.warn(book.toString()); return book; } @RequestMapping("/book/{title}") public Page getBookByTitleHighLight(@PathVariable("title") String title) { Page 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 opt = bookService.findById(id); Book book = opt.get(); log.warn(book.toString()); return book; } @RequestMapping("/book/title/{title}") public Page getBookByTitleHighLight(@PathVariable("title") String title) { Page books = bookService.findByTitle(title, 1, 10); log.warn(books.toString()); return books; } }

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

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



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3