Java 11: Synchronous HTTP GET into a String

Java 11+ ships with a new HttpClient

To fetch the content of a website synchronously into a String:



import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse.BodyHandlers;

...a request has to be constructed with an URI:


var uri = URI.create("https://airhacks.tv");
var request = HttpRequest.newBuilder(uri).GET().build();

...then sent (=executed) with an instance of HttpClient:


var client = HttpClient.newHttpClient();
String responseBody = client.send(request, BodyHandlers.ofString()).body();
System.out.println(responseBody);

jOOQ Loves SQL--airhacks.fm episode

Subscribe to airhacks.fm podcast via: spotify| iTunes| RSS

The #115 airhacks.fm episode with Lukas Eder (@lukaseder) about:
abstractions, query builders, jOOQ, design decisions and usability, database vs. Java first, managing external dependencies, open source and the value of commercial support
is available for

JDD 2020: Building Kickass Frontends with Vanilla Web Components

Building a frontend with vanilla Web Components for a "Kickass Java Backend"

GitHub's Time Elements Web Components

GitHub's Time Elements: https://github.com/github/time-elements is a set of web components which comes without any dependencies, does not require any processing and is useful for formatting time and dates:

J4K: Live Virtual MicroProfile on Kubernetes / AWS / EKS Hacking #slideless

Building, designing, and structuring a cloud-native MicroProfile (Metrics, OpenAPI, FaultTolerance, JAX-RS, Config, Health, JSON-B, REST Client and JAX-RS) / Quarkus application. AWS / EKS / Kubernetes deployment included:

Building Software for Data Center Providers with Java--airhacks.fm episode

Subscribe to airhacks.fm podcast via: spotify| iTunes| RSS

The #114 airhacks.fm episode with Ruslan Synytsky (@siruslan) about:
working for secret organizations with Java, Java EE and GlassFish, building a Java BaaS and PaaS with Java, rightsizing and stateful workloads with Jelastic
is available for

Java EE 6 to Jakarta EE 9 Migration

Migration of an 8 years old Java EE 6 / Java 6 application written on stage of the JDD 2012 (Java EE: Future Is Now, But It Is Not Evenly Distributed Yet) conference to Jakarta EE 9 with the new jakarta.* packages.

GlassFish 6 was used as an example Jakarta EE 9 runtime:

What Should Happen in 2021 with Java Backends and Web Frontends--JCON 2020 Keynote

Past, present and possible future of Java backends and Web frontends:

MicroProfile 3.3: Links to Specs and JavaDoc

MicroProfile also relies on Java EE 8 APIs. The specs from the opensource Java EE 8 counterpart: Jakarta EE 8 are easier (directly) accessible:

Emitting JAX-RS Messages into MicroProfile Reactive Messaging Channels

Reactive Messaging for MicroProfile API allows mapping of method's parameters and return values to virtual channels:


import org.eclipse.microprofile.reactive.messaging.Incoming;
import org.eclipse.microprofile.reactive.messaging.Outgoing;

public class HelloListener {

    @Incoming("hello")
    @Outgoing("confirmed")
    public String onHello(String hello) {
        return "confirmed: " + hello;
    }
}    

Messages consumed by Jakarta RESTful Web Services / Java API for RESTful Web Services can be ingested into the system with the injected Emitter qualified with the @Channel annotation.

After sending the String to the Emitter, the message becomes available in the channel and can be consumed as method parameter or sent to Kafka topic:


import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.MediaType;

import org.eclipse.microprofile.reactive.messaging.Channel;
import org.eclipse.microprofile.reactive.messaging.Emitter;

@Path("/hello")
public class HelloResource {

    @Inject
    @Channel("hello")
    Emitter<String> emitter;

    @POST
    @Consumes(MediaType.TEXT_PLAIN)
    public void forward(String message) {
        emitter.send(message);
    }
}    

See it in action, and from scratch in 7 minutes:

Online Workshops
...the last 150 posts
...the last 10 comments
License