diff --git a/src/main/java/io/github/nstdio/http/ext/BodyHandlers.java b/src/main/java/io/github/nstdio/http/ext/BodyHandlers.java index 294d994..a7afa15 100644 --- a/src/main/java/io/github/nstdio/http/ext/BodyHandlers.java +++ b/src/main/java/io/github/nstdio/http/ext/BodyHandlers.java @@ -22,6 +22,7 @@ import java.io.InputStream; import java.net.http.HttpResponse; import java.net.http.HttpResponse.BodyHandler; +import java.util.function.Supplier; /** * Implementations of {@code BodyHandler}'s. @@ -51,7 +52,7 @@ public static BodyHandler ofDecompressing(BodyHandler downstream) { * @param The required type. * @return The JSON body handler. */ - public static BodyHandler ofJson(Class targetType) { + public static BodyHandler> ofJson(Class targetType) { return responseInfo -> BodySubscribers.ofJson(targetType); } @@ -62,7 +63,7 @@ public static BodyHandler ofJson(Class targetType) { * @param The required type. * @return The JSON body handler. */ - public static BodyHandler ofJson(TypeReference targetType) { + public static BodyHandler> ofJson(TypeReference targetType) { return responseInfo -> BodySubscribers.ofJson(targetType); } diff --git a/src/main/java/io/github/nstdio/http/ext/BodySubscribers.java b/src/main/java/io/github/nstdio/http/ext/BodySubscribers.java index cf65e7c..be08d63 100644 --- a/src/main/java/io/github/nstdio/http/ext/BodySubscribers.java +++ b/src/main/java/io/github/nstdio/http/ext/BodySubscribers.java @@ -16,6 +16,9 @@ package io.github.nstdio.http.ext; +import static java.net.http.HttpResponse.BodySubscribers.mapping; +import static java.net.http.HttpResponse.BodySubscribers.ofInputStream; + import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.JavaType; import com.fasterxml.jackson.databind.ObjectMapper; @@ -24,6 +27,7 @@ import java.io.IOException; import java.io.UncheckedIOException; import java.net.http.HttpResponse.BodySubscriber; +import java.util.function.Supplier; import static java.net.http.HttpResponse.BodySubscribers.mapping; import static java.net.http.HttpResponse.BodySubscribers.ofByteArray; @@ -35,30 +39,30 @@ public final class BodySubscribers { private BodySubscribers() { } - public static BodySubscriber ofJson(Class targetType) { + public static BodySubscriber> ofJson(Class targetType) { return ofJson(TF.constructType(targetType)); } - public static BodySubscriber ofJson(TypeReference targetType) { + public static BodySubscriber> ofJson(TypeReference targetType) { return ofJson(TF.constructType(targetType)); } - public static BodySubscriber ofJson(ObjectMapper objectMapper, Class targetType) { + public static BodySubscriber> ofJson(ObjectMapper objectMapper, Class targetType) { return ofJson(objectMapper, objectMapper.getTypeFactory().constructType(targetType)); } - public static BodySubscriber ofJson(ObjectMapper mapper, TypeReference targetType) { + public static BodySubscriber> ofJson(ObjectMapper mapper, TypeReference targetType) { return ofJson(mapper, mapper.getTypeFactory().constructType(targetType)); } - private static BodySubscriber ofJson(JavaType targetType) { + private static BodySubscriber> ofJson(JavaType targetType) { return ofJson(ObjectMapperHolder.INSTANCE, targetType); } - private static BodySubscriber ofJson(ObjectMapper objectMapper, JavaType targetType) { - return mapping(ofByteArray(), bytes -> { - try { - return objectMapper.readValue(bytes, targetType); + private static BodySubscriber> ofJson(ObjectMapper objectMapper, JavaType targetType) { + return mapping(ofInputStream(), in -> () -> { + try (var stream = in) { + return objectMapper.readValue(stream, targetType); } catch (IOException e) { throw new UncheckedIOException(e); } diff --git a/src/test/java/io/github/nstdio/http/ext/BodyHandlersTest.java b/src/test/java/io/github/nstdio/http/ext/BodyHandlersTest.java index a5f5989..cd76c47 100644 --- a/src/test/java/io/github/nstdio/http/ext/BodyHandlersTest.java +++ b/src/test/java/io/github/nstdio/http/ext/BodyHandlersTest.java @@ -16,16 +16,21 @@ package io.github.nstdio.http.ext; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; + import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.core.type.TypeReference; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; +import java.io.UncheckedIOException; import java.net.URI; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; import java.util.Map; +import java.util.function.Supplier; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatIOException; @@ -46,9 +51,11 @@ void shouldProperlyReadJson() { //when var body1 = client.sendAsync(request, BodyHandlers.ofJson(typeReference)) .thenApply(HttpResponse::body) - .join(); + .thenApply(Supplier::get) + .join(); var body2 = client.sendAsync(request, BodyHandlers.ofJson(Object.class)) .thenApply(HttpResponse::body) + .thenApply(Supplier::get) .join(); //then @@ -62,8 +69,8 @@ void shouldThrowUncheckedExceptionIfCannotRead() { var request = HttpRequest.newBuilder(URI.create("https://httpbin.org/html")).build(); //when - assertThatIOException() - .isThrownBy(() -> client.send(request, BodyHandlers.ofJson(Object.class))) + assertThatExceptionOfType(UncheckedIOException.class) + .isThrownBy(() -> client.send(request, BodyHandlers.ofJson(Object.class)).body().get()) .withRootCauseExactlyInstanceOf(JsonParseException.class); } }