I am working in a project where i need to get the data out of image using ocr. I am using ocr by third parties where I can upload file and get the data out of ocr.
I need to call this API through spring boot. This api is multipart/form-data.
I created a function which takes the file and try to create a request to post the file to the external api. I am getting error
"message": "Type definition error: [simple type, class java.io.FileDescriptor]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class java.io.FileDescriptor and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain:
org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile[\"inputStream\"]->java.io.FileInputStream[\"fd\"])",
Controller Method:
@PostMapping(value = "/ocrImage")
public ResponseEntity<GenericResponse> ocrImage(@RequestParam("file") MultipartFile file) {
Object ocrDataImage = ocrService.ocrImage(file);
return ResponseBuilder.buildResponse(ocrDataImage , 0, "");
}
Service called
public Object ocrImage(MultipartFile file) {
// adding headers to the api
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
headers.set("x-key", API_KEY);
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
body.add("file", file);
HttpEntity<MultiValueMap<String, Object>> requestEntity= new HttpEntity<>(body, headers);
RestTemplate restTemplate = new RestTemplate();
Object result = restTemplate.postForEntity(EXTERNAL_API_ENDPOINT, requestEntity,
String.class);
System.out.println(result);
return result;
}
When postForEntity is called, i Get the error mentioned above.
Let me know if you need more details.