These tutorials target Micronaut Framework 3. Read, Guides for Micronaut Framework 4.

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: 3.9.2

1. Getting Started

In this guide, we will create a Micronaut application written in Groovy 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 application step by step. However, you can go right to the completed example.

4. Writing the Application

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

mn create-app example.micronaut.micronautguide \
    --features=reactor,serialization-jackson \
    --build=maven
    --lang=groovy
If you don’t specify the --build argument, Gradle is used as the build tool.
If you don’t specify the --lang argument, Java is used as the language.

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

If you use Micronaut Launch, select Micronaut Application as application type and add reactor, and serialization-jackson features.

If you have an existing Micronaut application and want to add the functionality described here, you can view the dependency and configuration changes from the specified features and apply those changes to your application.

4.1. GitHub API

In this guide, you will 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

Add configuration to associate a service identifier to the GitHub API URL.

src/main/resources/application.yml
---
micronaut:
  http:
    services:
      github:
        url: 'https://api.github.com'

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

src/main/groovy/example/micronaut/GithubConfiguration.groovy
package example.micronaut

import groovy.transform.CompileStatic
import io.micronaut.context.annotation.ConfigurationProperties
import io.micronaut.context.annotation.Requires

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

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

    String organization
    String repo
    String username
    String token
}

In this guide, you will 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/groovy/example/micronaut/GithubRelease.groovy
package example.micronaut

import groovy.transform.CompileStatic
import io.micronaut.serde.annotation.Serdeable

@Serdeable
@CompileStatic
class GithubRelease {
    String name
    String url
}

Create GithubLowLevelClient:

src/main/groovy/example/micronaut/GithubLowLevelClient.groovy
package example.micronaut

import groovy.transform.CompileStatic
import io.micronaut.core.type.Argument
import io.micronaut.http.HttpRequest
import io.micronaut.http.client.HttpClient
import io.micronaut.http.client.annotation.Client
import io.micronaut.http.uri.UriBuilder
import jakarta.inject.Singleton
import reactor.core.publisher.Mono

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

@Singleton (1)
@CompileStatic
class GithubLowLevelClient {

    private final HttpClient httpClient
    private final URI uri

    GithubLowLevelClient(@Client(id = "github") HttpClient httpClient,  (2)
                         GithubConfiguration configuration) {  (3)
        this.httpClient = httpClient
        uri = UriBuilder.of("/repos")
            .path(configuration.organization)
            .path(configuration.repo)
            .path("releases")
            .build()
    }

    Mono<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)
        Mono.from(httpClient.retrieve(req, Argument.listOf(GithubRelease))) (7)
    }
}
1 Use jakarta.inject.Singleton to designate a class as a singleton.
2 Inject HttpClient via constructor injection. The @Client id member uses github; the service identifier set in the configuration.
3 Inject the previously defined configuration parameters.
4 Creating HTTP Requests is easy thanks to the Micronaut framework 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>.
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 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/groovy/example/micronaut/GithubApiClient.groovy
package example.micronaut

import io.micronaut.http.annotation.Get
import io.micronaut.http.annotation.Header
import io.micronaut.http.client.annotation.Client
import org.reactivestreams.Publisher;

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

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

    @Get('/repos/${github.organization}/${github.repo}/releases') (4)
    Publisher<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 any reactive type of any implementation (RxJava, Reactor…​), but it’s better to use the Reactive Streams public interfaces like Publisher.

4.4. Controller

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

  • The Micronaut framework 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).

  • The Framework enables you to consume/produce JSON Streams.

src/main/groovy/example/micronaut/GithubController.groovy
package example.micronaut

