liveBook · Manning

@FeignClient annotation

As explained in Spring Start Here, Second Edition

The @FeignClient annotation declares an interface as a client for a remote service and provides configuration for generating its implementation.

A remote-service call can require handwritten request code. With OpenFeign, @FeignClient marks an interface as a client contract and tells OpenFeign to provide its implementation. Its name identifies the client, and its url sets the base URI; keeping that URI in an application property lets it vary by environment. section 11.3

A payments client

@FeignClient(name = "payments", url = "${name.service.url}") public interface PaymentsProxy { @PostMapping("/payment") Payment createPayment(@RequestHeader String requestId, @RequestBody Payment payment); } The interface method describes the REST call; OpenFeign supplies the implementation. section 11.3

Figure 11.5 With OpenFeign, you only need to define an interface (a contract) and tell OpenFeign where to find this contract to implement it. OpenFeign implements the interface and provides the implementation as a bean in the Spring context based on configurations you define with annotations. You can inject the bean from the Spring context anywhere you need it in your app.

See how OpenFeign turns the annotated interface into a Spring bean that can be injected where needed. section 11.3

How Spring Start Here, Second Edition covers it

How deep it goes
Defines the client name and base URL, then illustrates endpoint mappings; it does not detail annotation internals.
Examples
Uses a PaymentsProxy for POST /payment, with a request header, request body, and property-based service URL.
What you can do after
Apply it by declaring an interface for OpenFeign to implement, setting its name and URL, and mapping methods to REST calls.
How it connects
Connects OpenFeign clients with REST endpoint mappings, Spring beans, and application properties.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring Start Here, Second Edition. The section links point to the parts of the book it draws on.