4

我有一个 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)
4

3 回答 3

0

我知道可能会迟到,但我希望能帮助别人。

什么对我有用:

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestParam;

@PostMapping(path = "${service-uri}", consumes = MediaType.APPLICATION_JSON_VALUE)
    String callService(@RequestBody MyBodyClass body, @RequestParam Integer param01, @RequestParam String param02, @RequestHeader Map<String, String> headerMap);

@HeaderMap 提出的解决方案不起作用,显然是因为这个类来自feign包(import feign.HeaderMap)。

于 2022-01-07T16:32:38.300 回答
0

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

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

应该做的伎俩

于 2019-12-11T20:29:21.077 回答
0
@RequestLine("POST /v1/users")
@Body("{userBody}")
Response createUser(@HeaderMap Map headerMap, @Param(value = "userBody") String userBody);

更新

@RequestLine("POST /v1/users") 
Response createUser(String userBody, @HeaderMap Map headerMap);
于 2020-07-27T06:34:57.213 回答