Spring mvc Ambiguous mapping found. Cannot map controller bean method

一笑奈何 提交于 2019-11-29 02:53:26
Alexey Semenyuk

You should write

@Controller("/review")
public class ReviewController {

and

@Controller("/book")
public class BookController {

because in your code you have the two methods without an explicit/unique path for mapping(eg. if we have a call /edit/1 , this is impossible clearly to determine a controller's method from your editBook BookController or ReviewController editReview)

gilberto Nd

If the issue is about ambigious method, probably the @RequestMapping should be the issue. Change from @RequestMapping(name =...) to @RequestMapping(value =...)

@RequestMapping(name = "xxx.htm", method = RequestMethod.GET)

to

@RequestMapping(value = "xxx.htm", method = RequestMethod.GET)

For me adding "params" attribute in @RequestMapping worked as shown

  @ResponseBody
  @RequestMapping(method = RequestMethod.GET, params = {"id"})
  public User getUserById(final @RequestParam(name="id", required = true) String Id)
    throws InvalidArgumentException {

    return userService.getUserById(UUID.fromString(Id));
  }

  /**
   * REST service endpoint.
   * @param name Unique name for the user in the system.
   * @return Object of type {@link User} if exists otherwise null.
   */
  @ResponseBody
  @RequestMapping(method = RequestMethod.GET, params = {"name"})
  public User getUserByName(final @RequestParam(name="name", required = true) String name)
    throws InvalidArgumentException {

    return userService.getUserByName(name);
  }

However adding both the parameters at a time in query string will give 500 error with message :

Ambiguous handler methods mapped for HTTP path

In that case you can have another controller method taking both params and but only uses one of them which I feel is not necessary.

In my case I had two methods in two different controllers in a Spring boot application, both with the same @DeleteMapping("/detailbonadelete/{id}") // delete, I changed one of them to @DeleteMapping("/bonadelete/{id}") // delete and it works.

The same thing if you have:

@PutMapping("/detailbonaupdate/{id}") // update

@PostMapping("/detailbonainsert") // insert

@GetMapping("/detailbonaidbon/{idBona}") // selectByBonaId

Make sure it is unique for every controller.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!