Spring RestTemplate

If you need to call a service from another service, you will need a HTTP client. Spring provides the very useful RestTemplate class. It gives you a synchronous client-side HTTP access, simplifies communication with HTTP servers, and enforces RESTful principles. It handles HTTP connections, leaving application code to provide URLs (with possible template variables) and extracts results. By default, RestTemplate relies on standard JDK facilities to establish HTTP connections. You can switch to a different HTTP library of your choice, such as Apache HttpComponents, Netty, and OkHttp through its setRequestFactory() method. Calling the REST resource to get a book with ID = 1 can be as simple as follows:

package pl.finsys.example.client; ...

Get Docker and Kubernetes for Java Developers now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.