From 445b76bbe819ba45ac2d887e355c42bba3d5395b Mon Sep 17 00:00:00 2001 From: Arjen Poutsma Date: Mon, 12 Nov 2018 14:11:58 +0100 Subject: [PATCH] Polishing --- .../codec/json/AbstractJackson2Encoder.java | 40 +++++++++++-------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/http/codec/json/AbstractJackson2Encoder.java b/spring-web/src/main/java/org/springframework/http/codec/json/AbstractJackson2Encoder.java index 16cc53b543..b5f09d2cf5 100644 --- a/spring-web/src/main/java/org/springframework/http/codec/json/AbstractJackson2Encoder.java +++ b/spring-web/src/main/java/org/springframework/http/codec/json/AbstractJackson2Encoder.java @@ -121,23 +121,31 @@ public abstract class AbstractJackson2Encoder extends Jackson2CodecSupport imple return Mono.from(inputStream).map(value -> encodeValue(value, mimeType, bufferFactory, elementType, hints, encoding)).flux(); } - - for (MediaType streamingMediaType : this.streamingMediaTypes) { - if (streamingMediaType.isCompatibleWith(mimeType)) { - byte[] separator = STREAM_SEPARATORS.getOrDefault(streamingMediaType, NEWLINE_SEPARATOR); - return Flux.from(inputStream).map(value -> { - DataBuffer buffer = encodeValue(value, mimeType, bufferFactory, elementType, hints, encoding); - if (separator != null) { - buffer.write(separator); - } - return buffer; - }); - } + else { + return this.streamingMediaTypes.stream() + .filter(mediaType -> mediaType.isCompatibleWith(mimeType)) + .findFirst() + .map(mediaType -> { + byte[] separator = + STREAM_SEPARATORS.getOrDefault(mediaType, NEWLINE_SEPARATOR); + return Flux.from(inputStream).map(value -> { + DataBuffer buffer = + encodeValue(value, mimeType, bufferFactory, elementType, hints, + encoding); + if (separator != null) { + buffer.write(separator); + } + return buffer; + }); + }) + .orElseGet(() -> { + ResolvableType listType = + ResolvableType.forClassWithGenerics(List.class, elementType); + return Flux.from(inputStream).collectList().map(list -> + encodeValue(list, mimeType, bufferFactory, listType, hints, + encoding)).flux(); + }); } - - ResolvableType listType = ResolvableType.forClassWithGenerics(List.class, elementType); - return Flux.from(inputStream).collectList().map(list -> - encodeValue(list, mimeType, bufferFactory, listType, hints, encoding)).flux(); } private DataBuffer encodeValue(Object value, @Nullable MimeType mimeType, DataBufferFactory bufferFactory,