How to Include Feign?

After adding the library annotate the main Application file with this @EnableFeignClients annotation like below

@SpringBootApplication
@EnableFeignClients
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

}

Create an Interface and annotate it with @FeignClient and declare your calling methods like below

@FeignClient(name = "giveYourServiceName", url = "provideYourUrlHere", path = "provideYourContextPathHere")
public interface AddressClient {

@GetMapping("/address/{id}")
public ResponseEntity<AddressResponse> getAddressByEmployeeId(@PathVariable("id") int id);

}

Now it’s ready for use in the service class file. You can refer to the below code

@Service
public class EmployeeService {


// More Code Here


// -------------

// Spring will create the implementation
// for this class
// and will inject the bean here (proxy)
@Autowired
private AddressClient addressClient;

public EmployeeResponse getEmployeeById(int id) {

// More Code Here

// Using FeignClient
ResponseEntity<AddressResponse> addressResponse = addressClient.getAddressByEmployeeId(id);
employeeResponse.setAddressResponse(addressResponse.getBody());

return employeeResponse;
}

}

Let’s understand the whole thing by developing two Spring Boot projects.

Spring Cloud – OpenFeign with Example Project

OpenFeign is an open-source project that was originally developed by Netflix and then moved to the open-source community. Feign is a declarative rest client that creates a dynamic implementation of the interface that’s declared as FeignClient. Writing web services with the help of FeignClient is very easier. FeignClient is mostly used to consume REST API endpoints which are exposed by third-party or microservice.

Similar Reads

Dependencies For OpenFeign

For Maven:...

How to Include Feign?

After adding the library annotate the main Application file with this @EnableFeignClients annotation like below...

Example Spring Boot Project

In this project, we are going to develop two Microservices...