gpt4 book ai didi

spring-boot - 如何在 Spring Boot Feign Client 上定义全局静态 header

转载 作者:行者123 更新时间:2023-12-02 21:22:17 26 4
gpt4 key购买 nike

我有一个 Spring Boot 应用程序,想要创建一个 Feign 客户端,它具有静态定义的 header 值(用于身份验证,但不是基本身份验证)。我找到了 @Headers 注释,但它似乎在 Spring Boot 领域不起作用。我怀疑这与使用 SpringMvcContract 有关。

这是我想要工作的代码:

@FeignClient(name = "foo", url = "http://localhost:4444/feign")
@Headers({"myHeader:value"})
public interface LocalhostClient {

但它没有添加标题。

我通过尝试制作了一个干净的 Spring Boot 应用程序,并发布到 github:github example

我能够使其工作的唯一方法是将 RequestInterceptor 定义为全局 bean,但我不想这样做,因为它会影响其他客户端。

最佳答案

您还可以通过向各个方法添加 header 来实现此目的,如下所示:

@RequestMapping(method = RequestMethod.GET, path = "/resource", headers = {"myHeader=value"})

Using @Headers with dynamic values in Feign client + Spring Cloud (Brixton RC2)讨论使用 @RequestHeader 的动态值解决方案。

关于spring-boot - 如何在 Spring Boot Feign Client 上定义全局静态 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50457362/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com