我想創建一個API,它可以有參數作爲多部分文件和JSON對象(@RequestBody)。調用此API時請找到以下代碼片斷我得到415 HTTP錯誤。如果我刪除「@RequestBody LabPatientInfo reportData」,那麼它工作正常。春天我們可以一起使用multipart和@RequestBody ..嗎?
@RequestMapping(value={"/lab/saveReport"}, method={RequestMethod.POST},
consumes={"multipart/form-data"}, headers={"Accept=application/json"})
@ResponseBody
public ResponseEntity<String>
saveReport(@RequestParam(value="reportFile") MultipartFile reportFile,
@RequestBody LabPatientInfo reportData) throws IOException {
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type", "application/json; charset=utf-8");
logger.info("in Lab Save Report");
logger.info("Report Data {} ", reportData);
//logger.info("Request BODY {} ", request.getAttribute("data"));
return new ResponseEntity<String>(HttpStatus.OK);
}
以下是LabPatientInfo類。
@RooJson(deepSerialize = true)
@RooToString
public class LabPatientInfo {
private String firstName;
private String phoneNumber;
private String DateOfBirth;
private Integer age;
private String gender;
private String refferedBy;
private String reportfile;
private String reportType;
private String reportDate;
private String purpose;
private String followUpDate;
private List<ReportDataInfo> analytes;
同時擊中API我傳遞以下JSON對象與上傳文件..
{
"firstName":"abc",
"phoneNumber":"898989",
"DateOfBirth":"asas",
"age":"asas",
"gender":"asas",
"refferedBy":"asas",
"reportfile":"asas",
"reportType":"asas",
"reportDate":"asas",
"purpose":"asas",
"followUpDate":"asas",
"analytes":null
}
是的,這將工作 –
嘿我使用上面的代碼,現在得到HTTP狀態400 - 所需的請求部分'reportData'不存在。 – Mayur
@mayur你如何發送請求?你可以嘗試使用捲曲。我已經用細節更新了我的答案。請檢查。 – abaghel