提交 59d3721a 编写于 作者: S Sebastien Deleuze

Support default content type in CodecHttpMessageConverter

上级 52325a21
......@@ -39,6 +39,7 @@ import org.springframework.http.support.MediaTypeUtils;
* {@link Encoder} and {@link Decoder}.
*
* @author Arjen Poutsma
* @author Sebastien Deleuze
*/
public class CodecHttpMessageConverter<T> implements HttpMessageConverter<T> {
......@@ -132,11 +133,28 @@ public class CodecHttpMessageConverter<T> implements HttpMessageConverter<T> {
}
HttpHeaders headers = outputMessage.getHeaders();
if (headers.getContentType() == null) {
headers.setContentType(contentType);
MediaType contentTypeToUse = contentType;
if (contentType == null || contentType.isWildcardType() || contentType.isWildcardSubtype()) {
contentTypeToUse = getDefaultContentType(type);
}
headers.setContentType(contentTypeToUse);
}
DataBufferFactory dataBufferFactory = outputMessage.bufferFactory();
Flux<DataBuffer> body =
this.encoder.encode(inputStream, dataBufferFactory, 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.
* <p>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
*/
protected MediaType getDefaultContentType(ResolvableType type) {
return (!this.writableMediaTypes.isEmpty() ? this.writableMediaTypes.get(0) : null);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册