软件编程
位置:首页>> 软件编程>> java编程>> spring cloud Feign使用@RequestLine遇到的坑

spring cloud Feign使用@RequestLine遇到的坑

作者:RayLiang微博  发布时间:2023-12-15 09:18:16 

标签:spring,cloud,Feign,@RequestLine

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.

spring cloud Feign使用@RequestLine遇到的坑

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


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

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

@RequestLine的使用及配置

@RequestLine与其它请求不同,只需要简单写请求方式和路径就能达到请求其它服务的目的。


@FeignClient(value = "feign-server",configuration = FeignConfig.class)  //需要一个配置文件
public interface TestService {
   @RequestLine("POST /feign/test")    //对应请求方式和路径
   String feign(@RequestBody UserDO userDO);
}

@EnableFeignClients
@SpringBootConfiguration
public class FeignConfig {
   @Bean
   public Contract contract(){
       return new feign.Contract.Default();
   }
}

来源:https://blog.csdn.net/liangweihua123/article/details/87881358

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com