Search code examples
springspring-mvcrestful-url

java.lang.IllegalStateException: Ambiguous mapping. Cannot map method


Though am new to spring-mvc rest the concept looks clear in this code below. But I get the below mentioned exception which I have put after the code that is getting thrown :

@RestController
@RequestMapping("/relationship")
public class RelationshipEndpoint  {

    private static final Logger LOG = LoggerFactory.getLogger(RelationshipEndpoint.class);

    @Autowired
    private IRelationshipService relationshipService;

    @PostMapping(name = ResourcePathConstants.RELATIONSHIP_LIST)
    @ResponseBody
    public ResponseEntity<ServiceResponse> getListofRelationships(final HttpServletRequest httpRequest,
            @RequestBody @Valid RelationshipRequest request) {

        ServiceResponse result = true ? getResult(HrcaRelationshipListResponse.class, JsonConstants.RelationshipListResponse)
                : relationshipService.getListofRelationships(null);
        return prepareResponse(result);
    }

    @PostMapping(name = ResourcePathConstants.RELATIONSHIP_INFO)
    @ResponseBody
    public ResponseEntity<ServiceResponse> getRelationshipInfo(final HttpServletRequest httpRequest,
            @RequestBody @Valid RelationshipIdRequest request) {

        ServiceResponse result = relationshipService.getRelationshipInfo(request);
        return prepareResponse(result);
    }}

The exception below talk about ambiguity in resource which looks incorrect. kindly suggest.

 19-Apr-2018 12:49:47.585 SEVERE [localhost-startStop-1] org.apache.catalina.core.ContainerBase.addChildInternal ContainerBase.addChild: start: 
 org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/HRCAWebApp]]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:167)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:752)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:728)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:734)
    at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:986)
    at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1857)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerMapping' defined in org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration: Invocation of init method failed; nested exception is java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'relationshipEndpoint' method 
public org.springframework.http.ResponseEntity<com.company.core.io.model.web.response.ServiceResponse> com.company.api.endpoint.RelationshipEndpoint.getRelationshipInfo(javax.servlet.http.HttpServletRequest,com.company.core.io.model.web.request.RelationshipIdRequest)
to {[/relationship],methods=[POST]}: There is already 'relationshipEndpoint' bean method
public org.springframework.http.ResponseEntity<com.company.core.io.model.web.response.ServiceResponse>

Solution

  • Two methods for POST request. That is the problem. Try to add path attribute to remove the ambiguity.

        @PostMapping(name = ResourcePathConstants.RELATIONSHIP_LIST, path = {"/all"})
        @ResponseBody
        public ResponseEntity<ServiceResponse> getListofRelationships(final HttpServletRequest httpRequest,
                @RequestBody @Valid RelationshipRequest request) {
    
            ServiceResponse result = true ? getResult(HrcaRelationshipListResponse.class, JsonConstants.RelationshipListResponse)
                    : relationshipService.getListofRelationships(null);
            return prepareResponse(result);
        }
    
        @PostMapping(name = ResourcePathConstants.RELATIONSHIP_INFO, path = {"/info"})
        @ResponseBody
        public ResponseEntity<ServiceResponse> getRelationshipInfo(final HttpServletRequest httpRequest,
                @RequestBody @Valid RelationshipIdRequest request) {
    
            ServiceResponse result = relationshipService.getRelationshipInfo(request);
            return prepareResponse(result);
        }
    

    Because when you call POST /relationship Spring does not know which method to call (as you have 2).

    And once you fix this you should call them like /relationship/all and /relationship/info or something like that.