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

Posted

技术标签:

【中文标题】java.lang.IllegalStateException:不明确的映射。无法映射方法【英文标题】:java.lang.IllegalStateException: Ambiguous mapping. Cannot map method 【发布时间】:2018-04-19 07:36:05 【问题描述】:

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

@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>

【问题讨论】:

【参考方案1】:

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 这样称呼它们。

【讨论】:

是的,确实我必须将其命名为 value 而不是名称。非常感谢。【参考方案2】:

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

/relationship

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

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

【讨论】:

这就是我所做的,不是吗,它们是 2 个不同的 URI。我看不出哪里有歧义。 当您不在 PostMapping on 方法中写入属性 'value' 时,动作指向来自标题注释的映射。在您的情况下,getListofRelationships 和 getRelationshipInfo 指向“RequestMapping”标题注释中的相同 uri“/relationship”。 我认为您必须将 @PostMapping(name = ResourcePathConstants.RELATIONSHIP_[...]) 更改为 @PostMapping(value = ResourcePathConstants.RELATIONSHIP_[...]) 并确保您的路径常量以 / 开头

以上是关于java.lang.IllegalStateException:不明确的映射。无法映射方法的主要内容,如果未能解决你的问题,请参考以下文章