diff --git a/spring-web-reactive/src/main/java/org/springframework/http/converter/reactive/CodecHttpMessageConverter.java b/spring-web-reactive/src/main/java/org/springframework/http/converter/reactive/CodecHttpMessageConverter.java index 75c56cdc7f..1bb4400df4 100644 --- a/spring-web-reactive/src/main/java/org/springframework/http/converter/reactive/CodecHttpMessageConverter.java +++ b/spring-web-reactive/src/main/java/org/springframework/http/converter/reactive/CodecHttpMessageConverter.java @@ -40,6 +40,7 @@ import org.springframework.http.support.MediaTypeUtils; * * @author Arjen Poutsma * @author Sebastien Deleuze + * @author Rossen Stoyanchev */ public class CodecHttpMessageConverter implements HttpMessageConverter { @@ -51,6 +52,7 @@ public class CodecHttpMessageConverter implements HttpMessageConverter { private final List writableMediaTypes; + /** * Create a {@code CodecHttpMessageConverter} with the given {@link Encoder}. When * using this constructor, all read-related methods will in {@code false} or an @@ -89,6 +91,7 @@ public class CodecHttpMessageConverter implements HttpMessageConverter { Collections.emptyList(); } + @Override public boolean canRead(ResolvableType type, MediaType mediaType) { return this.decoder != null && this.decoder.canDecode(type, mediaType); @@ -109,6 +112,7 @@ public class CodecHttpMessageConverter implements HttpMessageConverter { return this.writableMediaTypes; } + @Override public Flux read(ResolvableType type, ReactiveHttpInputMessage inputMessage) { if (this.decoder == null) { @@ -118,16 +122,13 @@ public class CodecHttpMessageConverter implements HttpMessageConverter { if (contentType == null) { contentType = MediaType.APPLICATION_OCTET_STREAM; } - - Flux body = inputMessage.getBody(); - - return this.decoder.decode(body, type, contentType); + return this.decoder.decode(inputMessage.getBody(), type, contentType); } @Override public Mono write(Publisher inputStream, ResolvableType type, - MediaType contentType, - ReactiveHttpOutputMessage outputMessage) { + MediaType contentType, ReactiveHttpOutputMessage outputMessage) { + if (this.encoder == null) { return Mono.error(new IllegalStateException("No decoder set")); } @@ -139,22 +140,23 @@ public class CodecHttpMessageConverter implements HttpMessageConverter { } headers.setContentType(contentTypeToUse); } - DataBufferFactory dataBufferFactory = outputMessage.bufferFactory(); - Flux body = - this.encoder.encode(inputStream, dataBufferFactory, type, contentType); + DataBufferFactory bufferFactory = outputMessage.bufferFactory(); + Flux body = this.encoder.encode(inputStream, bufferFactory, type, contentType); return outputMessage.writeWith(body); } /** - * Returns the default content type for the given type. Called when {@link #write} - * is invoked without a specified content type parameter. - *

By default, this returns a {@link MediaType} created using the first element of - * the encoder {@link Encoder#getEncodableMimeTypes() encodableMimeTypes} property, if any. - * Can be overridden in subclasses. - * @param type the type to return the content type for - * @return the content type, or {@code null} if not known + * Return the default content type for the given {@code ResolvableType}. + * Used when {@link #write} is called without a concrete content type. + * + *

By default returns the first of {@link Encoder#getEncodableMimeTypes() + * encodableMimeTypes}, if any. + * + * @param elementType the type of element for encoding + * @return the content type, or {@code null} */ - protected MediaType getDefaultContentType(ResolvableType type) { + protected MediaType getDefaultContentType(ResolvableType elementType) { return (!this.writableMediaTypes.isEmpty() ? this.writableMediaTypes.get(0) : null); } + }