23
loading...
This website collects cookies to deliver better user experience
package com.bazlur;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
public class Day011 {
public static final String CHUCK_NORRIS_RANDOM_JOKES_API = "https://api.chucknorris.io/jokes/random";
public static void main(String[] args) throws URISyntaxException, IOException, InterruptedException {
var client = HttpClient.newBuilder()
.version(HttpClient.Version.HTTP_2)
.connectTimeout(Duration.ofSeconds(20))
.build();
var request = HttpRequest.newBuilder()
.uri(new URI(CHUCK_NORRIS_RANDOM_JOKES_API))
.header("Accept", "application/json")
.GET()
.build();
var response = client.send(request, HttpResponse.BodyHandlers.ofString());
var body = response.body();
System.out.println("body = " + body);
}
}
send()
to sendAsync()
var completableFuture = client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
.thenApply(HttpResponse::body);
CompletableFuture
.implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.12.3'
static class UncheckedObjectMapper extends ObjectMapper {
Joke readValue(String content) {
try {
return this.readValue(content, new TypeReference<>() {
});
} catch (JsonProcessingException e) {
throw new CompletionException(e);
}
}
}
{
"categories": [],
"created_at": "2020-01-05 13:42:28.420821",
"icon_url": "https://assets.chucknorris.host/img/avatar/chuck-norris.png",
"id": "Kqck_igkQNCw6Y0ADR2iyA",
"updated_at": "2020-01-05 13:42:28.420821",
"url": "https://api.chucknorris.io/jokes/Kqck_igkQNCw6Y0ADR2iyA",
"value": "The Terminator wears a Chuck Norris tee shirt. ."
}
record
for this.@JsonIgnoreProperties(ignoreUnknown = true)
record Joke(
@JsonProperty("created_at")
String createdAt,
@JsonProperty("icon_url")
String iconUrl,
@JsonProperty("id")
String id,
@JsonProperty("updated_at")
String updatedAt,
@JsonProperty("url")
String url,
@JsonProperty("value")
String value
) { }
package com.bazlur;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandlers;
import java.util.concurrent.CompletionException;
import java.util.concurrent.ExecutionException;
public class Day011_1 {
public static final String CHUCKNORRIS_RANDOM_JOKES_API = "https://api.chucknorris.io/jokes/random";
public static void main(String[] args) throws URISyntaxException, IOException, InterruptedException, ExecutionException {
var objectMapper = new UncheckedObjectMapper();
objectMapper.enable(DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT);
var httpClient = HttpClient.newHttpClient();
var request = HttpRequest.newBuilder()
.uri(new URI(CHUCKNORRIS_RANDOM_JOKES_API))
.header("Accept", "application/json")
.GET()
.build();
var joke = httpClient.sendAsync(request, BodyHandlers.ofString())
.thenApply(HttpResponse::body)
.thenApply(objectMapper::readValue)
.get();
System.out.println("joke = " + joke.value());
}
@JsonIgnoreProperties(ignoreUnknown = true)
record Joke(
@JsonProperty("created_at")
String createdAt,
@JsonProperty("icon_url")
String iconUrl,
@JsonProperty("id")
String id,
@JsonProperty("updated_at")
String updatedAt,
@JsonProperty("url")
String url,
@JsonProperty("value")
String value
) { }
static class UncheckedObjectMapper extends ObjectMapper {
Joke readValue(String content) {
try {
return this.readValue(content, new TypeReference<>() {
});
} catch (JsonProcessingException e) {
throw new CompletionException(e);
}
}
}
}