使用feign 的时候遇到了一个坑,不知道有没有人也遇到。
可是编译会出现如下错误:
编译就通过了。
问题描述:
定义好 feign client的interface以后 的java代码
@FeignClient(value = "test")
public interface TeacApiClient {
@RequestMapping(method = RequestMethod.GET, value = "/{test}")
public Object saveUpVersion(@Param("test") String test);
}
为了使用consul动态发现服务的url,所以需要自己构建Feign的client:
this.httpClient = Feign.builder().logLevel(Logger.Level.BASIC).target(TeacApiClient.class, url);
可是编译会出现如下错误:
Caused by: java.lang.IllegalStateException: Method saveUpVersion not annotated with HTTP method type (ex. GET, POST)
at feign.Util.checkState(Util.java:128) ~[feign-core-9.5.0.jar:na]
at feign.Contract$BaseContract.parseAndValidateMetadata(Contract.java:97) ~[feign-core-9.5.0.jar:na]
at feign.Contract$BaseContract.parseAndValidatateMetadata(Contract.java:64) ~[feign-core-9.5.0.jar:na]
at feign.ReflectiveFeign$ParseHandlersByName.apply(ReflectiveFeign.java:146) ~[feign-core-9.5.0.jar:na]
at feign.ReflectiveFeign.newInstance(ReflectiveFeign.java:53) ~[feign-core-9.5.0.jar:na]
at feign.Feign$Builder.target(Feign.java:218) ~[feign-core-9.5.0.jar:na]
at feign.Feign$Builder.target(Feign.java:214) ~[feign-core-9.5.0.jar:na]
at se.ericsson.bid.teso.saveupversion.services.TeacSaveUpVersionService.buildUpHttpClient(TeacSaveUpVersionService.java:49) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_131]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_131]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_131]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_131]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:701) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
... 30 common frames omitted
但是使用
@RequestLine("POST /{test}")
public Object saveUpVersion(@Param("test") String test);
编译就通过了。
可是总觉得这样写不太优雅。
解决方法:
官方文档也是用的@RequestMapping并没有这个问题, google了好久也没有好的解决方案,隐约感觉用该是feign使用的 mvn contract不太对,因为有人讨论 使用default feign contract 会出现同样的问题,所以代码改成:this.httpClient = Feign.builder().logLevel(Logger.Level.BASIC).contract(new SpringMvcContract()).target(TeacApiClient.class, url);
就可以了。应该是默认使用的Contract.default并不支持spring 的requestmapping标签.
或者:
使用 FeignClientsConfiguration 中生成的contract
@Import(FeignClientsConfiguration.class)
public class xxx {
@Autowired
private void buildUpHttpClient(Contract contract) {
this.httpClient = Feign.builder()
.contract(contract)
.target(TeacApiClient.class, getTeacUrl());
}