org.springframework.web.reactive.function

Class BodyInserters

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


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

      • BodyInserters

        public BodyInserters()
    • Method Detail

      • fromObject

        public static <T> BodyInserter<T,ReactiveHttpOutputMessage> fromObject(T body)
        Inserter to write the given object.

        Alternatively, consider using the syncBody(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
      • fromPublisher

        public static <T,P extends org.reactivestreams.Publisher<T>> BodyInserter<P,ReactiveHttpOutputMessage> fromPublisher(P publisher,
                                                                                                                             java.lang.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 type of elements in the publisher
        Returns:
        the inserter to write a Publisher
      • fromPublisher

        public static <T,P extends org.reactivestreams.Publisher<T>> BodyInserter<P,ReactiveHttpOutputMessage> fromPublisher(P publisher,
                                                                                                                             ParameterizedTypeReference<T> typeRef)
        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
        typeRef - the type of elements contained in the publisher
        Returns:
        the inserter to write a Publisher
      • fromResource

        public static <T extends ResourceBodyInserter<T,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
      • fromFormData

        public static BodyInserters.FormInserter<java.lang.String> fromFormData(MultiValueMap<java.lang.String,java.lang.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 syncBody(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
      • fromMultipartData

        public static BodyInserters.MultipartInserter fromMultipartData(java.lang.String name,
                                                                        java.lang.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 syncBody(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 org.reactivestreams.Publisher<T>> BodyInserters.MultipartInserter fromMultipartAsyncData(java.lang.String name,
                                                                                                                            P publisher,
                                                                                                                            java.lang.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 syncBody(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 org.reactivestreams.Publisher<T>> BodyInserters.MultipartInserter fromMultipartAsyncData(java.lang.String name,
                                                                                                                            P publisher,
                                                                                                                            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 syncBody(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