spring cloud Feign使用@RequestLine

如何在微服务项目中调用其它项目的接口试使用spring cloud feign声明式调用。

/**
 * 客户端请去
 * @author RAY
 *
 */
@FeignClient(name="store",configuration=FooConfiguration .class)
public interface UserFeignClient {
    
    
    @RequestLine("GET /simple/{id}")
    public  User findById(@Param("id") Long id);
}

但是启动得时候报错: 
 

Method getLinksForTrack not annotated with HTTP method type (ex. GET, POST)

官方文档说明

@RequestLine is a core Feign annotation, but you are using the Spring Cloud @FeignClientwhich uses Spring MVC annotations. 

意思就是feign 默认使用的是spring mvc 注解(就是RequestMapping 之类的) ,所以需要通过新增一个配置类来修改其“契约”。

@Configuration
public class FooConfiguration {
    @Bean
    public Contract feignContract() {
        return new feign.Contract.Default();
//使用feign自带契约
    }

}

PS :  feignContract方法名不要跟一样。否则启动得时候会报错。 得改一个跟类名不一样得方法名!

完!!!!

猜你喜欢

转载自blog.csdn.net/liangweihua123/article/details/87881358