liveBook · Manning

OpenFeign client

As explained in Spring Start Here, Second Edition

An OpenFeign client is an annotated Java interface whose implementation is generated to call remote HTTP endpoints.

Calling a remote REST endpoint does not require writing the HTTP-calling code yourself. An OpenFeign client is an annotated Java interface: its methods describe endpoint calls, and OpenFeign supplies their implementation. section 11.3

To define a client:

  • Annotate the interface with @FeignClient, giving it a unique name and a base URI. Keep environment-specific URIs in properties, rather than hardcoding them. section 11.3
  • Declare a method for each endpoint call and use annotations to specify its path, HTTP method, and any request parameters, headers, or body. section 11.3
  • Enable client discovery with @EnableFeignClients. Spring provides the generated client as a bean that application components can inject. section 11.3

A payments client

A PaymentsProxy interface can use @FeignClient(name = "payments", url = "${name.service.url}") and declare createPayment with @PostMapping("/payment"), a request header, and a request body. OpenFeign implements the call; a controller can inject and use the proxy. 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.

The diagram traces the contract-to-bean flow: define the interface, let OpenFeign implement it, then inject the bean where needed. section 11.3

How Spring Start Here, Second Edition covers it

How deep it goes
Defines endpoint mappings, request details, client identity, and base URI while leaving method implementation to OpenFeign.
Examples
Uses a payments proxy to POST a payment with a request ID header and body.
What you can do after
Apply it via Spring Cloud OpenFeign by declaring and configuring an interface for remote endpoint calls.
How it connects
Connects Java interfaces with Spring, REST endpoints, HTTP methods, and dependency injection.

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.