How can I receive multipart/form-data in Spring MVC Controller?
I hope I can help you with that. Try it.
@RequestMapping(value = "/putRequest",
produces = { "application/json" },
consumes = { "multipart/form-data" },
method = RequestMethod.PUT)
public ResponseEntity<SuccessDto> requestPut(@Valid @RequestParam(value = "commit", required = false, defaultValue="false") Boolean commit, @Valid @RequestPart("file") MultipartFile file) {
return new ResponseEntity<>(HttpStatus.OK);
}
Related videos on Youtube
JetBrains
Updated on June 04, 2022Comments
-
JetBrains almost 2 years
I need to implement a REST-Endpoint, that receives multipart/form-data
I use:- Spring Boot
- Kotlin
- Spring MVC
A multipart form submit with the following parts:
deployment-name ----- text/plain
enable-duplicate-filtering ----- text/plain
deploy-changed-only ----- text/plain
deployment-source ----- text/plain
tenant-id ----- text/plain
* ----- application/octet-streamThe Rest Controller looks so:
@PostMapping("/data/deployment/create") fun uploadDmn( @RequestParam("deployment-name") deploymentName: String, @RequestParam("enable-duplicate-filtering") enableDuplicateFiltering: String?, @RequestParam("deploy-changed-only") deployChangedOnly: String, @RequestParam("deployment-source") deploymentSource: String, @RequestParam("tenant-id") tenantId: String, @RequestParam("data") data: MultipartFile ) { println(deploymentName) println(deployChangedOnly) println(deploymentSource) println(tenantId) println(data.toString()) }
For all params that works, but for the last one that doesn't work. I've tried to give a name "data", "*", "file" that doesn't work.
Required request part 'data' is not present
The Controller doesn't see that file.
I've tried too to use Retrofit:
@PostMapping("/data/deployment/create") @Multipart fun uploadDmn( @Part("data") data: MultipartFile ) { println(data.toString()) }
But that doesn't work too:
Parameter specified as non-null is null
How can I work with that content type? multipart/form-data
Example of Request:
--28319d96a8c54b529aa9159ad75edef9 Content-Disposition: form-data; name="deployment-name" aName --28319d96a8c54b529aa9159ad75edef9 Content-Disposition: form-data; name="enable-duplicate-filtering" true --28319d96a8c54b529aa9159ad75edef9 Content-Disposition: form-data; name="deployment-source" process application --28319d96a8c54b529aa9159ad75edef9 Content-Disposition: form-data; name="data"; filename="test.bpmn" <?xml version="1.0" encoding="UTF-8"?> <bpmn2:definitions ...> <!-- BPMN 2.0 XML omitted --> </bpmn2:definitions> --28319d96a8c54b529aa9159ad75edef9--
Can anyone help please?
-
LHCHIN over 4 yearsHave you set the
spring.servlet.multipart.enabled
to betrue
? -
JetBrains over 4 years@LHCHIN no, but I've tried to do that in application.properties. Still the same problem:
Parameter specified as non-null is null
-
Ludovico Sidari over 4 yearsYou should add the multipart filter before the crsf one. stackoverflow.com/a/35405063/11951081
- Spring Boot
-
JetBrains over 4 yearsI don't know why you have annotated 90% of the code with a Swagger, but the main part is here the annotation
@RequestPart
. Unfortunately that doesn't work too. The same problem "Required request part 'data' is not present". I think the problem is in the App that sends this request. The App is "Camunda Modeler". -
pb11pb over 4 years"I think the problem is in the App that sends this request." I agree. The code above expects a FormData object in the body with the key "file". Of course I do not know in which form the client sends his request. Maybe the key has to be adjusted.
-
pb11pb over 4 yearsTry to change @RequestPart("file") with @RequestPart("data").