java.lang.IllegalStateException:映射不明确。无法映射方法

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

虽然我是 spring-mvc 的新手,但下面这段代码中的概念看起来很清楚。但是我得到了下面提到的异常,我把它放在了抛出的代码之后:

@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);
    }}

下面的异常讨论了看起来不正确的资源中的歧义。请建议。

 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>

对于两个相同的 URI,您有两个 POST 操作:

/relationship

您必须通过添加扩展 URI 来更改 POST 操作:

@PostMapping(value = "/some-uri", name = (...))

POST 请求的两种方法。那就是问题所在。尝试添加 path 属性以消除歧义。

    @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);
    }

因为当你调用POST /relationship时Spring不知道调用哪个方法(因为你有2个)。

一旦你解决了这个问题,你就应该像 /relationship/all/relationship/info 之类的称呼它们。