使用OpenFeign Client调用合法GET URL时出现400 Bad Request错误
Feign调用SAP系统返回400 Bad Request问题排查
环境与依赖
基于Spring 3.1.5搭建的应用,启用@EnableFeignClients,核心POM依赖如下:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <dependency> <!-- Required to use PATCH over default HttpURLConnection --> <groupId>io.github.openfeign</groupId> <artifactId>feign-hc5</artifactId> </dependency>
配置与代码
Feign客户端配置类
自定义配置类SoConfig,未添加@Configuration注解避免影响其他Feign客户端:
@Import(FeignClientsConfiguration.class) public class SoConfig { @Bean public RequestInterceptor intercept(CustomInterceptor interceptor) { return interceptor; } }
注:CustomInterceptor本身标记为@Component,负责为请求添加Bearer Token。
Feign客户端接口
@FeignClient(name = "so-client", url = "${config.url}", configuration = SoConfig.class) public interface SoClient { @GetMapping(value = "?$filter=Identifier eq '{param}'") SoData getSoData(@PathVariable("param") String param); }
调用网关类
@Component @RequiredArgsConstructor public class SoGateway { private final SoClient client; public SoData getSoData(@NonNull String param) { try { return client.getSoData(param); } catch (FeignException e) { // 自定义异常处理逻辑 } } }
运行时错误
调用过程中抛出400 Bad Request异常:
Caused by: feign.FeignException$BadRequest: [400 Bad Request] during [GET] to [https://resolved-config-url?%24filter=Identifier%20eq%20'testParam'] [SoClient#getSoData(String)]: [<!DOCTYPE html> <html> <body> <p>Sorry, the page you are looking for is currently unavailable.<br/> Please try again later.</p> </body> </html> ]
测试验证结果
- 将日志中的请求URL复制到Postman,携带Bearer Token发起GET请求,可正常获取预期响应(编码/解码后的URL均能访问SAP系统)
- 不带Bearer Token在Postman请求时,会收到需要提供JWT的提示,符合预期逻辑
已尝试的无效方案
- 替换
feign-hc5为okhttp,无论是否配置spring.cloud.openfeign.httpclient.(hc5.)enabled=true - 在配置类中显式指定默认编码器/解码器
- 设置
spring.cloud.openfeign.lazy-attributes-resolution=true - 修改
SoClient,传入完整URL而非在@GetMapping中设置value
求助需求
恳请帮忙排查Feign调用返回400 Bad Request的原因,并提供可行的解决方案。
内容的提问来源于stack exchange,提问作者MarcelTon
相关产品推荐
相关产品推荐

