Yes, you can do that. With Feign, we usually declare our interface with the method returning our class and Feign will automatically de-serialise the response from JSON into our POJO.

Here is the interface (operation):

@FeignClient(name = "library-book-service")
@RequestMapping("books")
public interface BookClient {

    @GetMapping
    public List<Book> getBooks(
        @RequestParam("page") Integer pageNum,
        @RequestParam("size") Integer pageSize,
        @RequestParam("reader") Long readerId);
}

And then you can use the feign client like this:

@Service
@RequiredArgsConstructor
public class BookService {
    private final @NonNull BookClient bookClient;

    public List<Book> retrieveBooks(
        Integer pageNum, 
        Integer pageSize, 
        Long readerId) {

            return bookClient.getBooks(pageNum, pageSize, readerId);
    }

However, in order to have access to the response headers, you need to declare your methods to return feign.Response.

import feign.Response;

@FeignClient(name = "library-book-service")
@RequestMapping("books")
public interface BookClient {

    @GetMapping
    public Response getBooks(
        @RequestParam("page") Integer pageNum,
        @RequestParam("size") Integer pageSize,
        @RequestParam("reader") Long readerId);
}

This way you can have access to the response body and headers:

@Service
@RequiredArgsConstructor
public class BookService {
    private final @NonNull BookClient bookClient;
    private final @NonNull ObjectMapper objectMapper;

    public List<Book> retrieveBooks(
        Integer pageNum, 
        Integer pageSize, 
        Long readerId) {

          var response = bookClient.getBooks(pageNum, pageSize, readerId);
          if (response == null) {
            return Collections.emptyList();
          }

          // retrieve body
          var books = objectMapper.readValue(
            new BufferedReader(new InputStreamReader(response.body().asInputStream(), StandardCharsets.UTF_8)),
            new TypeReference<List<Book>>(){});

          // retrieve headers
          Map<String, Collection<String>> headers = response.headers();
          // ... do whatever you need with the headers

          return books;
    }
Answer from dbaltor on Stack Overflow
🌐
Baeldung
baeldung.com › home › rest › setting request headers using feign
Setting Request Headers Using Feign | Baeldung
March 25, 2026 - OpenFeign is a library that helps us build clients to web services by expressing the contract in a Java interface. We look at how to include header values in our requests through the interface, with fixed and dynamic values, and how to handle cross-cutting concerns like authorization by using interceptors.
Top answer
1 of 3
7

Yes, you can do that. With Feign, we usually declare our interface with the method returning our class and Feign will automatically de-serialise the response from JSON into our POJO.

Here is the interface (operation):

@FeignClient(name = "library-book-service")
@RequestMapping("books")
public interface BookClient {

    @GetMapping
    public List<Book> getBooks(
        @RequestParam("page") Integer pageNum,
        @RequestParam("size") Integer pageSize,
        @RequestParam("reader") Long readerId);
}

And then you can use the feign client like this:

@Service
@RequiredArgsConstructor
public class BookService {
    private final @NonNull BookClient bookClient;

    public List<Book> retrieveBooks(
        Integer pageNum, 
        Integer pageSize, 
        Long readerId) {

            return bookClient.getBooks(pageNum, pageSize, readerId);
    }

However, in order to have access to the response headers, you need to declare your methods to return feign.Response.

import feign.Response;

@FeignClient(name = "library-book-service")
@RequestMapping("books")
public interface BookClient {

    @GetMapping
    public Response getBooks(
        @RequestParam("page") Integer pageNum,
        @RequestParam("size") Integer pageSize,
        @RequestParam("reader") Long readerId);
}

This way you can have access to the response body and headers:

@Service
@RequiredArgsConstructor
public class BookService {
    private final @NonNull BookClient bookClient;
    private final @NonNull ObjectMapper objectMapper;

