I am trying to do and https post to an site using spring rest template(It accepts post, but doesn't accept JSON) with Spring MultiPart file upload.
Following error was received, when doing so,
org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: No serializer found for class java.io.FileDescriptor and no properties discovered to create BeanSerializer.
MultiValueMap<String, Object> formData = new LinkedMultiValueMap<String, Object>();
formData.add("NUMBER", "ABC");
formData.add("ID", "123");
formData.add("FILE",file); // this is spring multipart file
HttpHeaders headers = new HttpHeaders();
headers.set("Content-Type", "multipart/form-data");
headers.set("Accept", "text/plain");
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<MultiValueMap<String, Object>>(formData, headers);
RestTemplate restTemplate = getRestTemplate();
String result = restTemplate.postForObject(uploadUri, requestEntity, String.class);
I too faced the same problem and I was able to sort this out when I was using ByteArrayResource instead of FileSystem resource. I copied the byte contents of the multipart file into the httprequest entity using ByteArrayResource.
Iterator<String> itr = request.getFileNames();
MultipartFile file = request.getFile(itr.next());
//Set the headers
................
formData .add("files", new ByteArrayResource(file.getBytes()));
You may refer this link to get more info
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