'How to solve Spring swagger Ambiguous mapping?

In my spring controller class i have below two methods

@GetMapping(value = "/published_messages", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<String> handleEmptyQueryParam() throws Exception
    {
        return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Invalid Request , No Request Param received in the request");
    }

    @GetMapping(value = "/published_messages", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<String> getEdiDetailsByusername(@RequestParam(required=false, value="username") String username) throws Exception
    {
        List<String> userList = userService.getUserList(username);
        return isAValidResponse(userList);
    }

with this when starting the app , got below exception

Caused by: java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'UserAppController' method 
public org.springframework.http.ResponseEntity<java.lang.String> com.rby.trans.controller.UserAppController.getEdiDetailsByusername(java.lang.String) throws java.lang.Exception
to {[/published_messages],methods=[GET],produces=[application/json]}: There is already 'UserAppController' bean method
public org.springframework.http.ResponseEntity<java.lang.String> com.tgt.trans.controller.UserAppController.handleEmptyQueryParam() throws java.lang.Exception mapped.
    at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping$MappingRegistry.assertUniqueMethodMapping(AbstractHandlerMethodMapping.java:576)
    at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping$MappingRegistry.register(AbstractHandlerMethodMapping.java:540)
    at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.registerHandlerMethod(AbstractHandlerMethodMapping.java:264)
    at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.detectHandlerMethods(AbstractHandlerMethodMapping.java:250)
    at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.initHandlerMethods(AbstractHandlerMethodMapping.java:214)
    at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.afterPropertiesSet(AbstractHandlerMethodMapping.java:184)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.afterPropertiesSet(RequestMappingHandlerMapping.java:127)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1642)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1579)
    ... 50 common frames omitted

seems something issue with swagger , which we have below dependencies in our build file

compile group: 'io.springfox', name: 'springfox-swagger2', version: '2.4.0'
    compile group: 'io.springfox', name: 'springfox-swagger-ui', version: '2.4.0'

can anyone suggest what to in this case ,

whatever am following and doing in the rest controller is correct only and it is possible to do that in spring , then why swagger giving errors on that ?

Note : I got the answer , we need to use params attribute in the @GetMapping , it solved my issue .

Thanks to all .


Solution 1:[1]

I had a similar error some time ago when I was creating a demo app during a presentation. The problem why this issue occurred was because in one of my endpoints mappings (e.g. other @GetMapping) I used 'name' instead of 'value' attribute so the problem might be in the part of the code which you did not provide.

Solution 2:[2]

We had the same error specifically for SpringBoot 2.5, but with the following setup

Setup:

  • Controller Api:
@Api(value = "items", description = "the items API")
public interface ItemsApi {

    @GetMapping(value = "items/{itemId}",
            produces = {"application/json"})
    Item getItem(@ApiParam(value = "The item Id number in the app, a UUID", required = true) @PathVariable("itemId") UUID itemId,
                       HttpServletRequest httpServletRequest);

}
  • Feign Client Api:
@FeignClient(value = "Foo-v1-0-Server", contextId = "Foo-v1-0-Server-ItemClient")
@RequestMapping("/")
public interface FooFeignClient {
    @ApiOperation(value = "Gets a list of item metadata")
    @ApiResponses(value = {
      @ApiResponse(code = 200, message = "Success", responseContainer = "List", response = FooItemResponse.class),
      @ApiResponse(code = 401, message = "Unauthorized"),
      @ApiResponse(code = 404, message = "Not Found"),
      @ApiResponse(code = 500, message = "Failure")})
    @RequestMapping(value = "items/{itemId}", method = RequestMethod.GET, produces = "application/json")
    List<FooItemResponse> getDocumentList(@PathVariable("itemId") String itemId);

Error:

java.lang.IllegalStateException: Ambiguous mapping. Cannot map ‘com.contoso.foo.client.feign.FooFeignClient

Cause:

We tracked it down to feign client having the @RequestMapping annotation at the class level. This was causing it to be picked up for WebMvcRegistrations.

Resolution:

Remove the @RequestMapping annotation at the class level on the feign client and republish a new version.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Strk89
Solution 2 Vijay Vepakomma