spring-framework / org.springframework.web.bind.annotation / RequestPart

RequestPart

@Target([AnnotationTarget.VALUE_PARAMETER]) class RequestPart

Annotation that can be used to associate the part of a "multipart/form-data" request with a method argument.

Supported method argument types include MultipartFile in conjunction with Spring's MultipartResolver abstraction, javax.servlet.http.Part in conjunction with Servlet 3.0 multipart requests, or otherwise for any other method argument, the content of the part is passed through an HttpMessageConverter taking into consideration the 'Content-Type' header of the request part. This is analogous to what @RequestBody does to resolve an argument based on the content of a non-multipart regular request.

Note that @RequestParam annotation can also be used to associate the part of a "multipart/form-data" request with a method argument supporting the same method argument types. The main difference is that when the method argument is not a String, @RequestParam relies on type conversion via a registered Converter or PropertyEditor while @RequestPart relies on HttpMessageConverters taking into consideration the 'Content-Type' header of the request part. @RequestParam is likely to be used with name-value form fields while @RequestPart is likely to be used with parts containing more complex content (e.g. JSON, XML).

Author
Rossen Stoyanchev

Author
Arjen Poutsma

Author
Sam Brannen

Since
3.1

See Also
RequestParamorg.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter

Constructors

<init>

RequestPart(value: String, name: String, required: Boolean)

Annotation that can be used to associate the part of a "multipart/form-data" request with a method argument.

Supported method argument types include MultipartFile in conjunction with Spring's MultipartResolver abstraction, javax.servlet.http.Part in conjunction with Servlet 3.0 multipart requests, or otherwise for any other method argument, the content of the part is passed through an HttpMessageConverter taking into consideration the 'Content-Type' header of the request part. This is analogous to what @RequestBody does to resolve an argument based on the content of a non-multipart regular request.

Note that @RequestParam annotation can also be used to associate the part of a "multipart/form-data" request with a method argument supporting the same method argument types. The main difference is that when the method argument is not a String, @RequestParam relies on type conversion via a registered Converter or PropertyEditor while @RequestPart relies on HttpMessageConverters taking into consideration the 'Content-Type' header of the request part. @RequestParam is likely to be used with name-value form fields while @RequestPart is likely to be used with parts containing more complex content (e.g. JSON, XML).

Properties

name

val name: String

The name of the part in the "multipart/form-data" request to bind to.

required

val required: Boolean

Whether the part is required.

Defaults to true, leading to an exception being thrown if the part is missing in the request. Switch this to false if you prefer a null value if the part is not present in the request.

value

val value: String

Alias for #name.