Reputation: 327
I tried to build a Spring Feign Client
to encapsulate the call to an internal API. In this request, I need to make a login in order to get a token and then use it as a Header parameter for my new request. To set the token dynamically, I used the code below.
@FeignClient(value = "APIBuscarCep", url = "${url}")
public interface BuscarCepFeignClient {
@RequestMapping(method = RequestMethod.GET, value = "url-complement", produces = "application/json")
@Headers({"Authorization: {token}"})
CepResponseDTO getAddressByCep(@Param("token") String token, @NotNull @PathVariable("cep") String cep);
}
Unfortunately, besides the login goes right and get token, my request always returns the 403 code
. (I used postman to check if the request should return successfully).
The only solution that works was using restTemplate
like the code below.
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.add("Authorization", token);
ResponseEntity<CepResponseDTO> exchange;
exchange = restTemplate.exchange(String.format("%s%s", endpoint, cep),
HttpMethod.GET, header, CepResponseDTO.class);
Using Spring boot 2.
Upvotes: 4
Views: 14375
Reputation: 712
You can't use dynamic value in @Header
annotation. However, you can use @RequestHeader
annotation in the input param to do this.
@FeignClient(value = "APIBuscarCep", url = "${url}")
public interface BuscarCepFeignClient {
@RequestMapping(method = RequestMethod.GET, value = "url-complement", produces = "application/json")
CepResponseDTO getAddressByCep(@RequestHeader("Authorization") String token, @NotNull @PathVariable("cep") String cep);
}
Upvotes: 5