    public List<Book> retrieveBooks(
        Integer pageNum, 
        Integer pageSize, 
        Long readerId) {

          var response = bookClient.getBooks(pageNum, pageSize, readerId);
          if (response == null) {
            return Collections.emptyList();
          }

          // retrieve body
          var books = objectMapper.readValue(
            new BufferedReader(new InputStreamReader(response.body().asInputStream(), StandardCharsets.UTF_8)),
            new TypeReference<List<Book>>(){});

          // retrieve headers
          Map<String, Collection<String>> headers = response.headers();
          // ... do whatever you need with the headers

          return books;
    }
2 of 3
2

You can use import feign.Response as a response like:

@PostMapping("/test")
Response test(@RequestBody TestRequest testRequest);

then you can reach http header

response.headers().get(HEADER_NAME).toString();

if you want get body in this case you have to some json-string manipulation by use response.body() this page may help you for this

🌐
GitHub
github.com › OpenFeign › feign › issues › 297
How do I get the response headers and status of a void method? · Issue #297 · OpenFeign/feign
November 26, 2015 - Changing the method signature form void to ResponseEntity<Entity> works, as log as the method returns a body. Default, the POST, PUT and DELETE methods on the RestRepository do not return a body, but only a status and a location-header.
Author: OpenFeign
🌐
GitHub
github.com › spring-cloud › spring-cloud-netflix › issues › 1764
Using Feign to get Response Headers · Issue #1764 · spring-cloud/spring-cloud-netflix
March 8, 2017 - Hi! I'm using Spring Cloud (v Brixton.SR7) with Hystrix + Feign for my requests. (v 1.2.3.RELEASE). According to this, I should be able to use ResponseEntity as the method return, to retrieve the headers in a response. However, if I use ...
Author: spring-cloud
🌐
Medium
medium.com › @mtl98 › sending-headers-in-feign-client-94096c9069e6
Sending Headers in Feign Client | by Mohammed Taoufik Lahmidi | Medium
December 13, 2024 - The @RequestHeader annotation is straightforward and convenient for adding headers to specific methods in your Feign client. Here's how you can use it: First, create an interface and annotate it with @FeignClient.
🌐
GitHub
github.com › PlaytikaOSS › feign-reactive › issues › 240
Using reactive feign client to read response headers · Issue #240 · PlaytikaOSS/feign-reactive
May 26, 2020 - @SpringBootApplication @EnableEurekaClient @RestController @EnableReactiveFeignClients @EnableFeignClients public class FeignApplication implements CommandLineRunner { @Autowired private GreetingReactive reactiveFeignClient; public static void main(String[] args) { SpringApplication.run(FeignApplication.class, args); } @Override public void run(String... args) throws Exception { Mono<Response> a = reactiveFeignClient.greetingWithParam("a"); a.subscribe(response -> System.out.println(response.headers().get("sessionId").iterator().next()), doe->e.printStackTrace()); while (true); } } @ReactiveFeignClient(name = "web-flux-app") public interface GreetingReactive { @GetMapping("/greeting") Mono<String> greeting(); @GetMapping(value = "/greetingWithParam") Mono<Response> greetingWithParam(@RequestParam(value = "id") String id); }
Author: PlaytikaOSS
🌐
GitHub
github.com › OpenFeign › feign › issues › 1268
Cannot get response headers from a FeignException · Issue #1268 · OpenFeign/feign
September 3, 2020 - @RestControllerAdvice class FeignExceptionHandler { @ExceptionHandler(FeignException.FeignClientException::class) fun handleFeignClientException(e: FeignException.FeignClientException): ResponseEntity<String> { val headers = HttpHeaders() headers.contentType = MediaType.APPLICATION_JSON return ResponseEntity(e.contentUTF8(), headers, HttpStatus.valueOf(e.status())) } }
Author: OpenFeign
🌐
Arnoldgalovics
arnoldgalovics.com › spring-cloud-feign-headers
Passing headers with Spring Cloud Feign – Arnold Galovics
On the app side, I have the Feign client in the following way: @FeignClient(name = "translation", url = "http://localhost:9001") public interface TranslationClient { @RequestMapping("/message") MessageResponse message(@RequestHeader("Accept-Language") String language); } As you can see, I used the very same Spring MVC annotation for passing the language header.
Find elsewhere
🌐
Tabnine
tabnine.com › home › code library
Code Library - Tabnine
July 25, 2024 - The Tabnine Code Library is no longer available — but you can still get the answers and suggestions you need from our AI code assistant.
🌐
Spring Cloud
cloud.spring.io › spring-cloud-openfeign › 2.0.x › single › spring-cloud-openfeign.html
Spring Cloud OpenFeign
The Logger.Level object that you may configure per client, tells Feign how much to log. Choices are: NONE, No logging (DEFAULT). BASIC, Log only the request method and URL and the response status code and execution time. HEADERS, Log the basic information along with request and response headers.
🌐
Codeboje
codeboje.de › getting-started-feignclient
Getting Started with Feign Client in Spring - codeboje
January 15, 2020 - In subsequent calls for the Kanban API, we will use the X-Auth-Token header with a token. Response headers can’t be returned directly as the method return value, but we can use Spring’s ResponseEntity, which is a response wrapper.
🌐
GitHub
github.com › OpenFeign › feign
GitHub - OpenFeign/feign: Feign makes writing java http clients easier · GitHub
As an example Feign includes a simple RedirectionInterceptor that can be used to extract the location header from redirection responses. public interface Api { // returns a 302 response @RequestLine("GET /location") String location(); } public ...
🌐
GitHub
github.com › OpenFeign › feign › issues › 396
Access to request, as well as response headers when response status != 2XX · Issue #396 · OpenFeign/feign
May 25, 2016 - I would like to be able to log the full request, including headers, in addition to response, when a response returns something other than 2XX, to be able to fully debug what is going on. I can turn on logging, but as far as I can tell in feign.SynchronousMethodHandler , it's not possible to get it ...
Top answer
1 of 2
11

You can create an interceptor that'd inject headers in all requests as below:

@Bean
public RequestInterceptor requestInterceptor() {
  return requestTemplate -> {
      requestTemplate.header("user", username);
      requestTemplate.header("password", password);
      requestTemplate.header("Accept", ContentType.APPLICATION_JSON.getMimeType());
  };
}

Also it provides a way to set interceptor using property files as below:

feign:
  client:
    config:
      default:
        requestInterceptors:
          com.baeldung.cloud.openfeign.JSONPlaceHolderInterceptor

we can create the configuration with default as the client name to configure all @FeignClient objects, or we can declare the feign client name for a configuration

Reference: https://www.baeldung.com/spring-cloud-openfeign

EDIT: Another way is to set headers in yml as below:

feign:
  client:
    config:
      default:
        defaultRequestHeaders:
          Authorization:
            - Basic dXNlcjpwYXNzd29yZA==
          SomeOtherHeader:
            - Value1
            - Value2
2 of 2
2

we have used below for kotlin to make sure we add proper headers to all the request sent :

Feign client with configuration setup

@FeignClient(name = "YourClient", url = "\${base-url}", configuration = [FeignHeaderConfiguration::class])
interface YourClient 

Header configuration

@Configuration
class FeignHeaderConfiguration {

    @Bean
    fun clientHeaderInterceptor(): ClientHeaderInterceptor {
        return ClientHeaderInterceptor()
    }
}

and interceptor where we added the header

class ClientHeaderInterceptor : RequestInterceptor {
    override fun apply(requestTemplate: RequestTemplate) {
       requestTemplate.header("Accept", MediaType.APPLICATION_JSON_VALUE)
    }
}
🌐
Theodo
blog.theodo.ma › consuming-rest-api-using-feign
Consuming REST API Using Feign
December 22, 2021 - Similar to Spring Data, OpenFeign uses the same abstraction to implement declared interfaces marked with the @FeignClient annotation, and we will use the same Spring MVC annotations, to set the headers, body requests, and the object type that we wish to return. So if we want to try our posts example with OpenFeign it will look like this: we don't need to learn any new syntax, we can use the same Spring MVC annotations like (@RequestMapping, @PathVariable, @RequestParam, @RequestBody, @ResponseBody, @RequestHeader, and @ResponseHeader ...).
🌐
Medium
medium.com › @piszu › feign-client-is-not-so-simple-as-we-want-case-study-920e4901e27b
Feign Client is not so simple as we want — case study | by Mariusz Pisz | Medium
October 27, 2019 - About log levels documentation ... per client, tells Feign how much to log. Choices are: NONE, No logging (DEFAULT). BASIC, Log only the request method and URL and the response status code and execution time. HEADERS, Log the basic information along with request and response headers. FULL, Log the headers, body, and metadata for both requests and responses." To parse responses we need to use objects mapper. As you can suppose there is no possibility to get two mappers ...