Micronaut HTTP Client

Learn how to use Micronaut low-level HTTP Client. Simplify your code with the declarative HTTP client.

Authors: Sergio del Amo, Iván López

Micronaut Version: 2.5.0

1. Getting Started

In this guide we are going to create a Micronaut app written in Java to consume the GitHub API with the Micronaut HTTP Client.

2. What you will need

To complete this guide, you will need the following:

  • Some time on your hands

  • A decent text editor or IDE

  • JDK 1.8 or greater installed with JAVA_HOME configured appropriately

3. Solution

We recommend that you follow the instructions in the next sections and create the app step by step. However, you can go right to the completed example.

4. Writing the App

Create an app using the Micronaut Command Line Interface or with Micronaut Launch.

mn create-app example.micronaut.micronautguide --build=maven --lang=java
If you don’t specify the --build argument, Gradle is used as a build tool.
If you don’t specify the --lang argument, Java is used as a language.

The previous command creates a Micronaut app with the default package example.micronaut in a folder named micronautguide.

If you are using Java or Kotlin and IntelliJ IDEA, make sure you have enabled annotation processing.

annotationprocessorsintellij

4.1. GitHub API

In this guide, you are going to consume the GitHub API from a Micronaut application.

In particular, we consume the List releases endpoint.

This returns a list of releases, which does not include regular Git tags that have not been associated with a release.

This API resource can be consumed by both authenticated and anonymous clients.

Initially, you will consume it anonymously, later we will discuss authentication.

Modify src/main/resources/application.yml to create some configuration parameters.

src/main/resources/application.yml
github:
  organization: micronaut-projects
  repo: micronaut-core

To encapsulate type-safe configuration retrieval, we use a @ConfigurationProperties object:

src/main/java/example/micronaut/GithubConfiguration.java
package example.micronaut;

import io.micronaut.context.annotation.ConfigurationProperties;
import io.micronaut.context.annotation.Requires;

@ConfigurationProperties(GithubConfiguration.PREFIX)
@Requires(property = GithubConfiguration.PREFIX)
public class GithubConfiguration {

    public static final String PREFIX = "github";
    public static final String GITHUB_API_URL = "https://api.github.com";

    private String organization;
    private String repo;
    private String username;
    private String token;

    public String getOrganization() {
        return organization;
    }

    public void setOrganization(String organization) {
        this.organization = organization;
    }

    public String getRepo() {
        return repo;
    }

    public void setRepo(String repo) {
        this.repo = repo;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }
}

In this guide, you are going to fetch Micronaut Core releases.

To consume the GitHub API, you will use Micronaut HTTP Client.

4.2. Low Level Client

Initially, you will create a Bean which uses the low-level Client API.

Create a POJO to parse the JSON response into an object:

src/main/java/example/micronaut/GithubRelease.java
package example.micronaut;

import io.micronaut.core.annotation.Introspected;

@Introspected
public class GithubRelease {

    private String name;
    private String url;

    public GithubRelease() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }
}

Create GithubLowLevelClient:

src/main/java/example/micronaut/GithubLowLevelClient.java
package example.micronaut;

import io.micronaut.core.type.Argument;
import io.micronaut.http.HttpRequest;
import io.micronaut.http.client.RxHttpClient;
import io.micronaut.http.client.annotation.Client;
import io.micronaut.http.uri.UriBuilder;
import io.reactivex.Flowable;
import io.reactivex.Maybe;

import javax.inject.Singleton;
import java.net.URI;
import java.util.List;

import static io.micronaut.http.HttpHeaders.ACCEPT;
import static io.micronaut.http.HttpHeaders.USER_AGENT;

@Singleton (1)
public class GithubLowLevelClient {

    private final RxHttpClient httpClient;
    private final URI uri;

    public GithubLowLevelClient(@Client(GithubConfiguration.GITHUB_API_URL) RxHttpClient httpClient,  (2)
                                GithubConfiguration configuration) {  (3)
        this.httpClient = httpClient;
        this.uri = UriBuilder.of("/repos")
                .path(configuration.getOrganization())
                .path(configuration.getRepo())
                .path("releases")
                .build();
    }

