Class BodyInserters

java.lang.Object
org.springframework.web.reactive.function.BodyInserters

public abstract class BodyInserters extends Object
Static factory methods for BodyInserter implementations.
Since:
5.0
Author:
Arjen Poutsma, Rossen Stoyanchev, Sebastien Deleuze
  • Constructor Details

    • BodyInserters

      public BodyInserters()
  • Method Details

    • empty

      public static <T> BodyInserter<T,org.springframework.http.ReactiveHttpOutputMessage> empty()
      Inserter that does not write.
      Returns:
      the inserter
    • fromValue

      public static <T> BodyInserter<T,org.springframework.http.ReactiveHttpOutputMessage> fromValue(T body)
      Inserter to write the given value.

      Alternatively, consider using the bodyValue(Object) shortcuts on WebClient and ServerResponse.

      Type Parameters:
      T - the type of the body
      Parameters:
      body - the value to write
      Returns:
      the inserter to write a single value
      Throws:
      IllegalArgumentException - if body is a Publisher or an instance of a type supported by ReactiveAdapterRegistry.getSharedInstance(), for which fromPublisher(Publisher, Class) or fromProducer(Object, Class) should be used.
      See Also:
    • fromObject

      @Deprecated public static <T> BodyInserter<T,org.springframework.http.ReactiveHttpOutputMessage> fromObject(T body)
      Deprecated.
      As of Spring Framework 5.2, in favor of fromValue(Object)
      Inserter to write the given object.

      Alternatively, consider using the bodyValue(Object) shortcuts on WebClient and ServerResponse.

      Type Parameters:
      T - the type of the body
      Parameters:
      body - the body to write to the response
      Returns:
      the inserter to write a single object
      Throws:
      IllegalArgumentException - if body is a Publisher or an instance of a type supported by ReactiveAdapterRegistry.getSharedInstance(), for which fromPublisher(Publisher, Class) or fromProducer(Object, Class) should be used.
      See Also:
    • fromProducer

      public static <T> BodyInserter<T,org.springframework.http.ReactiveHttpOutputMessage> fromProducer(T producer, Class<?> elementClass)
      Inserter to write the given producer of value(s) which must be a Publisher or another producer adaptable to a Publisher via ReactiveAdapterRegistry.

      Alternatively, consider using the body shortcuts on WebClient and ServerResponse.

      Type Parameters:
      T - the type of the body
      Parameters:
      producer - the source of body value(s).
      elementClass - the class of values to be produced
      Returns:
      the inserter to write a producer
      Since:
      5.2
    • fromProducer

      public static <T> BodyInserter<T,org.springframework.http.ReactiveHttpOutputMessage> fromProducer(T producer, org.springframework.core.ParameterizedTypeReference<?> elementTypeRef)
      Inserter to write the given producer of value(s) which must be a Publisher or another producer adaptable to a Publisher via ReactiveAdapterRegistry.

      Alternatively, consider using the body shortcuts on WebClient and ServerResponse.

      Type Parameters:
      T - the type of the body
      Parameters:
      producer - the source of body value(s).
      elementTypeRef - the type of values to be produced
      Returns:
      the inserter to write a producer
      Since:
      5.2
    • fromPublisher

      public static <T, P extends Publisher<T>> BodyInserter<P,org.springframework.http.ReactiveHttpOutputMessage> fromPublisher(P publisher, Class<T> elementClass)
      Inserter to write the given Publisher.

      Alternatively, consider using the body shortcuts on WebClient and ServerResponse.

      Type Parameters:
      T - the type of the elements contained in the publisher
      P - the Publisher type
      Parameters:
      publisher - the publisher to write with
      elementClass - the class of elements in the publisher
      Returns:
      the inserter to write a Publisher
    • fromPublisher

      public static <T, P extends Publisher<T>> BodyInserter<P,org.springframework.http.ReactiveHttpOutputMessage> fromPublisher(P publisher, org.springframework.core.ParameterizedTypeReference<T> elementTypeRef)
      Inserter to write the given Publisher.

      Alternatively, consider using the body shortcuts on WebClient and ServerResponse.

      Type Parameters:
      T - the type of the elements contained in the publisher
      P - the Publisher type
      Parameters:
      publisher - the publisher to write with
      elementTypeRef - the type of elements contained in the publisher
      Returns:
      the inserter to write a Publisher
    • fromResource

      public static <T extends org.springframework.core.io.Resource> BodyInserter<T,org.springframework.http.ReactiveHttpOutputMessage> fromResource(T resource)
      Inserter to write the given Resource.

      If the resource can be resolved to a file, it will be copied using zero-copy.

      Type Parameters:
      T - the type of the Resource
      Parameters:
      resource - the resource to write to the output message
      Returns:
      the inserter to write a Publisher
    • fromServerSentEvents

      public static <T, S extends Publisher<org.springframework.http.codec.ServerSentEvent<T>>> BodyInserter<S,org.springframework.http.server.reactive.ServerHttpResponse> fromServerSentEvents(S eventsPublisher)
      Inserter to write the given ServerSentEvent publisher.

      Alternatively, you can provide event data objects via fromPublisher(Publisher, Class) or fromProducer(Object, Class), and set the "Content-Type" to text/event-stream.

      Type Parameters:
      T - the type of the data elements in the ServerSentEvent
      Parameters:
      eventsPublisher - the ServerSentEvent publisher to write to the response body
      Returns:
      the inserter to write a ServerSentEvent publisher
      See Also:
    • fromFormData

      public static BodyInserters.FormInserter<String> fromFormData(org.springframework.util.MultiValueMap<String,String> formData)
      Return a BodyInserters.FormInserter to write the given MultiValueMap as URL-encoded form data. The returned inserter allows for additional entries to be added via BodyInserters.FormInserter.with(String, Object).

      Note that you can also use the bodyValue(Object) method in the request builders of both the WebClient and WebTestClient. In that case the setting of the request content type is also not required, just be sure the map contains String values only or otherwise it would be interpreted as a multipart request.

      Parameters:
      formData - the form data to write to the output message
      Returns:
      the inserter that allows adding more form data
    • fromFormData

      public static BodyInserters.FormInserter<String> fromFormData(String name, String value)
      Return a BodyInserters.FormInserter to write the given key-value pair as URL-encoded form data. The returned inserter allows for additional entries to be added via BodyInserters.FormInserter.with(String, Object).
      Parameters:
      name - the key to add to the form
      value - the value to add to the form
      Returns:
      the inserter that allows adding more form data
    • fromMultipartData

      public static BodyInserters.MultipartInserter fromMultipartData(org.springframework.util.MultiValueMap<String,?> multipartData)
      Return a BodyInserters.MultipartInserter to write the given MultiValueMap as multipart data. Values in the map can be an Object or an HttpEntity.

      Note that you can also build the multipart data externally with MultipartBodyBuilder, and pass the resulting map directly to the bodyValue(Object) shortcut method in WebClient.

      Parameters:
      multipartData - the form data to write to the output message
      Returns:
      the inserter that allows adding more parts
      See Also:
      • MultipartBodyBuilder
    • fromMultipartData

      public static BodyInserters.MultipartInserter fromMultipartData(String name, Object value)
      Return a BodyInserters.MultipartInserter to write the given parts, as multipart data. Values in the map can be an Object or an HttpEntity.

      Note that you can also build the multipart data externally with MultipartBodyBuilder, and pass the resulting map directly to the bodyValue(Object) shortcut method in WebClient.

      Parameters:
      name - the part name
      value - the part value, an Object or HttpEntity
      Returns:
      the inserter that allows adding more parts
    • fromMultipartAsyncData

      public static <T, P extends Publisher<T>> BodyInserters.MultipartInserter fromMultipartAsyncData(String name, P publisher, Class<T> elementClass)
      Return a BodyInserters.MultipartInserter to write the given asynchronous parts, as multipart data.

      Note that you can also build the multipart data externally with MultipartBodyBuilder, and pass the resulting map directly to the bodyValue(Object) shortcut method in WebClient.

      Parameters:
      name - the part name
      publisher - the publisher that forms the part value
      elementClass - the class contained in the publisher
      Returns:
      the inserter that allows adding more parts
    • fromMultipartAsyncData

      public static <T, P extends Publisher<T>> BodyInserters.MultipartInserter fromMultipartAsyncData(String name, P publisher, org.springframework.core.ParameterizedTypeReference<T> typeReference)
      Variant of fromMultipartAsyncData(String, Publisher, Class) that accepts a ParameterizedTypeReference for the element type, which allows specifying generic type information.

      Note that you can also build the multipart data externally with MultipartBodyBuilder, and pass the resulting map directly to the bodyValue(Object) shortcut method in WebClient.

      Parameters:
      name - the part name
      publisher - the publisher that forms the part value
      typeReference - the type contained in the publisher
      Returns:
      the inserter that allows adding more parts
    • fromDataBuffers

      public static <T extends Publisher<org.springframework.core.io.buffer.DataBuffer>> BodyInserter<T,org.springframework.http.ReactiveHttpOutputMessage> fromDataBuffers(T publisher)
      Inserter to write the given Publisher<DataBuffer> to the body.
      Type Parameters:
      T - the type of the publisher
      Parameters:
      publisher - the data buffer publisher to write
      Returns:
      the inserter to write directly to the body
      See Also:
      • ReactiveHttpOutputMessage.writeWith(Publisher)