import groovy.transform.CompileStatic
import io.micronaut.http.MediaType
import io.micronaut.http.annotation.Controller
import io.micronaut.http.annotation.Get
import org.reactivestreams.Publisher
import reactor.core.publisher.Mono

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

    private final GithubLowLevelClient githubLowLevelClient
    private final GithubApiClient githubApiClient

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

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

    @Get(uri = "/releases", produces = MediaType.APPLICATION_JSON_STREAM) (5)
    Publisher<GithubRelease> fetchReleases() { (6)
        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 maps the index method to all requests that use an HTTP GET
4 The releasesWithLowLevelClient returns a Mono 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 any reactive type of any implementation (RxJava, Reactor…​), but it’s better to use the Reactive Streams public interfaces like Publisher.

4.5. Tests

Create a test to verify that both clients work as expected, and the controller echoes the output of the GitHub API in a Reactive way.

src/test/groovy/example/micronaut/GithubControllerSpec.groovy
package example.micronaut

import io.micronaut.context.ApplicationContext
import io.micronaut.context.annotation.Requires
import io.micronaut.core.io.ResourceLoader
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.annotation.Consumes
import io.micronaut.http.annotation.Controller
import io.micronaut.http.annotation.Get
import io.micronaut.http.annotation.Header
import io.micronaut.http.client.BlockingHttpClient
import io.micronaut.http.client.HttpClient
import io.micronaut.runtime.server.EmbeddedServer
import spock.lang.Ignore
import spock.lang.Specification
import java.util.regex.Pattern

class GithubControllerSpec extends Specification {
    private static Pattern MICRONAUT_RELEASE =
            Pattern.compile("[Micronaut|Micronaut Framework] [0-9].[0-9].[0-9]([0-9])?( (RC|M)[0-9])?")

    @Ignore
    void "verify GithubReleases can Be fetched With Low Level Http Client"() {
        given:
        EmbeddedServer github = ApplicationContext.run(EmbeddedServer,
                Collections.singletonMap("spec.name", "GithubControllerTest")) (1)
        String url = "http://localhost:${github.port}"
        EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer,
                Collections.singletonMap("micronaut.http.services.github.url", url)) (2)
        HttpClient httpClient = embeddedServer.getApplicationContext()
                .createBean(HttpClient, embeddedServer.getURL())
        BlockingHttpClient client = httpClient.toBlocking()

        when:
        HttpRequest<?> request = HttpRequest.GET("/github/releases-lowlevel")
        HttpResponse<List<GithubRelease>> rsp = client.exchange(request, (3)
                Argument.listOf(GithubRelease)) (4)

        then:
        HttpStatus.OK == rsp.getStatus()   (5)

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

        then:
        releases
        releases.stream()
                .map(GithubRelease::getName)
                .allMatch(name -> MICRONAUT_RELEASE.matcher(name)
                        .find())

        cleanup:
        httpClient.close()
        embeddedServer.close()
        github.close()
    }

    @Requires(property = "spec.name", value = "GithubControllerTest") (1)
    @Controller
    static class GithubReleases {
        private final ResourceLoader resourceLoader
        GithubReleases(ResourceLoader resourceLoader) {
            this.resourceLoader = resourceLoader
        }

        @Consumes(value = ["application/vnd.github.v3+json", "application/json"])
        @Get("/repos/micronaut-projects/micronaut-core/releases") (3)
        String coreReleases() {
            resourceLoader.getResourceAsStream("releases.json")
                    .map { it.text }
                    .orElseGet(() -> '[]')
        }
    }
}
1 Combine @Requires and properties (either via the @Property annotation or by passing properties when starting the context) to avoid bean pollution.
2 This test mocks an HTTP Server for GitHub with an extra Micronaut Embedded Server. This allows you to test how your application behaves with a specific JSON response or avoid issues such as rate limits which can make your tests flaky.
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 HTTP Client simplifies binding a JSON array to a list of POJOs by using Argument::listOf.
5 Use status to check the HTTP status code.
6 Use .body() to retrieve the parsed payload.

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, the Micronaut framework 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 your own Personal Token. Then you can use those credentials to access the GitHub API using Basic Auth.

Create a Filter:

src/main/groovy/example/micronaut/GithubFilter.groovy
package example.micronaut

import groovy.transform.CompileStatic
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

@CompileStatic
@Filter("/repos/**") (1)
@Requires(property = "github.username") (2)
@Requires(property = "github.token") (2)
class GithubFilter implements HttpClientFilter {

    private final GithubConfiguration configuration

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

    @Override
    Publisher<? extends HttpResponse<?>> doFilter(MutableHttpRequest<?> request, ClientFilterChain chain) {
        chain.proceed(request.basicAuth(configuration.getUsername(), configuration.getToken())) (4)
    }
}
1 Supply the pattern you want to match to the @Filter annotation.
2 The Micronaut framework 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 a logger to src/main/resources/logback.xml to see the 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. Next steps

8. Help with the Micronaut Framework

The Micronaut Foundation sponsored the creation of this Guide. A variety of consulting and support services are available.