SpringCloud 使用 Feign各 种报错

发布时间:2022-06-20 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了SpringCloud 使用 Feign各 种报错脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

由于只想在SpringBoot中使用一下Feign客户端,来访问第三方请求,但因为各种版本问题,一直报各种乱七八糟的错

pom文件

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.12.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
 
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.16.20</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
        <version>2.0.2.RELEASE</version>
    </dependency>
</dependencies>

注意这里的springboot的版本号和openfeign的版本号非常重要,不要盲目使用最新版本

然后是常规操作

在项目启动类上添加@EnableFeignClients注解

//from fhadmin.cn
@EnableFeignClients
@SpringBootApplication
public class ClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(ClientApplication.class,args);
    }
}

如果忘记添加该注解,会报自己定义的FeignClient注入不进去的错

编写接口Interface写FeignClient

格式大致像如下方式

@FeignClient(
        name = "userService",
        url = "http://localhost:8081/api/server"
)
//from fhadmin.cn
public interface UserFeignClient {
    @RequestMapping(value = "/getUserInfo",method = RequestMethod.GET)
    @ResponseBody
    String getUserInfo();
}

 

如果未为FeignClient设置name,也会报错,会报一个没有name的错,一看就能明白

搜索

复制

<iframe src="/admin/blogs/"></iframe>

脚本宝典总结

以上是脚本宝典为你收集整理的SpringCloud 使用 Feign各 种报错全部内容,希望文章能够帮你解决SpringCloud 使用 Feign各 种报错所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: