Consuming REST Services
When your Spring Boot backend needs to fetch external data from a third-party API (like triggering a Stripe payment), it must act as a client.
OpenFeign
OpenFeign is a declarative HTTP client developed by Netflix. Instead of manually writing HTTP connection strings, you declare an interface, and Spring generates the actual network logic.
@FeignClient(name = "userClient", url = "https://jsonplaceholder.typicode.com")
public interface UserClient {
@GetMapping("/users/{id}")
User fetchRemoteUser(@PathVariable("id") Long id);
}
RestTemplate
RestTemplate is the legacy synchronous HTTP client found in standard Spring Web. It executes blocking requests.
@Service
public class MetricService {
public void getMetrics() {
RestTemplate restTemplate = new RestTemplate();
String result = restTemplate.getForObject("http://api.example.com/data", String.class);
System.out.println(result);
}
}