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

Spring java.lang.IllegalStateException:映射不明确。无法映射方法,spring,spring-mvc,restful-url,Spring,Spring Mvc,Restful Url,虽然我是SpringMVC rest新手,但在下面的代码中,这个概念看起来很清楚。但是我得到了下面提到的异常,我把它放在抛出的代码之后: @RestController @RequestMapping("/relationship") public class RelationshipEndpoint { private static final Logger LOG = LoggerFactory.getLogger(RelationshipEndpoint.class);

虽然我是SpringMVC 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);
    }}
@RestController
@请求映射(“/relationship”)
公共类关系端点{
私有静态最终记录器LOG=LoggerFactory.getLogger(RelationshipEndpoint.class);
@自动连线
私人IRelationshipService关系服务;
@后期映射(名称=ResourcePathConstants.RELATIONSHIP\u列表)
@应答器
公共响应获取关系列表(最终HttpServletRequest httpRequest,
@RequestBody@Valid RelationshipRequest请求){
ServiceResponse result=true?getResult(HrcaRelationshipListResponse.class,JsonConstants.RelationshipListResponse)
:relationshipService.getListofRelationships(null);
返回准备响应(结果);
}
@PostMapping(name=ResourcePathConstants.RELATIONSHIP\u INFO)
@应答器
公共响应getRelationshipInfo(最终HttpServletRequest httpRequest,
@RequestBody@Valid RelationshipIdRequest请求){
ServiceResponse结果=relationshipService.getRelationshipInfo(请求);
返回准备响应(结果);
}}
下面的异常讨论了资源中的模糊性,它看起来不正确。请建议

 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>
2018年4月19日12:49:47.585严重[localhost-startStop-1]org.apache.catalina.core.ContainerBase.addChild内部ContainerBase.addChild:开始:
org.apache.catalina.LifecycleeException:无法启动组件[StandardEngine[catalina].StandardHost[localhost].StandardContext[/HRCAWebApp]]
位于org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:167)
位于org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:752)
位于org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:728)
位于org.apache.catalina.core.StandardHost.addChild(StandardHost.java:734)
位于org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:986)
位于org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1857)
位于java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
在java.util.concurrent.FutureTask.run(FutureTask.java:266)处
位于java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
位于java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
运行(Thread.java:748)
原因:org.springframework.beans.factory.BeanCreationException:创建名为“requestMappingHandlerMapping”的bean时出错,该名称在org.springframework.web.servlet.config.annotation.delegatingWebMVCCConfiguration中定义:调用init方法失败;嵌套异常为java.lang.IllegalStateException:不明确映射。无法映射“relationshipEndpoint”方法
public org.springframework.http.ResponseEntity.com.company.api.endpoint.RelationshipEndpoint.getRelationshipInfo(javax.servlet.http.HttpServletRequest,com.company.core.io.model.web.request.RelationshipIdRequest)
到{[/relationship],methods=[POST]}:已经有'relationshipEndpoint'bean方法
public org.springframework.http.ResponseEntity

对于两个相同的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);
    }
@PostMapping(name=ResourcePathConstants.RELATIONSHIP_LIST,path={”/all})
@应答器
公共响应获取关系列表(最终HttpServletRequest httpRequest,
@RequestBody@Valid RelationshipRequest请求){
ServiceResponse result=true?getResult(HrcaRelationshipListResponse.class,JsonConstants.RelationshipListResponse)
:relationshipService.getListofRelationships(null);
返回准备响应(结果);
}
@PostMapping(name=ResourcePathConstants.RELATIONSHIP_INFO,path={“/INFO”})
@应答器
公共响应getRelationshipInfo(最终HttpServletRequest httpRequest,
@RequestBody@Valid RelationshipIdRequest请求){
ServiceResponse结果=relationshipService.getRelationshipInfo(请求);
返回准备响应(结果);
}
因为当您调用
POST/relationship
Spring时,它不知道要调用哪个方法(正如您所使用的2)


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

我就是这么做的,不是吗,它们是两个不同的URI。我看不出歧义在哪里。当您不在PostMapping on方法中写入属性“value”时,操作将指向来自heading注释的映射。在您的例子中,getListofRelationships和getRelationshipInfo从“RequestMapping”标题注释指向相同的uri“/relationship”。我认为您必须将
@PostMapping(name=ResourcePathConstants.relationship_[…])
更改为
@PostMapping(value=ResourcePathConstants.relationship_[…])
并且为了确保路径常量以
/
是开头,实际上我必须将其命名为
,而不是名称。谢谢。