org.springframework.http.client

Class MultipartBodyBuilder

  • java.lang.Object
    • org.springframework.http.client.MultipartBodyBuilder


  • public final class MultipartBodyBuilder
    extends java.lang.Object
    A mutable builder for multipart form bodies. For example:
     MultipartBodyBuilder builder = new MultipartBodyBuilder();
     builder.part("form field", "form value");
    
     Resource image = new ClassPathResource("image.jpg");
     builder.part("image", image).header("Baz", "Qux");
    
     MultiValueMap<String, HttpEntity<?>> multipartBody = builder.build();
     // use multipartBody with RestTemplate or WebClient
     
    Since:
    5.0.2
    Author:
    Arjen Poutsma, Rossen Stoyanchev
    See Also:
    RFC 7578
    • Constructor Detail

      • MultipartBodyBuilder

        public MultipartBodyBuilder()
        Creates a new, empty instance of the MultipartBodyBuilder.
    • Method Detail

      • part

        public MultipartBodyBuilder.PartBuilder part(java.lang.String name,
                                                     java.lang.Object part)
        Add a part from an Object.
        Parameters:
        name - the name of the part to add
        part - the part data
        Returns:
        builder that allows for further customization of part headers
      • part

        public MultipartBodyBuilder.PartBuilder part(java.lang.String name,
                                                     java.lang.Object part,
                                                     @Nullable
                                                     MediaType contentType)
        Variant of part(String, Object) that also accepts a MediaType which is used to determine how to encode the part.
        Parameters:
        name - the name of the part to add
        part - the part data
        contentType - the media type for the part
        Returns:
        builder that allows for further customization of part headers
      • asyncPart

        public <T,P extends org.reactivestreams.Publisher<T>> MultipartBodyBuilder.PartBuilder asyncPart(java.lang.String name,
                                                                                                         P publisher,
                                                                                                         java.lang.Class<T> elementClass)
        Add an asynchronous part with Publisher-based content.
        Parameters:
        name - the name of the part to add
        publisher - the part contents
        elementClass - the type of elements contained in the publisher
        Returns:
        builder that allows for further customization of part headers
      • build

        public MultiValueMap<java.lang.String,HttpEntity<?>> build()
        Return a MultiValueMap with the configured parts.