无法让 Feign Client 工作。首先尝试使用 POST。不断遇到与编码器/解码器相关的错误,说类型不正确。然后在github上找到了一个调用简单GET API的例子,决定试一试。仍然失败
在 Github 和在线上,我看到多个版本的 Feign Client Spring-Cloud、OpenFeign、Netflix.feign 有不同的版本。谁能描述一个应该用于生产的最好和稳定的 Feign 客户端是什么?
package com.paa.controllers;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@FeignClient (name="test-service",url="https://www.reddit.com/r")
public interface GetFeignClient {
@RequestMapping(method = RequestMethod.GET, value = "/java.json")
public String posts();
}
Controller:
@RestController
@RequestMapping("/some/api")
public class TestWLCController {
@Autowired
private GetFeignClient getFeignClient;
.. some stuff
@RequestMapping(value="/postSomething",method = RequestMethod.POST)
@ApiOperation(value = "Configures something",
notes = "basic rest controller for testing feign")
public ResponseEntity<SomeResponse> feignPost(
UriComponentsBuilder builder,
@ApiParam(name = "myRequest",
value = "request for configuring something",
required = true)
@Valid @RequestBody SomeRequest someRequest) {
String resp = null;
try {
resp = getFeignClient.posts();
} catch (Exception er) {
er.printStackTrace();
}
}
}
应用:
尝试了所有可能的注释排列,认为它可以解决 AutoWire 的问题,但仍然失败
@Configuration
@ComponentScan
@EnableAutoConfiguration
//@EnableEurekaClient
@EnableFeignClients
//@SpringBootApplication
//@EnableFeignClients
//@EnableFeignClients(basePackages = {"com.paa.xenia.controllers", "com.paa.xenia.services"})
public class ServiceApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(XeniaServiceApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(ServiceApplication.class, args);
}
}
2016-07-20 18:15:42.406[0;39m [31mERROR[0;39m [35m32749[0;39m [2m---[0;39m [2m[主][0;39m [36mo.s.boot .SpringApplication [0;39m [2m:[0;39m 应用程序启动失败
org.springframework.beans.factory.BeanCreationException:创建名为“testWLCController”的bean时出错:注入自动装配的依赖项失败;嵌套异常是 org.springframework.beans.factory.BeanCreationException:无法自动装配字段:私有 com.paa.controllers.GetFeignClient com.paa.controllers.TestWLCController.gfClient;嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名称为“com.aa..controllers.GetFeignClient”的 bean 时出错:FactoryBean 在创建对象时抛出异常;嵌套异常是 java.lang.NullPointerException at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] at组织。run(SpringApplication.java:1180) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE] at com.paa.ServiceApplication.main(ServiceApplication.java:44) [bin/:na] 由:org.springframework.beans.factory.BeanCreationException:无法自动装配字段:私有com.paa.controllers.GetFeignClient com.paa.controllers.TestWLCController.gfClient;嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名称为“com.paa.controllers.GetFeignClient”的 bean 时出错:FactoryBean 在创建对象时抛出异常;嵌套异常是 java.lang.NullPointerException 在 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:573) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE ] 在 org.springframework.beans。