Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Could not write JSON: No serializer found for class java.io.FileDescriptor and no properties discovered to create BeanSerializer

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);
like image 896
Dhanu Avatar asked Nov 16 '15 16:11

Dhanu


1 Answers

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

like image 72
Tharsan Sivakumar Avatar answered Nov 02 '22 05:11

Tharsan Sivakumar