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 SurveyPostHelper
s 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
Remove Parentheses, Dashes, and Spaces from Phone Number
Empty Check With String[] Array
How to Download Entire Folder Located on S3 Bucket
On Selenium Webdriver How to Get Text from Span Tag
How to Make Multiple Line String to Single Line String
Include Comma in a Field Value of CSV (Delimeter as Comma) in Java
How to Change Read-Only Status of Files in Intellij
How to Get Values of All Elements from Xml String in Java
Error Starting Applicationcontext in Spring Boot App
How to Skip @Postconstruct When Unit Testing
How to Sort Integer Digits in Ascending Order Without Strings or Arrays
How to Valid @Requestheader in Spring Boot
Update Objects in One List Based on Values from Second One Using Streams
How to Get the Autoincremented Id When I Insert a Record in a Table Via Jdbctemplate
How to Convert Date Which I Got from Firebase Server as Am Getting Error Date
Selenium Basic Authentication Via Url Headlesschrome ( on Linux Server)