Spring Boot Feign客户端请求行

2eafrhcq  于 2023-02-08  发布在  Spring
关注(0)|答案(2)|浏览(143)

我想从Spring云的客户端服务获得响应FeignClint我编写了一个FeignClient接口

@FeignClient(name = "currencyClient", url = "https://example", configuration = FeignConfig.class)
public interface CurrencyClient {
    @RequestLine(value = "GET /{name}/{date}")
    @Headers("Content-Type: application/json")
    Currency getByNameAndDate(@Param String name, @Param LocalDateTime date);
}

但它没有编译,正在发生此错误

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'uz.md.shopapp.controller.client.CurrencyClient': FactoryBean threw exception on object creation
    at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:154) ~[spring-beans-6.0.4.jar:6.0.4]
    at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:90) ~[spring-beans-6.0.4.jar:6.0.4]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanInstance(AbstractBeanFactory.java:1823) ~[spring-beans-6.0.4.jar:6.0.4]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getObjectForBeanInstance(AbstractAutowireCapableBeanFactory.java:1265) ~[spring-beans-6.0.4.jar:6.0.4]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:259) ~[spring-beans-6.0.4.jar:6.0.4]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) ~[spring-beans-6.0.4.jar:6.0.4]
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:254) ~[spring-beans-6.0.4.jar:6.0.4]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.addCandidateEntry(DefaultListableBeanFactory.java:1628) ~[spring-beans-6.0.4.jar:6.0.4]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1585) ~[spring-beans-6.0.4.jar:6.0.4]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1368) ~[spring-beans-6.0.4.jar:6.0.4]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1325) ~[spring-beans-6.0.4.jar:6.0.4]
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:885) ~[spring-beans-6.0.4.jar:6.0.4]
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:789) ~[spring-beans-6.0.4.jar:6.0.4]
    ... 38 common frames omitted
Caused by: java.lang.IllegalStateException: Method CurrencyClient#getByNameAndDate(String,LocalDateTime) not annotated with HTTP method type (ex. GET, POST)
Warnings:
- 
    at feign.Util.checkState(Util.java:136) ~[feign-core-12.1.jar:na]
    at feign.Contract$BaseContract.parseAndValidateMetadata(Contract.java:115) ~[feign-core-12.1.jar:na]
    at org.springframework.cloud.openfeign.support.SpringMvcContract.parseAndValidateMetadata(SpringMvcContract.java:193) ~[spring-cloud-openfeign-core-4.0.1.jar:4.0.1]
    at feign.Contract$BaseContract.parseAndValidateMetadata(Contract.java:65) ~[feign-core-12.1.jar:na]
    at feign.ReflectiveFeign$ParseHandlersByName.apply(ReflectiveFeign.java:142) ~[feign-core-12.1.jar:na]
    at feign.ReflectiveFeign.newInstance(ReflectiveFeign.java:58) ~[feign-core-12.1.jar:na]
    at feign.ReflectiveFeign.newInstance(ReflectiveFeign.java:50) ~[feign-core-12.1.jar:na]
    at feign.Feign$Builder.target(Feign.java:197) ~[feign-core-12.1.jar:na]
    at org.springframework.cloud.openfeign.DefaultTargeter.target(DefaultTargeter.java:30) ~[spring-cloud-openfeign-core-4.0.1.jar:4.0.1]
    at org.springframework.cloud.openfeign.FeignClientFactoryBean.getTarget(FeignClientFactoryBean.java:470) ~[spring-cloud-openfeign-core-4.0.1.jar:4.0.1]
    at org.springframework.cloud.openfeign.FeignClientFactoryBean.getObject(FeignClientFactoryBean.java:421) ~[spring-cloud-openfeign-core-4.0.1.jar:4.0.1]
    at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:148) ~[spring-beans-6.0.4.jar:6.0.4]
xoshrz7s

xoshrz7s1#

您应该尝试注解@PathVariable@GetMapping

@GetMapping(path = "/{name}/{date}")
    Currency getByNameAndDate(@PathVariable String name, @PathVariable LocalDateTime date);
idfiyjo8

idfiyjo82#

客户端应按以下方式设置:

public interface CurrencyClient {
    @RequestMapping(method = RequestMethod.GET, value = "/{name}/{date}")
    @Headers("Content-Type: application/json")
    List<Store> getByNameAndDate(@PathVariable("name") String name, @PathVariable("date") Date date);
}

相关问题