From e481ab4b0f757c50eac17d3087e099d09dbe00c9 Mon Sep 17 00:00:00 2001 From: Stuart Douglas Date: Tue, 15 Dec 2020 09:06:56 +1100 Subject: [PATCH] Fixes around default JSON handling This makes sure that the correct writer is selected for RESTEasy, and that the correct content type is set for RESTEasy Reactive. --- .../common/runtime/jackson/QuarkusJacksonSerializer.java | 1 + .../resteasy/common/runtime/jsonb/QuarkusJsonbSerializer.java | 3 ++- .../jackson/runtime/serialisers/JacksonMessageBodyWriter.java | 3 +++ .../jsonb/runtime/serialisers/JsonbMessageBodyWriter.java | 3 +++ 4 files changed, 9 insertions(+), 1 deletion(-) diff --git a/extensions/resteasy-common/runtime/src/main/java/io/quarkus/resteasy/common/runtime/jackson/QuarkusJacksonSerializer.java b/extensions/resteasy-common/runtime/src/main/java/io/quarkus/resteasy/common/runtime/jackson/QuarkusJacksonSerializer.java index d4fc18db96f62..ec4b48899850b 100644 --- a/extensions/resteasy-common/runtime/src/main/java/io/quarkus/resteasy/common/runtime/jackson/QuarkusJacksonSerializer.java +++ b/extensions/resteasy-common/runtime/src/main/java/io/quarkus/resteasy/common/runtime/jackson/QuarkusJacksonSerializer.java @@ -59,6 +59,7 @@ public boolean isWriteable(Class type, Type genericType, Annotation[] annotat return false; } return mediaType.equals(MediaType.APPLICATION_OCTET_STREAM_TYPE) + || mediaType.isWildcardType() || super.isWriteable(type, genericType, annotations, mediaType); } diff --git a/extensions/resteasy-common/runtime/src/main/java/io/quarkus/resteasy/common/runtime/jsonb/QuarkusJsonbSerializer.java b/extensions/resteasy-common/runtime/src/main/java/io/quarkus/resteasy/common/runtime/jsonb/QuarkusJsonbSerializer.java index 47277ea80803a..c554e886eb59c 100644 --- a/extensions/resteasy-common/runtime/src/main/java/io/quarkus/resteasy/common/runtime/jsonb/QuarkusJsonbSerializer.java +++ b/extensions/resteasy-common/runtime/src/main/java/io/quarkus/resteasy/common/runtime/jsonb/QuarkusJsonbSerializer.java @@ -58,7 +58,8 @@ public boolean isWriteable(Class type, Type genericType, Annotation[] annotat if (BUILTIN_DEFAULTS.contains(type)) { return false; } - return isSupportedMediaType(mediaType) || mediaType.equals(MediaType.APPLICATION_OCTET_STREAM_TYPE); + return isSupportedMediaType(mediaType) || mediaType.equals(MediaType.APPLICATION_OCTET_STREAM_TYPE) + || mediaType.isWildcardType(); } @Override diff --git a/extensions/resteasy-reactive/quarkus-resteasy-reactive-jackson/runtime/src/main/java/io/quarkus/resteasy/reactive/jackson/runtime/serialisers/JacksonMessageBodyWriter.java b/extensions/resteasy-reactive/quarkus-resteasy-reactive-jackson/runtime/src/main/java/io/quarkus/resteasy/reactive/jackson/runtime/serialisers/JacksonMessageBodyWriter.java index af6719505b7af..2fc275a779dbb 100644 --- a/extensions/resteasy-reactive/quarkus-resteasy-reactive-jackson/runtime/src/main/java/io/quarkus/resteasy/reactive/jackson/runtime/serialisers/JacksonMessageBodyWriter.java +++ b/extensions/resteasy-reactive/quarkus-resteasy-reactive-jackson/runtime/src/main/java/io/quarkus/resteasy/reactive/jackson/runtime/serialisers/JacksonMessageBodyWriter.java @@ -8,6 +8,7 @@ import javax.inject.Inject; import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.HttpHeaders; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MultivaluedMap; @@ -46,6 +47,7 @@ public boolean isWriteable(Class type, Type genericType, Annotation[] annotat @Override public void writeTo(Object o, Class type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException { + httpHeaders.putSingle(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON); if (o instanceof String) { // YUK: done in order to avoid adding extra quotes... entityStream.write(((String) o).getBytes()); } else { @@ -60,6 +62,7 @@ public boolean isWriteable(Class type, ResteasyReactiveResourceInfo target, M @Override public void writeResponse(Object o, ServerRequestContext context) throws WebApplicationException, IOException { + context.serverResponse().setResponseHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON); OutputStream stream = context.getOrCreateOutputStream(); if (o instanceof String) { // YUK: done in order to avoid adding extra quotes... stream.write(((String) o).getBytes()); diff --git a/extensions/resteasy-reactive/quarkus-resteasy-reactive-jsonb/runtime/src/main/java/io/quarkus/resteasy/reactive/jsonb/runtime/serialisers/JsonbMessageBodyWriter.java b/extensions/resteasy-reactive/quarkus-resteasy-reactive-jsonb/runtime/src/main/java/io/quarkus/resteasy/reactive/jsonb/runtime/serialisers/JsonbMessageBodyWriter.java index eaa08beacd003..e814be620dd6a 100644 --- a/extensions/resteasy-reactive/quarkus-resteasy-reactive-jsonb/runtime/src/main/java/io/quarkus/resteasy/reactive/jsonb/runtime/serialisers/JsonbMessageBodyWriter.java +++ b/extensions/resteasy-reactive/quarkus-resteasy-reactive-jsonb/runtime/src/main/java/io/quarkus/resteasy/reactive/jsonb/runtime/serialisers/JsonbMessageBodyWriter.java @@ -8,6 +8,7 @@ import javax.inject.Inject; import javax.json.bind.Jsonb; import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.HttpHeaders; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MultivaluedMap; @@ -32,6 +33,7 @@ public boolean isWriteable(Class type, Type genericType, Annotation[] annotat @Override public void writeTo(Object o, Class type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException { + httpHeaders.putSingle(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON); if (o instanceof String) { // YUK: done in order to avoid adding extra quotes... entityStream.write(((String) o).getBytes()); } else { @@ -46,6 +48,7 @@ public boolean isWriteable(Class type, ResteasyReactiveResourceInfo target, M @Override public void writeResponse(Object o, ServerRequestContext context) throws WebApplicationException, IOException { + context.serverResponse().setResponseHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON); OutputStream originalStream = context.getOrCreateOutputStream(); OutputStream stream = new NoopCloseAndFlushOutputStream(originalStream); if (o instanceof String) { // YUK: done in order to avoid adding extra quotes...