A REST client lets one service invoke another service’s REST endpoint. In a car-rental application, the Reservation service calls the Rental service when a confirmed reservation starts today. The Rental service exposes a POST endpoint at /rental/start/{userId}/{reservationId} and returns a Rental object. section 4.3
To make the call in Quarkus:
- Define a Rental model on the calling side to represent the response. section 4.3
- Declare a Java interface with @RegisterRestClient, the endpoint path, a @POST method, and path parameters. Inject it into the calling resource with @RestClient. section 4.3
- Give services distinct local ports; the Rental service uses quarkus.http.port=8082 to avoid a conflict. section 4.3
Listing 4.16 The source code of the RentalClient interface
package org.acme.reservation.rental;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;
import org.jboss.resteasy.reactive.RestPath;
@RegisterRestClient(baseUri = "http://localhost:8082")
@Path("/rental")
public interface RentalClient {
@POST
@Path("/start/{userId}/{reservationId}")
Rental start(@RestPath String userId,
@RestPath Long reservationId);
}
The RentalClient interface maps a Java method to the Rental service’s POST endpoint. section 4.3
A baseUri such as http://localhost:8082 is convenient locally but hardcodes the service location. For independent deployment, use @RegisterRestClient(configKey = "rental") and set quarkus.rest-client.rental.url in application.properties. Quarkus can also use the interface’s fully qualified name as the default configuration key, though renaming that interface would then require a property change. section 11.1.1
