Spring Cloud OpenFeign实战之 FeignException 405

本文解析了Feign在发起service to service调用时,原本定义为GET请求被错误转换为POST请求的问题。深入探讨了OpenFeign构造请求的机制,并提供了正确的参数注解使用方法,以解决此异常。

异常描述

feign.FeignException: status 405 reading UserFeignClient#get0(User); content:

{"timestamp":1482676142940,"status":405,"error":"Method Not Allowed", "exception":"org.springframework.web.HttpRequestMethodNotSupportedException","message":"Request method 'POST' not supported","path":"/user"}

FeignClient定义

@FeignClient("microservice-provider-user")

public interface UserFeignClient {

  @RequestMapping(value = "/user", method = RequestMethod.GET)

  public User get0(Long userId);

}

解决方法

明明定义的是GET请求,结果在发起service to service call时,被转换成了POST请求。原因是OpenFeign在构造请求时需要足够多的@RequestMapping/@RequestParam/@PathVariable/@RequestHeader等来构造http请求。通过阅读自己定义的FeignClient可以发现,GET请求中的参数定义缺少注解@RequestParam,故而报错。 将参数userId明确定义为@RequestParam("userId") Long userId

关注公众号交流学习

关注Java分布式架构实战, 持续精进

关注Java分布式架构实战, 持续精进

联系我,学习交流

联系我,学习交流

### Spring Cloud 项目实战教程与示例代码 Spring Cloud 是一个基于 Spring Boot 的分布式系统开发框架,用于构建微服务架构。以下是关于 Spring Cloud 项目的实战教程和示例代码的详细说明。 #### 1. 搭建 Spring Boot 父子项目 在开始 Spring Cloud 项目之前,通常需要搭建一个父子项目结构。父项目主要用于管理依赖版本和插件配置,子项目则负责实现具体的功能模块。 ```xml <!-- pom.xml 父项目 --> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>spring-cloud-parent</artifactId> <version>1.0-SNAPSHOT</version> <packaging>pom</packaging> <properties> <java.version>1.8</java.version> <spring-cloud.version>Hoxton.SR12</spring-cloud.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> </project> ``` #### 2. Feign/Ribbon/OpenFeign 的基本使用方法 Feign 是一个声明式的 HTTP 客户端,用于简化微服务之间的调用。以下是一个简单的 OpenFeign 使用示例[^1]。 ```java // 定义 Feign 接口 @FeignClient(name = "service-provider") public interface HelloService { @GetMapping("/hello") String sayHello(); } // 在控制器中使用 Feign 客户端 @RestController public class HelloController { @Autowired private HelloService helloService; @GetMapping("/feign-call") public String callHello() { return helloService.sayHello(); } } ``` #### 3. Hystrix 的使用方法 Hystrix 是一个熔断器组件,用于防止服务雪崩。以下是一个简单的 Hystrix 配置示例[^1]。 ```java // 使用 @HystrixCommand 注解定义降级逻辑 @Service public class HelloService { @HystrixCommand(fallbackMethod = "fallbackMethod") public String callRemoteService() { // 模拟远程服务调用 return restTemplate.getForObject("http://service-provider/hello", String.class); } public String fallbackMethod() { return "Fallback response"; } } ``` #### 4. Eureka/Nacos 注册中心的使用 Eureka 和 Nacos 是常见的服务注册与发现工具。以下是 Nacos 的简单使用示例[^2]。 ```yaml # application.yml 配置文件 spring: cloud: nacos: discovery: server-addr: localhost:8848 ``` ```java // 启动类添加注解 @SpringBootApplication @EnableDiscoveryClient public class ServiceApplication { public static void main(String[] args) { SpringApplication.run(ServiceApplication.class, args); } } ``` #### 5. Zuul 网关的使用 Zuul 是 Netflix 提供的 API 网关服务,用于路由和过滤请求。以下是一个简单的 Zuul 配置示例[^1]。 ```yaml # application.yml 配置文件 zuul: routes: service-provider: path: /provider/** serviceId: service-provider ``` #### 6. Spring Security 的使用 Spring Security 是一个强大的安全框架,用于保护微服务应用。以下是一个简单的 Spring Security 配置示例[^3]。 ```java @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/public/**").permitAll() .anyRequest().authenticated() .and() .oauth2Login(); } } ``` --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值