spring-cloud / spring-cloud-openfeign

Support for using OpenFeign in Spring Cloud apps
Apache License 2.0
1.18k stars 758 forks source link

Headers annotation not sending headers #983

Closed toadornode closed 5 months ago

toadornode commented 5 months ago

I'm not sure what I'm doing wrong here.

I've included spring cloud open feign dependency

implementation 'org.springframework.cloud:spring-cloud-starter-openfeign:4.1.0'

Then in my feign client interface

@FeignClient(name = "WeirdClient", url="http://localhost:8080")
public interface WeirdClient {
    @RequestMapping(method = RequestMethod.GET, value = "/test", consumes="application/json")
    @Headers({"test: someVal"})
    String getStuff();
}

Yet, the header does not get sent.

If I make the implementation like the following then it gets sent

public interface WeirdClient {
    @RequestLine("GET /test")
    @Headers({"test: someVal"})
    String getStuff();
}
@Configuration
public class FeignClientConfig {

    @Bean
    public WeirdClient weirdClient() {
        return Feign.builder()
                .target(WeirdClient.class, "http://localhost:8080");
    }
}
toadornode commented 5 months ago

Okay, I figured out the issue... You have to use the built in headers as part of the RequestMapping.

    @RequestMapping(method = RequestMethod.GET, value = "/test", consumes="application/json",
            headers = "test=someVal")

I guess I was referencing a different Feign Client implementation... kind of confusing https://www.baeldung.com/intro-to-feign

It might be worth adding a header example in the documentation: https://cloud.spring.io/spring-cloud-openfeign/reference/html/

OlgaMaciaszek commented 5 months ago

Hello @toadornode, yes, we do not recommend mixing the Spring Cloud OpenFeign and native Feign annotations on one method.