    Maybe<List<GithubRelease>> fetchReleases() {
        HttpRequest<?> req = HttpRequest.GET(uri) (4)
                .header(USER_AGENT, "Micronaut HTTP Client") (5)
                .header(ACCEPT, "application/vnd.github.v3+json, application/json"); (6)
        Flowable<List<GithubRelease>> flowable = httpClient.retrieve(req, Argument.listOf(GithubRelease.class)); (7)
        return flowable.firstElement(); (8)
    }
}
1 Use javax.inject.Singleton to designate a class as a singleton.
2 Inject RxClient via constructor injection.
3 Inject the previously defined configuration parameters.
4 Creating HTTP Requests is easy thanks to Micronaut’s fluid API.
5 GitHub API requires to set the User-Agent header.
6 GitHub encourages to explicitly request the version 3 via the Accept header. With @Header, you add the Accept: application/vnd.github.v3+json HTTP header to every request.
7 Use retrieve to perform an HTTP request for the given request object and convert the full HTTP response’s body into the specified type. e.g. List<GithubRelease>.
8 The retrieve method returns a Flowable which has a firstElement method that returns the first emitted item or nothing
Instead of retrieve we could have used jsonStream. You can use jsonStream() to stream arrays of type application/json or JSON streams of type application/x-json-stream. If we use retrieve, such as in the previous code listing, the operation will not block. However, it will not return until all the data has been received from the server. In the case of a JSON array that would be the whole array. However, if you are interested in just the first element of the array, jsonStream provides a better alternative since it starts streaming data from the server without needing the whole response. For example, jsonStream().firstElement() will only parse the first item in a JSON array. Hence it is more efficient.

4.3. Declarative Client

It is time to take a look at Micronaut’s support for declarative clients via the Client annotation.

Create GithubApiClient which clearly illustrates how a declarative Micronaut HTTP Client, which is generated at compile-time, simplifies our code.

src/main/java/example/micronaut/GithubApiClient.java
package example.micronaut;

import io.micronaut.http.annotation.Get;
import io.micronaut.http.annotation.Header;
import io.micronaut.http.client.annotation.Client;
import io.reactivex.Flowable;

import static io.micronaut.http.HttpHeaders.ACCEPT;
import static io.micronaut.http.HttpHeaders.USER_AGENT;

@Client(GithubConfiguration.GITHUB_API_URL) (1)
@Header(name = USER_AGENT, value = "Micronaut HTTP Client") (2)
@Header(name = ACCEPT, value = "application/vnd.github.v3+json, application/json") (3)
public interface GithubApiClient {

    @Get("/repos/${github.organization}/${github.repo}/releases") (4)
    Flowable<GithubRelease> fetchReleases(); (5)
}
1 URL of the remote service
2 GitHub API requires to set the User-Agent header.
3 GitHub encourages to explicitly request the version 3 via the Accept header. With @Header, you add the Accept: application/vnd.github.v3+json HTTP header to every request.
4 You can use configuration parameter interpolation when you define the path of the GET endpoint.
5 You can return reactive types, such as an RxJava Flowable.

4.4. Controller

Create a Controller. It uses both (low-level and declarative clients). It showcases several Micronaut’s capabilities.

  • Micronaut supports any framework that implements Reactive Streams, including RxJava, and Reactor. Thus, you can easily and efficiently compose multiple HTTP client calls without blocking (which will limit the throughput and scalability of your application).

  • Micronaut enables you to consume/produce JSON Streams.

src/main/java/example/micronaut/GithubController.java
package example.micronaut;

import io.micronaut.http.MediaType;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import io.reactivex.Flowable;
import io.reactivex.Maybe;

import java.util.List;

@Controller("/github") (1)
public class GithubController {

    private final GithubLowLevelClient githubLowLevelClient;
    private final GithubApiClient githubApiClient;

    public GithubController(GithubLowLevelClient githubLowLevelClient,
                            GithubApiClient githubApiClient) { (2)
        this.githubLowLevelClient = githubLowLevelClient;
        this.githubApiClient = githubApiClient;
    }

