Here is the approach details:
- My web application will take the file.
- Once upload a file, it received at the server side in the form of MultipartFile.
- I will send it to another service as is. (as a MultipartFile).
I am passing the multipart file object to another rest api service. Here is the code.(HTTP method is POST, url is "/uploadDocument")
public ResponseEntity<String> executeMultipartRequest(String url, HttpMethod method, MultipartFile obj){
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
requestHeaders.add("Accept", MediaType.APPLICATION_JSON_VALUE);
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
body.add("file", obj.getResource());
HttpEntity<?> requestEntity = new HttpEntity<>(body, requestHeaders);
return restTemplate.exchange(url, method, requestEntity, String.class);
}
This is the service I am calling from above method call:
@PostMapping(value = "/uploadDocument", consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> uploadFile(@RequestParam("file") MultipartFile file, @RequestParam("userName") String userName) {
return null; // removed actual code.
}
But when I execute the method I am getting below error:
org.apache.catalina.core.StandardWrapperValve invoke SEVERE: Servlet.service() for servlet [dispatcher] in context with path [/attest] threw exception [Request processing failed; nested exception is org.springframework.web.client.HttpClientErrorException$BadRequest: 400 : [no body]] with root cause org.springframework.web.client.HttpClientErrorException$BadRequest: 400 : [no body]
Any idea what exactly the issue is?
I also want to know, is the approach correct?
Any change required?
Any suggestions?
Thanks,
Atul