6

I have a Feign client to access to an createUser endpoint that requires two headers: username and password. I know how to add one header, but how do I add two headers to the request?

@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);

}

Update: Now based on the answer below, I changed the interface body to:

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

And the code that calls it is:

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

Then I am getting an error:

 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)
jlp
  • 1,656
  • 6
  • 33
  • 48

3 Answers3

2

I know it may be late but I am hopping to help others.

What has worked for me:

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);

The solution proposed with @HeaderMap doesn't work, apparently because this class is from feign package (import feign.HeaderMap).

Paulo Pedroso
  • 3,555
  • 2
  • 29
  • 34
0

Headers accepts String[] for value ... So

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

Should do the trick

James Gawron
  • 871
  • 10
  • 27
  • Thanks, I tried that and I added `password` to the createUser method signature, but I got an error saying "Method has too many Body parameters:" – jlp Dec 11 '19 at 20:54
  • can you post that code @jlp? As the headers and body parameters are different ... – James Gawron Dec 11 '19 at 20:56
0
@RequestLine("POST /v1/users")
@Body("{userBody}")
Response createUser(@HeaderMap Map headerMap, @Param(value = "userBody") String userBody);

Updated

@RequestLine("POST /v1/users") 
Response createUser(String userBody, @HeaderMap Map headerMap);
Mohit Hurkat
  • 406
  • 2
  • 8
  • 1
    Not for me: IllegalStateException: Method has too many Body parameters :-( – Paulo Pedroso Jan 07 '22 at 15:25
  • As per the Documentation ... @RequestLine("GET /servers/{serverId}") void get(@Param("serverId") String serverId, @HeaderMap Map ); ... Could you try as mentioned below?? `@RequestLine("POST /v1/users") Response createUser(String userBody, @HeaderMap Map headerMap);` – Mohit Hurkat Jan 08 '22 at 21:35