As per the announcement, from Spring 6.1 and Spring Boot 3.2 we have a brand new option called RestClient
:
Spring Framework 6.1 M2 introduces the RestClient, a new synchronous
HTTP client. As the name suggests, RestClient offers the fluent API of
WebClient with the infrastructure of RestTemplate.
RestClient restClient = RestClient.create();
String result = restClient.get()
.uri("https://example.com")
.retrieve()
.body(String.class);
Based on the JavaDoc of RestTemplate
, this is now the recommended replacement:
NOTE: As of 6.1, RestClient offers a more modern API for synchronous HTTP access.
The new RestClient can also be used with the recently introduced declarative HTTP interface without including Webflux on the classpath.
RestClient restClient = RestClient.builder().baseUrl("https://api.github.com/").build();
RestClientAdapter adapter = RestClientAdapter.create(restClient);
HttpServiceProxyFactory factory = HttpServiceProxyFactory.builderFor(adapter).build();
RepositoryService service = factory.createClient(RepositoryService.class);