Dhanu
Dhanu

Reputation: 121

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

Upvotes: 12

Views: 29240

Answers (2)

Max Telepchuk
Max Telepchuk

Reputation: 3

Updating gradle plugin to version

id "io.spring.dependency-management" version "1.0.9.RELEASE"

helped me

Upvotes: -3

Tharsan Sivakumar
Tharsan Sivakumar

Reputation: 6531

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

Upvotes: 8

Related Questions