简体   繁体   English

发布表单数据时,RestTemplate 对所有参数返回 NULL

[英]RestTemplate return NULL to all parameters when posting form data

I developed an OCR API using Flask to scan photos and return the scanned data.我使用 Flask 开发了一个 OCR API 来扫描照片并返回扫描的数据。 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.我想从 Spring Boot 调用这个 API,所以我可以将照片发布到 API 端点并获得响应并使用该响应将其映射到我的 POJO。 When ever i put my photo using postman i get all Parameters as NULL.每当我使用邮递员放置照片时,我都会将所有参数设置为 NULL。 everything i did is correct.我所做的一切都是正确的。 I converted the image and i sent it to the correct URL.我转换了图像并将其发送到正确的 URL。 I don't exactly know the problem here.我不完全知道这里的问题。

Flask API results with POSTMAN POSTMAN 的 Flask API 结果

{
    "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:这是我的 Pojo 课程:

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

Solved it!解决了! It turns out i was making ':' in the Flask response.事实证明,我在 Flask 响应中使用了“:”。 That's why Spring boot couldn't read the response because it was different.这就是为什么 Spring boot 无法读取响应,因为它不同。 I changed my Python script to match the exact naming in spring boot and that's when i got the data.我更改了我的 Python 脚本以匹配 Spring Boot 中的确切命名,这就是我获得数据的时候。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM