liveBook · Manning

REST client

As explained in Quarkus in Action

A REST client is a software component that invokes REST endpoints over HTTP.

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

How Quarkus in Action covers it

How deep it goes
Works through interface declaration, injection, and endpoint URL configuration, but does not explain client internals.
Examples
Uses a RentalClient POST interface and a ReservationsClient configuration change to illustrate calls and deployment settings.
What it assumes
Assumes Java interfaces and REST endpoints while showing how to register, inject, and configure a Quarkus REST client.
What you can do after
Apply it via Quarkus REST Client to call another service and configure its URL outside the client interface.
How it connects
Links REST endpoints, dependency injection, path parameters, and application configuration.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Quarkus in Action. The section links point to the parts of the book it draws on.