Can We Use Multipart and @Requestbody Together in Spring

How to send MultiPart and RequestBody together In Spring-boot 2.x

Define body as RequestPart too.

@PostMapping(RequestUrl.VIDEO_ADD_VIDEO)
public ResponseEntity<BulkWriteResult> addNewVideoToCategory(
@RequestPart("body") Video video
@RequestPart("file") MultipartFile file) {
// some logics

}

How to send @Requestbody and @Requestpart together in spring

I solved the issue by implementing multipartresolver in configuration.

@Bean
public MultipartResolver multipartResolver() {
return new StandardServletMultipartResolver();
}

Spring Multipart File with @RequestBody

I found out, that this method signature could do the job:

@ResponseBody
public Survey createSurvey(@RequestPart(required=true) SurveyPostHelper helper, @RequestPart(value="file", required = true) final MultipartFile[] images)

Important in my case was to set the MimeType in the client app. The files MimeType should be image/jpg and the SurveyPostHelpers to application/json to allow Spring to parse the json and bind it to my Object.

Unable to post multipart data and requestbody form data json request

The first thing is the MultipartFile cannot be inside the body of the Response from your client. The second thing is the data response from your browser should only be only one way.

My suggestion is you can put all the data you want to save to the database inside a form submission. And use @ModelAttribute Product product, @RequestParam MultipartFile[] files .
Also, your method can be void since you save data, you don't need to return anything.
Note: @ModelAttribute can be omitted.

MultipartFile inside Custom Request / Response Objects

Yes, you can. Spring has a class that handles Multipart files - org.springframework.web.multipart.MultipartFile. You can use it to send or receive multipart files. But the request body needs to be form-data. You can not send file over x-www-form-urlencoded.

Just use MultipartFile as an attribure in your custom request and you sould be good to go.

@Component
public class CustomRequest {
...
private Long checkSum;
private MultipartFile file;
...
// make sure you have the getters and setters right
// also the filed names are spelled exactly the same as they are in the form
// for example myFile and my_file are different

public MultiValueMap<String, String> getRequestBody() {
MultiValueMap<String, String> requestBody = new LinkedMultiValueMap<String, String>();
requestBody.add("checkSum", this.getCheckSum());
requestBody.add("fileContents", this.getFileContents());
// add other fileds if you need

return requestBody;
}
}

In the controller,

HttpHeaders headers = new HttpHeaders();  
headers.setContentType("mutipart/form-data");

HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(customRequest.getRequestBody(), headers);

ResponseEntity<UploadResponse> responseEntity = restTemplate.postForEntity(uploadUrl, requestEntity, UploadResponse.class);

What happened was that you were sending the request body inside the MultiValueMap. You have to send the form data inside the MultiValueMap. And make sure your response has the getters and setters. Spring needs getter setter to serialize the object.

Documentation is here

Spring Controller @RequestBody with file upload is it possible?

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());


Related Topics



Leave a reply



Submit