Sudhanshu Gupta
Sudhanshu Gupta

Reputation: 2315

Calling external api from spring boot with multipart/form-data

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.

Upvotes: 3

Views: 7108

Answers (2)

Sudhanshu Gupta
Sudhanshu Gupta

Reputation: 2315

I solved the issue by storing the file locally. While calling third party API, i gave the location of this file and it worked.

  1. In Controller Method:

    @PostMapping(value = "/ocrImage")
    public  ResponseEntity<GenericResponse> ocrImage(@RequestParam("file") MultipartFile file) {
    
        // I have created a function to store file locally and return the absolute path
        String fileName = fileStorageService.storeFile(file);
    
        // passing the filepath to the service method
        Object ocrDataImage = ocrService.ocrImage(fileName);
        return ResponseBuilder.buildResponse(ocrDataImage , 0, "");
    }
    
  2. Service called

    public String ocrImage(String path) {
    
        // getting the file from disk
        FileSystemResource value = new FileSystemResource(new File(path));
    
        // 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", value);
    
    
        HttpEntity<MultiValueMap<String, Object>> requestEntity= new HttpEntity<>(body, headers);
    
        RestTemplate restTemplate = new RestTemplate();
        String result = restTemplate.postForEntity(EXTERNAL_API_ENDPOINT, requestEntity,
            String.class).getBody().toString();
    
        System.out.println(result);
        return result;
    }
    

Upvotes: 2

Ananthapadmanabhan
Ananthapadmanabhan

Reputation: 6226

To disable fail on empty beans since you are using springboot, you can set the following property :

application.properties

spring.jackson.serialization.FAIL_ON_EMPTY_BEANS=false

or you can set it manually in object mapper like :

@Bean
public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    MappingJackson2HttpMessageConverter converter = 
        new MappingJackson2HttpMessageConverter(mapper);
    return converter;
}

Upvotes: 0

Related Questions