Class ResourceHttpMessageWriter

java.lang.Object
org.springframework.http.codec.ResourceHttpMessageWriter
All Implemented Interfaces:
HttpMessageWriter<org.springframework.core.io.Resource>

public class ResourceHttpMessageWriter extends Object implements HttpMessageWriter<org.springframework.core.io.Resource>
HttpMessageWriter that can write a Resource.

Also an implementation of HttpMessageWriter with support for writing one or more ResourceRegion's based on the HTTP ranges specified in the request.

For reading to a Resource, use ResourceDecoder wrapped with DecoderHttpMessageReader.

Since:
5.0
Author:
Arjen Poutsma, Brian Clozel, Rossen Stoyanchev
See Also:
  • ResourceEncoder
  • ResourceRegionEncoder
  • HttpRange
  • Constructor Details

    • ResourceHttpMessageWriter

      public ResourceHttpMessageWriter()
    • ResourceHttpMessageWriter

      public ResourceHttpMessageWriter(int bufferSize)
  • Method Details

    • canWrite

      public boolean canWrite(org.springframework.core.ResolvableType elementType, @Nullable MediaType mediaType)
      Description copied from interface: HttpMessageWriter
      Whether the given object type is supported by this writer.
      Specified by:
      canWrite in interface HttpMessageWriter<org.springframework.core.io.Resource>
      Parameters:
      elementType - the type of object to check
      mediaType - the media type for the write (possibly null)
      Returns:
      true if writable, false otherwise
    • getWritableMediaTypes

      public List<MediaType> getWritableMediaTypes()
      Description copied from interface: HttpMessageWriter
      Return the list of media types supported by this Writer. The list may not apply to every possible target element type and calls to this method should typically be guarded via canWrite(elementType, null). The list may also exclude media types supported only for a specific element type. Alternatively, use HttpMessageWriter.getWritableMediaTypes(ResolvableType) for a more precise list.
      Specified by:
      getWritableMediaTypes in interface HttpMessageWriter<org.springframework.core.io.Resource>
      Returns:
      the general list of supported media types
    • write

      public reactor.core.publisher.Mono<Void> write(Publisher<? extends org.springframework.core.io.Resource> inputStream, org.springframework.core.ResolvableType elementType, @Nullable MediaType mediaType, ReactiveHttpOutputMessage message, Map<String,Object> hints)
      Description copied from interface: HttpMessageWriter
      Write a given stream of object to the output message.
      Specified by:
      write in interface HttpMessageWriter<org.springframework.core.io.Resource>
      Parameters:
      inputStream - the objects to write
      elementType - the type of objects in the stream which must have been previously checked via HttpMessageWriter.canWrite(ResolvableType, MediaType)
      mediaType - the content type for the write (possibly null to indicate that the default content type of the writer must be used)
      message - the message to write to
      hints - additional information about how to encode and write
      Returns:
      indicates completion or error
    • addHeaders

      @Deprecated(since="6.1", forRemoval=true) public void addHeaders(ReactiveHttpOutputMessage message, org.springframework.core.io.Resource resource, @Nullable MediaType contentType, Map<String,Object> hints)
      Deprecated, for removal: This API element is subject to removal in a future version.
      Adds the default headers for the given resource to the given message.
      Since:
      6.0
    • addDefaultHeaders

      public reactor.core.publisher.Mono<Void> addDefaultHeaders(ReactiveHttpOutputMessage message, org.springframework.core.io.Resource resource, @Nullable MediaType contentType, Map<String,Object> hints)
      Adds the default headers for the given resource to the given message.
      Since:
      6.1
    • write

      public reactor.core.publisher.Mono<Void> write(Publisher<? extends org.springframework.core.io.Resource> inputStream, @Nullable org.springframework.core.ResolvableType actualType, org.springframework.core.ResolvableType elementType, @Nullable MediaType mediaType, ServerHttpRequest request, ServerHttpResponse response, Map<String,Object> hints)
      Description copied from interface: HttpMessageWriter
      Server-side only alternative to HttpMessageWriter.write(Publisher, ResolvableType, MediaType, ReactiveHttpOutputMessage, Map) with additional context available.
      Specified by:
      write in interface HttpMessageWriter<org.springframework.core.io.Resource>
      actualType - the actual return type of the method that returned the value; for annotated controllers, the MethodParameter can be accessed via ResolvableType.getSource().
      elementType - the type of Objects in the input stream
      mediaType - the content type to use (possibly null indicating the default content type of the writer should be used)
      request - the current request
      response - the current response
      Returns:
      a Mono that indicates completion of writing or error