Eclipse MicroProfile Rest-Client is a wonderful piece of software. A few annotations here and there and you have a typesafe REST-Client for your application. Forget all the low-level mangling of http connections etc. Let’s have a look at some code. First comes the definition of the interface that is to be called:

@Path("/api/verify")

@RegisterRestClient

public interface VerifyEngine {



@POST

@Consumes("application/json")

@Produces("application/json")

public Msg verify(Msg policy);

}

Using this in code is straightforward. Have CDI Inject the above client with @Inject @RestClient and then use it like with any other method call.

public class PolicyCrudService {



@Inject

@RestClient

VerifyEngine engine;



public Msg verifyTheMessage(Msg msg) {

return engine.verify(msg);

}

}

If you look at the above, you may wonder how that code should know where the service to be called actually is. You can set the base URL via MicroProfile config — e.g. in the file application.properties if you are on Quarkus:

org.acme.VerifyEngine/mp-rest/url=http://localhost:8083

The config key starts with the fully qualified class name of the interface that has the @RegisterRestClient annotation.

Now some services live behind authorisation checks. Depending on the kind you want to you can also have this set up declaratively:

@Path("/api/verify")

@RegisterRestClient

@ClientHeaderParam(name = "Authorization", value = "{lookupAuth}")

public interface VerifyEngine {



default String lookupAuth() {

return "Basic " +

Base64.getEncoder().encodeToString("user:pass".getBytes());

}

Of course, you are not forced to hard code username and password, but can obtain those again via MicroProfile Config, where one of the ConfigSources could obtain it from a Kubernetes secret.

More ?

If you want to learn more about Eclipse MicroProfile, have a look at the book “Hands-on Enterprise Java Microservices with Eclipse MicroProfile” where I am also a co-author.