I developed an OCR API using Flask to scan photos and return the scanned data. I want to call this API from Spring boot, So i can POST the photo to the API endpoint and get a response and use that response to map it to my POJO. When ever i put my photo using postman i get all Parameters as NULL. everything i did is correct. I converted the image and i sent it to the correct URL. I don't exactly know the problem here.
Flask API results with POSTMAN
{
"2": {
"adadFar": "ا 0",
"esmTijari": "PFE",
"esmTijariLatin": "EXAMPLE RNE",
"makarEjtima": "Shand! شارع الطيب",
"makarNachat": "شارع الطيب المهيري",
"nithamKanouni": "مسؤولية محدودة ald",
"rasMal": "1000000",
"tasmiya": "FACULTE DES SCIENCES",
"tasmiyaLatin": "LCS 3"
},
"adad_sejel: ": "F1700655698",
"modatCharika: ": "99",
"mouaref: ": "1887415R",
"nachatRaisi: ": "بيع الاعلاف و الالات الفلاحية و الصناعية",
"tarikh: ": "2015/09/29",
"tarikhBideyetNachat: ": "2001-12-6",
"tarikhEchhar: ": "2005-01-26"
}
Spring boot Result when POSTING the photo
{
"mouaref": null,
"adad_sejel": null,
"tarikh": null,
"tasmiya": null,
"tasmiyaLatin": null,
"esmTijari": null,
"esmTijariLatin": null,
"makarEjtima": null,
"makarNachat": null,
"nithamKanouni": null,
"rasMal": null,
"adadFar": null,
"nachatRaisi": null,
"tarikhBideyetNachat": null,
"tarikhEchhar": null,
"modatCharika": null
}
Here's my Pojo class:
public class formResponse {
private String mouaref;
private String adad_sejel;
private String tarikh;
private String tasmiya;
private String tasmiyaLatin;
private String esmTijari;
private String esmTijariLatin;
private String makarEjtima;
private String makarNachat;
private String nithamKanouni;
private String rasMal;
private String adadFar;
private String nachatRaisi;
private String tarikhBideyetNachat;
private String tarikhEchhar;
private String modatCharika;
\\getters and setters
public formResponse(){
}
Here's my Service class:
@Service
public interface FormRecognition {
public static formResponse getInfoClientFromRne(MultipartFile image) {
try {
formResponse form = new formResponse();
MultiValueMap<String, Object> bodyMap = new LinkedMultiValueMap<>();
bodyMap.add("image", new FileSystemResource(convert(image)));
// System.out.println("body map ;" bodyMap.size());
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(bodyMap, headers);
// System.out.println("Headers: " requestEntity.getHeaders());
// System.out.println("requestEntity ; " requestEntity.toString());
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<formResponse> response = restTemplate.exchange("http://172.20.10.3:3500/upload-image", HttpMethod.POST, requestEntity,
formResponse.class);
form= response.getBody();
// System.out.println("form ; " response);
// System.out.println("form ; " form.getMouaref());
return form;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static File convert(MultipartFile file) {
File convertFile = new File(file.getOriginalFilename());
try {
convertFile.createNewFile();
FileOutputStream fos = new FileOutputStream(convertFile);
fos.write(file.getBytes());
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return convertFile;
}
}
Here's my Controller:
@RestController
public class FileUploadController
{
@RequestMapping(value = "/upload", produces = {
MediaType.APPLICATION_JSON_VALUE }, method = RequestMethod.POST)
public formResponse upload(@RequestParam("image") MultipartFile[] image){
System.out.println("params: emtry");
try {
formResponse form = FormRecognition.getInfoClientFromRne(image[0]);
System.out.println("params: " form);
return form;
}
catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
CodePudding user response:
Solved it! It turns out i was making ':' in the Flask response. That's why Spring boot couldn't read the response because it was different. I changed my Python script to match the exact naming in spring boot and that's when i got the data.