从 OpenFeign 客户端获取有效 GET url 时收到 400 错误请求

问题描述 投票:0回答:1

我的

@EnableFeignClients
Spring(3.1.5)应用程序让我感到困惑。

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>

我有以下配置(

CustomInterceptor
本身就是一个
@Component
),不使用
@Configuration
,因为它会干扰其他客户端。

@Import(FeignClientsConfiguration.class)
public class SoConfig {

    @Bean
    public RequestInterceptor intercept(CustomInterceptor interceptor) {
        return interceptor;
    }
}

引自以下客户:

@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) {
            // handling
        }
    }
}

运行时执行时出现以下错误:

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>
]
  1. 当我复制 url
    https://resolved-config-url?%24filter=Identifier%20eq%20'testParam'
    并通过带有不记名令牌的 Postman 获取它时,我得到了所需的响应(URL 编码或解码,两者都适用于此 SAP 系统)。
  2. 当我在没有承载令牌的情况下通过邮递员执行相同操作时(认为拦截器可能没有连接),我收到一条消息,我需要提供 JWT(如预期)。

我已经为这个问题苦苦挣扎了几个小时,请派一位成人来。

我尝试过的(以及我现在忘记的其他事情)-->

  • 使用 okhttp 而不是 hc5(有或没有 spring.cloud.openfeign.httpclient.(hc5.)enabled=true
  • 在配置类中使用显式默认值(例如编码器/解码器)
  • 设置 spring.cloud.openfeign.lazy-attributes-resolution=true
  • 在 SoClient 中传递完整的
    url
    ,而不是在
    value
     上使用 
    @GetMapping
java spring-boot get bad-request openfeign
1个回答
0
投票

TLDR:我们触发了组件扫描,在同一请求上设置多个标头。


如果有人为此苦苦挣扎,我怀疑,这是更长的答案,这是困扰上述问题的高度具体的答案。

我们在主应用程序上添加了

@EnableFeignClients
注释(配置处理所需),这触发了对某些组件的组件扫描(以及后续的自动装配) - 阅读:请求拦截器 - 以前在手动假客户端上使用。

这导致多个

Authorization
标头被添加到
RestTemplate
请求中,最终在触发时抛出
400 Bad Request

我真的希望看到比上面更清晰的错误消息响应(我们尝试使用跟踪/完整日志记录,但从未看到更多信息),但是你已经看到了。

© www.soinside.com 2019 - 2024. All rights reserved.