Spring mvc Ambiguous mapping found. Cannot map controller bean method

前端 未结 4 1925
悲&欢浪女
悲&欢浪女 2020-12-15 15:39

I am trying to build an app which can list some values from the database and modify, add, delete if necessary using Spring 4 and i receive the following error(only if the \"

相关标签:
4条回答
  • 2020-12-15 16:09

    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)

    0 讨论(0)
  • 2020-12-15 16:09

    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)
    
    0 讨论(0)
  • 2020-12-15 16:22

    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.

    0 讨论(0)
  • 2020-12-15 16:29

    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.

    0 讨论(0)
提交回复
热议问题