feign client - Neethahiremath/Wiki GitHub Wiki

consider you want to call this URL:

http://localhost:8080/test

in yml u have

config:
    url: http://localhost:8080/test

add below dependency in pom

 <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
            <version>2.1.1.RELEASE</version>
 </dependency>

add @EnableFeignClients to main class

create interface client with @FeignClient annotation


@FeignClient(name = "Client", url = "${config.url}")
public interface Client {

    @PostMapping(value = "/postTest")
    ResponseEntity<Object> testPost(@RequestBody Request request);

    @GetMapping(value = "/testGet")
    String get();

}

use this client in service class or anywhere by @autowiring the Client class


Client.testPost(Request.builder().build());

if you want to pass the header to req(some header string ur looking at) fetch that string value from header and pass to client

 private String getHttpHeaderValue(HttpHeaders headers) {
        return headers.toSingleValueMap().get(X);
    }
  
 @PostMapping(value = "/test")
    Response test(@RequestBody Request request, @RequestHeader(name = "X") String headerValue);

⚠️ **GitHub.com Fallback** ⚠️