0

I am trying to consume a rest api to upload a Multipart file. I have set MultipartFile in UploadFIleObject and I am sending the same to rest api. However when trying to invoke a rest API as below, I am getting error No serializer found for class java.io.ByteArrayInputStream

public class UploadFileObject implements Serializable{

  MultipartFile file;

//getters and setters
}

public void uploadFile() {

Path path = Paths.get("C:/Accounts.txt");
byte[] content = null;

try{
    content =  Files.readAllBytes(path);   
} catch(final IOException e){
}

MultipartFile file = new MockMultipartFile("ABC.txt","ABC.txt","text/plain", content);
    
UploadFileObject obj = new UploadFileObject();
obj.setFile(file)

HttpHeaders headers = new HttpHeaders();
 headers.setContentType(MediaType.MULTIPART_FORM_DATA);
headers.set(MediaType.MULTIPART_FORM_DATA);
headers.setContentType("Accept", Mediatype.APPLICATION_JSON_VALUE);
headers.setContentType("Content-type", Mediatype.APPLICATION_JSON_VALUE);

HttpEntity<?> requestEntity = new HttpEntity<>(obj, headers);

String result = getRestTemplate().postForEntity("url", requestEntity, String.class);

}

in application.yml

I have set

spring.serialization.fail-on-empty-beans: false

and in pom.xml I have below of version 2.12.3

jackson-annotation
jackson-core
jackson-databind
jackson-dataformat
jackson-dataformat-xml
}
Artem Bilan
  • 113,505
  • 11
  • 91
  • 118
Nishant
  • 1
  • 3

0 Answers0