OpenFeign-Client 헤더 추가하기
1. 헤더 1개 추가
@FeignClient(name = "testClient", url = "http://localhost:8080")
public interface TestClient() {
@GetMapping(value = "/test", headers = "key1=value1")
Response getTest();
}
2. 헤더 2개 추가
@FeignClient(name = "testClient", url = "http://localhost:8080")
public interface TestClient() {
@GetMapping(value = "/test", headers = {"key1=value1", "key2=value2"})
Response getTest();
}
3. 모든 Feign 요청에 헤더 추가
모든 요청에서 헤더를 추가하고 싶으면 RequestInterceptor
인터페이스 사용.
// RequestInterceptor 구현
public class CustomRequestInterceptor implements RequestInterceptor {
@Value("${token}")
private final token;
@Override
public void apply(RequestTemplate template) {
template.header("Authorization", token);
}
}
// FeignConfig 설정 (빈 등록)
@Configuration
public class FeignConfig {
@Bean
public CustomRequestInterceptor feignInterceptor() {
return new CustomRequestInterceptor();
}
}
// Feign Client 설정
@FeignClient(name = "test", url = "http://localhost:8080", configuration = FeignConfig.class)
public interface Test() {
@GetMapping(value = "/test")
Response getTest();
}
참고: https://rudaks.tistory.com/entry/openfeign에서-header에-값-추가하는-방법