    @Get("/releases-lowlevel") (3)
    Maybe<List<GithubRelease>> releasesWithLowLevelClient() { (4)
        return githubLowLevelClient.fetchReleases();
    }

    @Get(uri = "/releases", produces = MediaType.APPLICATION_JSON_STREAM) (5)
    Flowable<GithubRelease> fetchReleases() { (6)
        return githubApiClient.fetchReleases();
    }
}
1 The class is defined as a controller with the @Controller annotation mapped to the path /github.
2 Inject beans via constructor injection.
3 The @Get annotation is used to map the index method to all requests that use an HTTP GET
4 The releasesWithLowLevelClient returns a Maybe which may or may not emit an item. If an item is not emitted a 404 is returned.
5 In order to do JSON streaming you can declare a controller method that returns a application/x-json-stream of JSON objects.
6 You can return reactive types, such as an RxJava Flowable.

4.5. Tests

Create a test which verifies both clients work as expected and the controller echoes the output of the GitHub API in a Reactive way.

src/test/java/example/micronaut/GithubControllerTest.java
package example.micronaut;

import io.micronaut.core.type.Argument;
import io.micronaut.http.HttpRequest;
import io.micronaut.http.HttpResponse;
import io.micronaut.http.HttpStatus;
import io.micronaut.http.client.RxStreamingHttpClient;
import io.micronaut.http.client.annotation.Client;
import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import io.reactivex.Flowable;
import org.junit.jupiter.api.Test;

import javax.inject.Inject;
import java.util.Arrays;
import java.util.List;
import java.util.stream.StreamSupport;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

@MicronautTest (1)
class GithubControllerTest {

    @Inject
    @Client("/")
    RxStreamingHttpClient client; (2)

    private static final List<String> expectedReleases = Arrays.asList("Micronaut 2.5.0", "Micronaut 2.4.4", "Micronaut 2.4.3");

    @Test
    public void verifyGithubReleasesCanBeFetchedWithLowLevelHttpClient() {
        //when:
        HttpRequest<Object> request = HttpRequest.GET("/github/releases-lowlevel");

        HttpResponse<List<GithubRelease>> rsp = client.toBlocking().exchange(request, (3)
                Argument.listOf(GithubRelease.class)); (4)

        //then: 'the endpoint can be accessed'
        assertEquals(HttpStatus.OK, rsp.getStatus());   (5)
        assertNotNull(rsp.body()); (6)

        //when:
        List<GithubRelease> releases = rsp.body();

        //then:
        for (String name : expectedReleases) {
            assertTrue(releases.stream().map(GithubRelease::getName).anyMatch(name::equals));
        }
    }

    @Test
    public void verifyGithubReleasesCanBeFetchedWithCompileTimeAutoGeneratedAtClient() {
        //when:
        HttpRequest<Object> request = HttpRequest.GET("/github/releases-lowlevel");

        Flowable<GithubRelease> githubReleaseStream = client.jsonStream(request, GithubRelease.class); (7)
        Iterable<GithubRelease> githubReleases = githubReleaseStream.blockingIterable();

        //then:
        for (String name : expectedReleases) {
            assertTrue(StreamSupport.stream(githubReleases.spliterator(), false)
                    .map(GithubRelease::getName)
                    .anyMatch(name::equals));
        }
    }
}
1 Annotate the class with @MicronautTest to let Micronaut starts the embedded server and inject the beans. More info: https://micronaut-projects.github.io/micronaut-test/latest/guide/index.html.
2 Inject the RxStreamingHttpClient bean in the application context.
3 Sometimes, receiving just the object is not enough, and you need information about the response. In this case, instead of retrieve you should use the exchange method.
4 Micronaut makes it easy to parse JSON into Java objects.
5 Use status to check the HTTP status code.
6 Use .body() to retrieve the parsed payload.
7 Use the jsonStream method, which returns a Flowable, to consume the endpoint which generates a JSON Stream.

5. Testing the Application

To run the tests:

$ ./mvnw test

6. HTTP Client Filter

