使用Feign Client时是否可以添加多个标题

时间:2019-12-11 20:24:29

标签: java spring-cloud-feign openfeign

我有一个Feign客户端,可访问需要两个标头的createUser端点:用户名和密码。我知道如何添加一个标头,但是如何向请求中添加两个标头?

@FeignClient(name = "client", url = "https://abc.abc.com/user/", configuration = FeignClientConfig.class)
public interface MyFeignClient {

@Headers("username_header: {username}")  // how do I add "password" here.
@PostMapping(value = "v1/users")
void createUser((@Param("username") String username, User userRequest);

}

更新:现在,根据下面的答案,我将界面主体更改为:

@Headers({"username_header: {username}", "password_header: {password}"}) 
@PostMapping(value = "v1/users")
void createUser(@Param("username") String username, 
                @Param("password") String password,
                User userRequest);

调用它的代码是:

feignClient.createUser("me", "123", userObj);

然后我得到一个错误:

 org.springframework.beans.factory.BeanCreationException: Error creating bean,
 nested exception is java.lang.IllegalStateException: Method has too many Body parameters: 
 feignClient.createUser(java.lang.String,java.lang.String, User)

2 个答案:

答案 0 :(得分:0)

Header接受String []作为值...所以

    @Headers({ "username: {username}", "password: {password}" })

应该做到这一点

答案 1 :(得分:0)

@RequestLine("POST /v1/users")
@Body("{userBody}")
Response createUser(@HeaderMap Map headerMap, @Param(value = "userBody") String userBody);

这有效。