java.lang.IllegalStateException:方法[方法名称]只能包含1个方法字段。发现:[]

时间:2016-04-05 05:52:14

标签: spring-boot netflix-eureka spring-cloud-feign

Caused by: java.lang.IllegalStateException: Method findByApplicationName can only contain 1 method field. Found: []
    at feign.Util.checkState(Util.java:117) ~[feign-core-8.15.1.jar:8.15.1]
    at org.springframework.cloud.netflix.feign.support.SpringMvcContract.checkOne(SpringMvcContract.java:180) ~[spring-cloud-netflix-core-1.1.0.BUILD-SNAPSHOT.jar:1.1.0.BUILD-SNAPSHOT]
    at org.springframework.cloud.netflix.feign.support.SpringMvcContract.processAnnotationOnMethod(SpringMvcContract.java:143) ~[spring-cloud-netflix-core-1.1.0.BUILD-SNAPSHOT.jar:1.1.0.BUILD-SNAPSHOT]
    at feign.Contract$BaseContract.parseAndValidateMetadata(Contract.java:92) ~[feign-core-8.15.1.jar:8.15.1]
    at org.springframework.cloud.netflix.feign.support.SpringMvcContract.parseAndValidateMetadata(SpringMvcContract.java:100) ~[spring-cloud-netflix-core-1.1.0.BUILD-SNAPSHOT.jar:1.1.0.BUILD-SNAPSHOT]
    at feign.Contract$BaseContract.parseAndValidatateMetadata(Contract.java:61) ~[feign-core-8.15.1.jar:8.15.1]
    at feign.ReflectiveFeign$ParseHandlersByName.apply(ReflectiveFeign.java:140) ~[feign-core-8.15.1.jar:8.15.1]
    at feign.ReflectiveFeign.newInstance(ReflectiveFeign.java:58) ~[feign-core-8.15.1.jar:8.15.1]
    at feign.Feign$Builder.target(Feign.java:198) ~[feign-core-8.15.1.jar:8.15.1]
    at org.springframework.cloud.netflix.feign.FeignClientFactoryBean$DefaultTargeter.target(FeignClientFactoryBean.java:203) ~[spring-cloud-netflix-core-1.1.0.BUILD-SNAPSHOT.jar:1.1.0.BUILD-SNAPSHOT]
    at org.springframework.cloud.netflix.feign.FeignClientFactoryBean.loadBalance(FeignClientFactoryBean.java:153) ~[spring-cloud-netflix-core-1.1.0.BUILD-SNAPSHOT.jar:1.1.0.BUILD-SNAPSHOT]
    at org.springframework.cloud.netflix.feign.FeignClientFactoryBean.getObject(FeignClientFactoryBean.java:173) ~[spring-cloud-netflix-core-1.1.0.BUILD-SNAPSHOT.jar:1.1.0.BUILD-SNAPSHOT]
    at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:168) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
    ... 40 common frames omitted

如何解决此错误?

3 个答案:

答案 0 :(得分:1)

我认为您的假装客户端方法签名的@RequestMapping注释缺少方法参数,例如获取或发布 - 请参阅下面的代码示例:

@FeignClient("client")
public interface MyClient {
  @RequestMapping(method = RequestMethod.GET, value = "/path-to-endpoint")
  MyResponse getMyResponse();
}

答案 1 :(得分:0)

观察到的伪装客户端始终需要spring-cloud-dependencies作为依赖项管理。尝试添加它

答案 2 :(得分:0)

当Feign Client的RequestMapping具有多个主映射值时,将发生这种类型的问题。

例如

@FeignClient("PhotoClient")
public interface PhotoClient {
    @GetMapping(value = {"/photo/{photoId}", "/admin/photo/{photoId}"}, produces = APPLICATION_JSON_VALUE)
    ResponseEntity<PhotoDto> getPhoto(@PathVariable("photoId") String photoId);
}

在上面的示例GetMapping中引用了两个主要映射,例如"/photo/{photoId}""/admin/photo/{photoId}"

如果您使用Spring Boot的@EnableFeignClients来初始化上述伪客户端

@EnableFeignClients(basePackageClasses = {PhotoApi.class})

然后将引发以下异常

FactoryBean在对象创建时引发异常;嵌套异常为 java.lang.IllegalStateException:方法getPhoto只能包含在 最多1个值字段。找到:[/ photo / {photoId},/ admin / photo / {photoId}]

很遗憾,没有现成的配置可以解决此问题。因此,下面的继承模式是一种简单的解决方案:

在公共接口中声明请求方法

public interface PhotoApi {
    @GetMapping(value = {"/photo/{photoId}"}, produces = APPLICATION_JSON_VALUE)
    ResponseEntity<PhotoDto> getPhoto(@PathVariable("photoId") String photoId);
}

根据我们的需要创建两个伪装客户端,一个用于普通用户,另一个用于管理员

@FeignClient("PhotoClient")
public interface PhotoClient extends PhotoApi{}
@FeignClient("AdminPhotoClient")
@RequestMapping({"/admin"})
interface AdminPhotoClient extends PhotoApi{}