The @RequestPart annotation binds one part of a multipart/form-data request to a controller method parameter. Unlike @RequestParam, it runs the part through an HttpMessageConverter chosen by that part's Content-Type, so a JSON part becomes a Java object (and can be validated), while a file part arrives as a MultipartFile or Part.
That makes it the right choice when one request carries a file together with structured data, for example an image plus a JSON description of it.
A multipart request is a list of parts, each with its own headers. @RequestPart("name") selects the part called name:
MultipartFile, Part (Servlet) or byte[], the part is bound as a file or raw content.application/json, so the part is deserialised into your class or record.Spring parses metadata with Jackson and applies Bean Validation because of @Valid. If validation fails, it raises MethodArgumentNotValidException, which Spring answers with 400. The record uses jakarta.validation constraints, which is the namespace in Spring Boot 3 and later.
The JSON part must carry Content-Type: application/json, or Spring will not find a converter and returns 415 Unsupported Media Type. With curl:
The ;type=application/json suffix is the part that most often goes missing. From browser JavaScript, wrap the JSON in a Blob:
Do not set the Content-Type header yourself with fetch, because the browser must add the multipart boundary.
| Aspect | @RequestPart | @RequestParam |
|---|---|---|
| Conversion of non-file parts | HttpMessageConverter by part content type | Conversion service, string to simple types |
| JSON part to an object | Yes | No |
@Valid on the part | Yes | Not on a JSON object |
| Typical use | File plus structured JSON | File plus simple text fields |
| Missing part exception | MissingServletRequestPartException | MissingServletRequestParameterException |
If your other fields are plain strings and numbers, @RequestParam is simpler. Reach for @RequestPart when the data has structure.
Make a part optional with required = false, or Optional:
Several files under the same part name bind to a List<MultipartFile>.
In a test, build the JSON part yourself and set its content type:
spring.servlet.multipart.max-file-size (1 MB default) and max-request-size (10 MB default). Larger requests fail with MaxUploadSizeExceededException.400 with MissingServletRequestPartException. A part with the wrong type gives 415.@RequestPart with FilePart and Mono<FilePart>; the Servlet types do not apply.Yes, that is its main use. Send the JSON as its own part with Content-Type: application/json, and the file as another part. Both bind in the same method.
The part had no application/json content type, often because it was sent as plain text. Set the type on the part, as in the curl and Blob examples above.
No. @RequestParam("file") MultipartFile works for files. @RequestPart also accepts files but is chosen mainly for converted, validated parts.
Yes. Add @Valid or @Validated to the parameter and Bean Validation annotations to the class.
@RequestPart binds an individual part of a multipart request, converting non-file parts with message converters based on their content type. It is the way to combine a file and validated JSON in one request. Remember to label the JSON part as application/json, configure upload limits, and use @RequestParam when your extra fields are simple strings.