Often, you need to include the same HTTP headers or URL parameters in a set of requests against a third-party API or when calling another Microservice. To simplify this, Micronaut includes the ability to define HttpClientFilter classes that are applied to all matching HTTP clients.

For a real world example, let us provide GitHub Authentication via an HttpClientFilter. Follow the steps in to create you own Personal Token. Then you can use those credentials to access the GitHub API using Basic Auth.

Create a Filter:

src/main/java/example/micronaut/GithubFilter.java
package example.micronaut;

import io.micronaut.context.annotation.Requires;
import io.micronaut.http.HttpResponse;
import io.micronaut.http.MutableHttpRequest;
import io.micronaut.http.annotation.Filter;
import io.micronaut.http.filter.ClientFilterChain;
import io.micronaut.http.filter.HttpClientFilter;
import org.reactivestreams.Publisher;

@Filter("/repos/**") (1)
@Requires(property = GithubConfiguration.PREFIX + ".username") (2)
@Requires(property = GithubConfiguration.PREFIX + ".token") (2)
public class GithubFilter implements HttpClientFilter {

    private final GithubConfiguration configuration;

    public GithubFilter(GithubConfiguration configuration) { (3)
        this.configuration = configuration;
    }

    @Override
    public Publisher<? extends HttpResponse<?>> doFilter(MutableHttpRequest<?> request, ClientFilterChain chain) {
        return chain.proceed(request.basicAuth(configuration.getUsername(), configuration.getToken())); (4)
    }
}
1 Supply the pattern you want to match to the @Filter annotation.
2 Micronaut will not load the Bean unless configuration properties are set.
3 Constructor injection of the configuration parameters.
4 Enhance every request sent to GitHub API providing Basic Authentication.

6.1. Configuration Parameters

Add your GitHub username and token to src/main/resource/application.yml

github:
  organization: micronaut-projects
  repo: micronaut-core
  username: yourgithubusername
  token: xxxxxxxxxxxx

Add to src/main/resources/logback.xml, a logger to see Micronaut’s HTTP client output.

<logger name="io.micronaut.http.client" level="TRACE"/>

If you run again the tests, you will see the that the Filter is invoked and HTTP Basic Auth is used against GitHub API.

13:09:56.662 [default-nioEventLoopGroup-1-4] DEBUG i.m.h.client.netty.DefaultHttpClient - Sending HTTP GET to https://api.github.com/repos/micronaut-projects/micronaut-core/releases
13:09:56.663 [default-nioEventLoopGroup-1-4] TRACE i.m.h.client.netty.DefaultHttpClient - User-Agent: Micronaut HTTP Client
13:09:56.663 [default-nioEventLoopGroup-1-4] TRACE i.m.h.client.netty.DefaultHttpClient - Accept: application/json
13:09:56.663 [default-nioEventLoopGroup-1-4] TRACE i.m.h.client.netty.DefaultHttpClient - Authorization: Basic xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
13:09:56.664 [default-nioEventLoopGroup-1-4] TRACE i.m.h.client.netty.DefaultHttpClient - host: api.github.com

7. Generate a Micronaut app’s Native Image with GraalVM

We are going to use GraalVM, the polyglot embeddable virtual machine, to generate a Native image of our Micronaut application.

Native images compiled with GraalVM ahead-of-time improve the startup time and reduce the memory footprint of JVM-based applications.

Use of GraalVM’s native-image tool is only supported in Java or Kotlin projects. Groovy relies heavily on reflection which is only partially supported by GraalVM.

7.1. Native Image generation

The easiest way to install GraalVM is to use SDKMan.io.

# For Java 8
$ sdk install java 21.1.0.r8-grl

# For Java 11
$ sdk install java 21.1.0.r11-grl

You need to install the native-image component which is not installed by default.

$ gu install native-image

To generate a native image using Maven run:

$ ./mvnw package -Dpackaging=native-image

The native image will be created in target/application and can be run with ./target/application.

8. Next steps

9. Help with Micronaut

Object Computing, Inc. (OCI) sponsored the creation of this Guide. A variety of consulting and support services are available.