0
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

如何解决此错误?

4

3 回答 3

1

我认为您的 feign 客户端的方法签名的 @RequestMapping 注释缺少方法参数,例如 GET 或 POST - 请参见下面的代码示例:

@FeignClient("client")
public interface MyClient {
  @RequestMapping(method = RequestMethod.GET, value = "/path-to-endpoint")
  MyResponse getMyResponse();
}
于 2016-04-05T15:16:10.640 回答
1

当 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 初始化上述 feign 客户@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);
}

根据我们的需要创建两个 feign-client,一个用于普通用户,另一个用于 admin

@FeignClient("PhotoClient")
public interface PhotoClient extends PhotoApi{}
@FeignClient("AdminPhotoClient")
@RequestMapping({"/admin"})
interface AdminPhotoClient extends PhotoApi{}
于 2020-05-06T17:04:22.060 回答
0

观察到的 feign 客户端总是需要 spring-cloud-dependencies 作为依赖管理。尝试添加它

于 2020-02-05T02:23:38.473 回答