RestTemplate return 发布表单数据时所有参数为 NULL
RestTemplate return NULL to all parameters when posting form data
我开发了一个 OCR API 使用 Flask 来扫描照片和 return 扫描数据。我想从 Spring 引导调用此 API,所以我可以 POST 照片到 API 端点并获得响应并使用该响应将其映射到我的 POJO .每当我使用邮递员放置照片时,我都会将所有参数设置为 NULL。我所做的一切都是正确的。我转换了图像并将其发送到正确的 URL。我不太清楚这里的问题。
Flask API 结果与 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 POSTING 照片
时的启动结果
{
"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
}
这是我的 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(){
}
这是我的服务 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;
}
}
这是我的控制器:
@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;
}
}
}
解决了!事实证明我在 Flask 响应中制作了“:”。这就是为什么 Spring boot 无法读取响应,因为它不同。我更改了我的 Python 脚本以匹配 spring 引导中的确切命名,那是我获得数据的时候。
我开发了一个 OCR API 使用 Flask 来扫描照片和 return 扫描数据。我想从 Spring 引导调用此 API,所以我可以 POST 照片到 API 端点并获得响应并使用该响应将其映射到我的 POJO .每当我使用邮递员放置照片时,我都会将所有参数设置为 NULL。我所做的一切都是正确的。我转换了图像并将其发送到正确的 URL。我不太清楚这里的问题。
Flask API 结果与 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 POSTING 照片
时的启动结果{
"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
}
这是我的 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(){
}
这是我的服务 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;
}
}
这是我的控制器:
@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;
}
}
}
解决了!事实证明我在 Flask 响应中制作了“:”。这就是为什么 Spring boot 无法读取响应,因为它不同。我更改了我的 Python 脚本以匹配 spring 引导中的确切命名,那是我获得数据的时候。