I have a Controller like this and I want to submit a form with file uploading as well as some form data like label as shown below. Also, I want to do that using @RequestBody so I can use the @Valid annotation on the wrapper as more variables will be added.
public @ResponseBody WebResponse<Boolean> updateEUSettings( final Locale locale, @Validated @ModelAttribute final EUPSettingsWrapper endUserPortalSettingsWrapper) { }
And my wrapper is:
public class EUPSettingsWrapper { private String label; private MultipartFile logo; // getter , setters..etc... }
But I would like to convert it into a @RequestBody from ModelAttributes.
The way I'm trying is by having the file upload separated as request parameter like this:
public @ResponseBody WebResponse<Boolean> updateEUSettings( final Locale locale, @Validated @RequestBody final EUPSettingsWrapper endUserPortalSettingsWrapper, @RequestParam(value = "file1", required = true) final MultipartFile logo) { endUserPortalSettingsWrapper.setLogo(logo); // ... }
In my mock MVC, I am setting:
getMockMvc().perform(fileUpload(uri).file(logo) .accept(MediaType.APPLICATION_JSON) .content(JSONUtils.toJSON(wrapper)) .contentType(MediaType.MULTIPART_FORM_DATA)) .andExpect(status().isOk());
But I'm getting an error like this which says:
org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'multipart/form-data' not supported
Does anyone have a good idea of how Multipart file uploads can be used with @RequestBody? Anything I am doing wrong above?
To pass the Json and Multipart in the POST method we need to mention our content type in the consume part. And we need to pass the given parameter as User and Multipart file. Here, make sure we can pass only String + file not POJO + file. Then convert the String to Json using ObjectMapper in Service layer.
Spring Boot file uploader Create a Spring @Controller class; Add a method to the controller class which takes Spring's MultipartFile as an argument; Save the uploaded file to a directory on the server; and. Send a response code to the client indicating the Spring file upload was successful.
Simply put, the @RequestBody annotation maps the HttpRequest body to a transfer or domain object, enabling automatic deserialization of the inbound HttpRequest body onto a Java object. Spring automatically deserializes the JSON into a Java type, assuming an appropriate one is specified.
You can actually simplify your life here since all you are doing is submitting a form that contains some fields and file. You don't need @RequestBody for what you are trying to do. You can use regular Spring MVC features, so your controller method would look like:
@ResponseBody public WebResponse<Boolean> updateEUSettings( Locale locale, @Valid EUPSettingsWrapper endUserPortalSettingsWrapper, @RequestParam(value = "file1", required = true) MultipartFile logo ) { }
The client that submits the request to this controller will need to have a form with enctype="multipart/form-data"
.
In your Spring MVC test you would write something like this:
getMockMvc().perform(fileUpload(uri).file("file1", "some-content".getBytes()) .param("someEuSettingsProperty", "someValue") .param("someOtherEuSettingsProperty", "someOtherValue") .accept(MediaType.APPLICATION_JSON) .contentType(MediaType.MULTIPART_FORM_DATA)) .andExpect(status().isOk());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With