DEV Community

eidher
eidher

Posted on • Updated on

Spring Cloud OpenFeign

Feign is a declarative web service client. It makes writing web service clients easier. To use Feign create an interface and annotate it.

Source: https://cloud.spring.io/spring-cloud-netflix/multi/multi_spring-cloud-feign.html

With Feign, URLs are not hardcoded and Feign integrates with Ribbon and Eureka automatically. To use it you must add this dependency:

<dependency>
  <artifactId>spring-cloud-starter-openfeign</artifactId>
  <groupId>org.springframework.cloud</groupId>
</dependency>
Enter fullscreen mode Exit fullscreen mode

Then, you must add the @EnableFeignClients annotation to the main class and declare a Feign client using the @FeignClient annotation in the interface:

@FeignClient(name = "other-service-name")
public interface Proxy {

  @GetMapping("/other-service-uri/{variable}")
  public Response getResponse(
      @PathVariable String variable);

}
Enter fullscreen mode Exit fullscreen mode

Now, you can use it:

Response response = proxy.getResponse(variable);
Enter fullscreen mode Exit fullscreen mode

For more information see: https://www.baeldung.com/spring-cloud-openfeign

Top comments (0)