Top Description Fields Constructors Methods
org.springframework.http.converter

public Class ResourceHttpMessageConverter

extends AbstractHttpMessageConverter<Resource>
Class Inheritance
Imports
java.io.FileNotFoundException, .IOException, .InputStream, .OutputStream, org.springframework.core.io.ByteArrayResource, .InputStreamResource, .Resource, org.springframework.http.HttpInputMessage, .HttpOutputMessage, .MediaType, .MediaTypeFactory, org.springframework.lang.Nullable, org.springframework.util.StreamUtils

Implementation of HttpMessageConverter that can read/write Resources and supports byte range requests.

By default, this converter can read all media types. The MediaTypeFactory is used to determine the Content-Type of written resources.

Authors
Arjen Poutsma, Juergen Hoeller, Kazuki Shimizu
Since
3.0.2

Field Summary

Modifier and TypeField and Description
private final boolean
Inherited from org.springframework.http.converter.AbstractHttpMessageConverter:
logger

Constructor Summary

AccessConstructor and Description
public
ResourceHttpMessageConverter()

Create a new instance of the ResourceHttpMessageConverter that supports read streaming, i.e. can convert an HttpInputMessage to InputStreamResource.

public
ResourceHttpMessageConverter(boolean
whether the converter should support read streaming, i.e. convert to InputStreamResource
supportsReadStreaming
)

Create a new instance of the ResourceHttpMessageConverter.

Method Summary

Modifier and TypeMethod and Description
public void
addDefaultHeaders(HttpOutputMessage message, Resource resource, MediaType contentType)

Adds the default headers for the given resource to the given message.

protected Long
getContentLength(Resource
the type to return the content length for
resource
,
MediaType contentType)

Overrides org.springframework.http.converter.AbstractHttpMessageConverter.getContentLength.

Returns the content length for the given type.
protected MediaType
getDefaultContentType(Resource
the type to return the content type for
resource
)

Overrides org.springframework.http.converter.AbstractHttpMessageConverter.getDefaultContentType.

Returns the default content type for the given type.
protected Resource
readInternal(Class<? extends Resource>
the type of object to return
clazz
,
HttpInputMessage
the HTTP input message to read from
inputMessage
)

Implements abstract org.springframework.http.converter.AbstractHttpMessageConverter.readInternal.

Abstract template method that reads the actual object.
protected boolean
supports(Class<?>
the class to test for support
clazz
)

Implements abstract org.springframework.http.converter.AbstractHttpMessageConverter.supports.

Indicates whether the given class is supported by this converter.
protected boolean
supportsRepeatableWrites(Resource
the object t
resource
)

Overrides org.springframework.http.converter.AbstractHttpMessageConverter.supportsRepeatableWrites.

Indicates whether this message converter can write the given object multiple times.
protected void
writeContent(Resource resource, HttpOutputMessage outputMessage)

protected void
writeInternal(Resource
the object to write to the output message
resource
,
HttpOutputMessage
the HTTP output message to write to
outputMessage
)

Implements abstract org.springframework.http.converter.AbstractHttpMessageConverter.writeInternal.

Abstract template method that writes the actual body.
Inherited from org.springframework.http.converter.AbstractHttpMessageConverter:
addDefaultHeaderscanReadcanReadcanWritecanWritegetDefaultCharsetgetSupportedMediaTypesreadsetDefaultCharsetsetSupportedMediaTypeswrite

Field Detail

supportsReadStreamingback to summary
private final boolean supportsReadStreaming

Constructor Detail

ResourceHttpMessageConverterback to summary
public ResourceHttpMessageConverter()

Create a new instance of the ResourceHttpMessageConverter that supports read streaming, i.e. can convert an HttpInputMessage to InputStreamResource.

ResourceHttpMessageConverterback to summary
public ResourceHttpMessageConverter(boolean supportsReadStreaming)

Create a new instance of the ResourceHttpMessageConverter.

Parameters
supportsReadStreaming:boolean

whether the converter should support read streaming, i.e. convert to InputStreamResource

Since
5.0

Method Detail

addDefaultHeadersback to summary
public void addDefaultHeaders(HttpOutputMessage message, Resource resource, MediaType contentType) throws IOException

Adds the default headers for the given resource to the given message.

Since
6.0
getContentLengthback to summary
protected Long getContentLength(Resource resource, MediaType contentType) throws IOException

Overrides org.springframework.http.converter.AbstractHttpMessageConverter.getContentLength.

Doc from org.springframework.http.converter.AbstractHttpMessageConverter.getContentLength.

Returns the content length for the given type.

By default, this returns null, meaning that the content length is unknown. Can be overridden in subclasses.

Parameters
resource:Resource

the type to return the content length for

Returns:Long

the content length, or null if not known

Annotations
@Override
getDefaultContentTypeback to summary
protected MediaType getDefaultContentType(Resource resource)

Overrides org.springframework.http.converter.AbstractHttpMessageConverter.getDefaultContentType.

Doc from org.springframework.http.converter.AbstractHttpMessageConverter.getDefaultContentType.

Returns the default content type for the given type. Called when write is invoked without a specified content type parameter.

By default, this returns the first element of the supportedMediaTypes property, if any. Can be overridden in subclasses.

Parameters
resource:Resource

the type to return the content type for

Returns:MediaType

the content type, or null if not known

Annotations
@Override
readInternalback to summary
protected Resource readInternal(Class<? extends Resource> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException

Implements abstract org.springframework.http.converter.AbstractHttpMessageConverter.readInternal.

Doc from org.springframework.http.converter.AbstractHttpMessageConverter.readInternal.

Abstract template method that reads the actual object. Invoked from read.

Parameters
clazz:Class<? extends Resource>

the type of object to return

inputMessage:HttpInputMessage

the HTTP input message to read from

Returns:Resource

the converted object

Annotations
@Override
Exceptions
IOException:
in case of I/O errors
HttpMessageNotReadableException:
in case of conversion errors
supportsback to summary
protected boolean supports(Class<?> clazz)

Implements abstract org.springframework.http.converter.AbstractHttpMessageConverter.supports.

Doc from org.springframework.http.converter.AbstractHttpMessageConverter.supports.

Indicates whether the given class is supported by this converter.

Parameters
clazz:Class<?>

the class to test for support

Returns:boolean

true if supported; false otherwise

Annotations
@Override
supportsRepeatableWritesback to summary
protected boolean supportsRepeatableWrites(Resource resource)

Overrides org.springframework.http.converter.AbstractHttpMessageConverter.supportsRepeatableWrites.

Doc from org.springframework.http.converter.AbstractHttpMessageConverter.supportsRepeatableWrites.

Indicates whether this message converter can write the given object multiple times.

Default implementation returns false.

Parameters
resource:Resource

the object t

Returns:boolean

true if t can be written repeatedly; false otherwise

Annotations
@Override
writeContentback to summary
protected void writeContent(Resource resource, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException
writeInternalback to summary
protected void writeInternal(Resource resource, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException

Implements abstract org.springframework.http.converter.AbstractHttpMessageConverter.writeInternal.

Doc from org.springframework.http.converter.AbstractHttpMessageConverter.writeInternal.

Abstract template method that writes the actual body. Invoked from write.

Parameters
resource:Resource

the object to write to the output message

outputMessage:HttpOutputMessage

the HTTP output message to write to

Annotations
@Override
Exceptions
IOException:
in case of I/O errors
HttpMessageNotWritableException:
in case of conversion errors