7. Generate a Micronaut Application Native Executable with GraalVM
7.1. GraalVM installation
7.2. Native executable generation
8. Next steps
9. Help with the Micronaut Framework
10. License
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: 4.6.3
mn create-app example.micronaut.micronautguide \
--features=http-client,graalvm \
--build=gradle \
--lang=java \
--test=junit
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
http-client
, and
graalvm
features.
4.1. Dependency
To use the
Micronaut HTTP Client based on Netty
, add the following dependency:
build.gradle
implementation("io.micronaut:micronaut-http-client")
To use the
Micronaut HTTP Client based on Java HTTP Client
, add the following dependency:
build.gradle
implementation("io.micronaut:micronaut-http-client-jdk")
4.2. GitHub API
In this guide, you will consume the
GitHub API
from a Micronaut application.
In this guide, you will fetch
Micronaut Core releases
via the
List releases
endpoint.
This API resource can be consumed by both authenticated and anonymous clients. Initially, you will consume it anonymously, later we will discuss authentication.
Create a record to parse the JSON response into an object:
src/main/java/example/micronaut/GithubRelease.java
package example.micronaut;
import io.micronaut.serde.annotation.Serdeable;
@Serdeable
public record GithubRelease(String name, String url) {
4.3. Configuration
Modify src/main/resources/application.properties
to create some configuration parameters.
src/main/resources/application.properties
github.organization=micronaut-projects
github.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;
import io.micronaut.core.annotation.Nullable;
@ConfigurationProperties(GithubConfiguration.PREFIX)
@Requires(property = GithubConfiguration.PREFIX)
public record GithubConfiguration(String organization,
String repo,
@Nullable String username,
@Nullable String token) {
public static final String PREFIX = "github";
4.3.1. JSON Codec Configuration
Add configuration to treat application/vnd.github.v3+json
as JSON.
src/main/resources/application.properties
micronaut.codec.json.additional-types[0]=application/vnd.github.v3+json
4.3.2. HTTP Client Service Configuration
Add configuration to associate a service identifier to the GitHub API URL.
src/main/resources/application.properties
micronaut.http.services.github.url=https://api.github.com
4.4. Low Level Client
Initially, you will create a Bean which uses the low-level Client API.
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.HttpClient;
import io.micronaut.http.client.annotation.Client;
import io.micronaut.http.uri.UriBuilder;
import jakarta.inject.Singleton;
import org.reactivestreams.Publisher;
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 HttpClient httpClient;
private final URI uri;
public 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();
Publisher<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)
return httpClient.retrieve(req, Argument.listOf(GithubRelease.class)); (7)
Inject HttpClient
via constructor injection. The @Client
id
member uses github
; the service identifier set in the configuration.
Inject the previously defined configuration parameters.
Creating HTTP Requests is easy thanks to the Micronaut framework fluid API.
GitHub API requires to set the User-Agent
header.
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.
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>
.
4.5. 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/java/example/micronaut/GithubApiClient.java
package example.micronaut;
import io.micronaut.core.async.annotation.SingleResult;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.annotation.Header;
import io.micronaut.http.client.annotation.Client;
import org.reactivestreams.Publisher;
import java.util.List;
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)
public interface GithubApiClient {
@Get("/repos/${github.organization}/${github.repo}/releases") (4)
@SingleResult (5)
Publisher<List<GithubRelease>> fetchReleases(); (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.
You can use configuration parameter interpolation when you define the path of the GET endpoint.
Annotation to describe that an API emits a single result even if the return type is an org.reactivestreams.Publisher
.
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.6. Controller
Create a Controller. It uses both (low-level and declarative clients). The Micronaut framework supports Reactive Streams implementations such as RxJava or Project Reactor. Thus, you can efficiently compose multiple HTTP client calls without blocking (which will limit the throughput and scalability of your application).
src/main/java/example/micronaut/GithubController.java
package example.micronaut;
import io.micronaut.core.async.annotation.SingleResult;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import org.reactivestreams.Publisher;
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)
@SingleResult (4)
Publisher<List<GithubRelease>> releasesWithLowLevelClient() {
return githubLowLevelClient.fetchReleases();
@Get("/releases") (5)
@SingleResult (4)
Publisher<List<GithubRelease>> fetchReleases() { (6)
return githubApiClient.fetchReleases();
The class is defined as a controller with the @Controller annotation mapped to the path /github
.
Inject beans via constructor injection.
The @Get annotation maps the index
method to all requests that use an HTTP GET
Annotation to describe that an API emits a single result even if the return type is an org.reactivestreams.Publisher
.
The @Get annotation maps the fetchReleases
method to an HTTP GET request on /releases
.
src/test/resources/releases.json
[{"url":"https://api.github.com/repos/micronaut-projects/micronaut-core/releases/91622014","assets_url
":"https://api.github.com/repos/micronaut-projects/micronaut-core/releases/91622014/assets","upload_url":"https://uploads.github.com/repos/micronaut-projects/micronaut-core/releases/91622014/assets{?name,label}","html_url":"https://github.com/micronaut-projects/micronaut-core/releases/tag/v3.8.4","id":91622014,"author":{"login":"sdelamo","id":864788,"node_id":"MDQ6VXNlcjg2NDc4OA==","avatar_url":"https://avatars.githubusercontent.com/u/864788?v=4","gravatar_id":"","url":"https://api.github.com/users/sdelamo","html_url":"https://github.com/sdelamo","followers_url":"https://api.github.com/users/sdelamo/followers","following_url":"https://api.github.com/users/sdelamo/following{/other_user}","gists_url":"https://api.github.com/users/sdelamo/gists{/gist_id}","starred_url":"https://api.github.com/users/sdelamo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sdelamo/subscriptions","organizations_url":"https://api.github.com/users/sdelamo/orgs","repos_url":"https://api.github.com/users/sdelamo/repos","events_url":"https://api.github.com/users/sdelamo/events{/privacy}","received_events_url":"https://api.github.com/users/sdelamo/received_events","type":"User","site_admin":false},"node_id":"RE_kwDOB2eaPM4Fdgp-","tag_name":"v3.8.4","target_commitish":"3.8.x","name":"Micronaut Framework 3.8.4","draft":false,"prerelease":false,"created_at":"2023-02-07T15:53:03Z","published_at":"2023-02-07T15:53:05Z","assets":[{"url":"https://api.github.com/repos/micronaut-projects/micronaut-core/releases/assets/94667997","id":94667997,"node_id":"RA_kwDOB2eaPM4FpITd","name":"artifacts.zip","label":"","uploader":{"login":"github-actions[bot]","id":41898282,"node_id":"MDM6Qm90NDE4OTgyODI=","avatar_url":"https://avatars.githubusercontent.com/in/15368?v=4","gravatar_id":"","url":"https://api.github.com/users/github-actions%5Bbot%5D","html_url":"https://github.com/apps/github-actions","followers_url":"https://api.github.com/users/github-actions%5Bbot%5D/followers","following_url":"https://api.github.com/users/github-actions%5Bbot%5D/following{/other_user}","gists_url":"https://api.github.com/users/github-actions%5Bbot%5D/gists{/gist_id}","starred_url":"https://api.github.com/users/github-actions%5Bbot%5D/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/github-actions%5Bbot%5D/subscriptions","organizations_url":"https://api.github.com/users/github-actions%5Bbot%5D/orgs","repos_url":"https://api.github.com/users/github-actions%5Bbot%5D/repos","events_url":"https://api.github.com/users/github-actions%5Bbot%5D/events{/privacy}","received_events_url":"https://api.github.com/users/github-actions%5Bbot%5D/received_events","type":"Bot","site_admin":false},"content_type":"application/zip","state":"uploaded","size":28063979,"download_count":33,"created_at":"2023-02-07T16:18:05Z","updated_at":"2023-02-07T16:18:06Z","browser_download_url":"https://github.com/micronaut-projects/micronaut-core/releases/download/v3.8.4/artifacts.zip"},{"url":"https://api.github.com/repos/micronaut-projects/micronaut-core/releases/assets/94669478","id":94669478,"node_id":"RA_kwDOB2eaPM4FpIqm","name":"multiple.intoto.jsonl","label":"","uploader":{"login":"github-actions[bot]","id":41898282,"node_id":"MDM6Qm90NDE4OTgyODI=","avatar_url":"https://avatars.githubusercontent.com/in/15368?v=4","gravatar_id":"
","url":"https://api.github.com/users/github-actions%5Bbot%5D","html_url":"https://github.com/apps/github-actions","followers_url":"https://api.github.com/users/github-actions%5Bbot%5D/followers","following_url":"https://api.github.com/users/github-actions%5Bbot%5D/following{/other_user}","gists_url":"https://api.github.com/users/github-actions%5Bbot%5D/gists{/gist_id}","starred_url":"https://api.github.com/users/github-actions%5Bbot%5D/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/github-actions%5Bbot%5D/subscriptions","organizations_url":"https://api.github.com/users/github-actions%5Bbot%5D/orgs","repos_url":"https://api.github.com/users/github-actions%5Bbot%5D/repos","events_url":"https://api.github.com/users/github-actions%5Bbot%5D/events{/privacy}","received_events_url":"https://api.github.com/users/github-actions%5Bbot%5D/received_events","type":"Bot","site_admin":false},"content_type":"application/octet-stream","state":"uploaded","size":65612,"download_count":34,"created_at":"2023-02-07T16:32:49Z","updated_at":"2023-02-07T16:32:49Z","browser_download_url":"https://github.com/micronaut-projects/micronaut-core/releases/download/v3.8.4/multiple.intoto.jsonl"}],"tarball_url":"https://api.github.com/repos/micronaut-projects/micronaut-core/tarball/v3.8.4","zipball_url":"https://api.github.com/repos/micronaut-projects/micronaut-core/zipball/v3.8.4","body":"<!-- Release notes generated using configuration in .github/release.yml at 3.8.x -->\r\n\r\n\r\n## What's Changed\r\n\r\n### Bugs 🐛\r\n* Allow programmatic logback config if xml config is absent (#8674)\r\n* Tweak uri to account for WebLogic/Windows URIs by @mattmoss in https://github.com/micronaut-projects/micronaut-core/pull/8704\r\n\r\n### Docs 📖\r\n* Docs on self-signed cert setup (#8684)\r\n\r\n### Dependency Upgrades 🚀\r\n\r\n* Micronaut Test to 3.8.2 (#8728)\r\n* Micronaut OpenAPI to 4.8.3 (#8724)\r\n* Micronaut Data to 3.9.6 (#8711)\r\n* Micronaut Rabbit to 3.4.1 (#8709)\r\n* Micronaut Azure to 3.7.1 (#8682)\r\n* Micronaut Micrometer to 4.7.2 (#8681)\r\n\r\n### Tests ✅\r\n\r\n* Require docker for test (#8698)\r\n* Add octet stream serialization to the TCK (#8712)\r\n\r\n**Full Changelog**: https://github.com/micronaut-projects/micronaut-core/compare/v3.8.3...v3.8.4","mentions_count":1}]
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/java/example/micronaut/GithubControllerTest.java
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.Controller;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.annotation.Produces;
import io.micronaut.http.client.BlockingHttpClient;
import io.micronaut.http.client.HttpClient;
import io.micronaut.runtime.server.EmbeddedServer;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.regex.Pattern;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static java.nio.charset.StandardCharsets.UTF_8;
class GithubControllerTest {
private static Pattern MICRONAUT_RELEASE =
Pattern.compile("Micronaut (Core |Framework )?v?\\d+.\\d+.\\d+( (RC|M)\\d)?");
@Test
void verifyGithubReleasesCanBeFetchedWithLowLevelHttpClient() {
EmbeddedServer github = ApplicationContext.run(EmbeddedServer.class,
Map.of("micronaut.codec.json.additional-types", "application/vnd.github.v3+json",
"spec.name", "GithubControllerTest")); (1)
EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer.class,
Collections.singletonMap("micronaut.http.services.github.url",
"http://localhost:" + github.getPort())); (2)
HttpClient httpClient = embeddedServer.getApplicationContext()
.createBean(HttpClient.class, embeddedServer.getURL());
BlockingHttpClient client = httpClient.toBlocking();
assertReleases(client, "/github/releases");
assertReleases(client, "/github/releases-lowlevel");
httpClient.close();
embeddedServer.close();
github.close();
private static void assertReleases(BlockingHttpClient client, String path) {
HttpRequest<Object> request = HttpRequest.GET(path);
HttpResponse<List<GithubRelease>> rsp = client.exchange(request, (4)
Argument.listOf(GithubRelease.class)); (5)
assertEquals(HttpStatus.OK, rsp.getStatus()); (6)
assertReleases(rsp.body()); (7)
private static void assertReleases(List<GithubRelease> releases) {
assertNotNull(releases);
assertTrue(releases.stream()
.map(GithubRelease::name)
.allMatch(name -> MICRONAUT_RELEASE.matcher(name)
.find()));
@Requires(property = "spec.name", value = "GithubControllerTest") (1)
@Controller
static class GithubReleases {
private final ResourceLoader resourceLoader;
GithubReleases(ResourceLoader resourceLoader) {
this.resourceLoader = resourceLoader;
@Produces("application/vnd.github.v3+json")
@Get("/repos/micronaut-projects/micronaut-core/releases")
Optional<String> coreReleases() {
return resourceLoader.getResourceAsStream("releases.json") (3)
.flatMap(inputStream -> {
try {
return Optional.of(new String(inputStream.readAllBytes(), UTF_8));
} catch (IOException e) {
return Optional.empty();
Combine @Requires
and properties (either via the @Property
annotation or by passing properties when starting the context) to avoid bean pollution.
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.
Create a sample releases.json
file in src/test/resources
directory. To get some test data call github api with curl or provide a few entries yourself.
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.
Micronaut HTTP Client simplifies binding a JSON array to a list of POJOs by using Argument::listOf
.
Use status
to check the HTTP status code.
Use .body()
to retrieve the parsed payload.
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/java/example/micronaut/GithubFilter.java
package example.micronaut;
import io.micronaut.context.annotation.Requires;
import io.micronaut.http.MutableHttpRequest;
import io.micronaut.http.annotation.ClientFilter;
import io.micronaut.http.annotation.RequestFilter;
@ClientFilter("/repos/**") (1)
@Requires(property = GithubConfiguration.PREFIX + ".username") (2)
@Requires(property = GithubConfiguration.PREFIX + ".token") (2)
public class GithubFilter {
private final GithubConfiguration configuration;
public GithubFilter(GithubConfiguration configuration) { (3)
this.configuration = configuration;
@RequestFilter (4)
public void doFilter(MutableHttpRequest<?> request) {
request.basicAuth(configuration.username(), configuration.token()); (5)
Annotate the class with @ClientFilter
and define the ANT Matcher pattern to intercept all the calls to the desire URI.
The Micronaut framework will not load the bean unless configuration properties are set.
Constructor injection of the configuration parameters.
A request filter is called before the request is sent out.
Enhance every request sent to GitHub API providing Basic Authentication.
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: MASKED
13:09:56.664 [default-nioEventLoopGroup-1-4] TRACE i.m.h.client.netty.DefaultHttpClient - host: api.github.com
We will use GraalVM, the polyglot embeddable virtual machine, to generate a native executable of our Micronaut application.
Compiling native executables ahead of time with GraalVM improves startup time and reduces the memory footprint of JVM-based applications.
7.1. GraalVM installation
The easiest way to install GraalVM on Linux or Mac is to use SDKMan.io.
Java 17
sdk install java 17.0.12-graal
Java 17
sdk use java 17.0.12-graal
For installation on Windows, or for manual installation on Linux or Mac, see the GraalVM Getting Started documentation.
The previous command installs Oracle GraalVM, which is free to use in production and free to redistribute, at no cost, under the GraalVM Free Terms and Conditions.
Alternatively, you can use the GraalVM Community Edition:
Java 17
sdk install java 17.0.9-graalce
Java 17
sdk use java 17.0.9-graalce
The native executable is created in build/native/nativeCompile
directory and can be run with build/native/nativeCompile/micronautguide
.
It is possible to customize the name of the native executable or pass additional parameters to GraalVM:
build.gradle
graalvmNative {
binaries {
main {
imageName.set('mn-graalvm-application') (1)
buildArgs.add('--verbose') (2)