簡體   English   中英

我們可以在 spring 中同時使用 multipart 和 @RequestBody 嗎?

[英]Can we use multipart and @RequestBody together in spring?

我想創建一個 API,它可以將參數作為多部分文件和 JSON 對象(@RequestBody)。 請在調用此 API 時找到以下代碼段。 我收到HTTP 415 Unsupported Media Type 錯誤 如果我刪除@RequestBody LabPatientInfo reportData那么它工作正常。

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

您可以像下面這樣使用@RequestPart 這將支持 json 對象和多部分文件。

@ResponseBody
public ResponseEntity<String>
saveReport(@RequestPart (value="reportFile") MultipartFile reportFile,
           @RequestPart LabPatientInfo reportData) throws IOException {

為了使用curl對其進行測試,您可以為 json 部分創建一個文件 ( reportData )。 例如,假設您創建了“mydata.json”文件並將您的 json 負載粘貼到其中。 並說您的reportFile是“report.txt”。 現在您可以使用 curl 發送請求,如下所示。

curl -v -H "Content-Type:multipart/form-data" -F "reportData=@mydata.json;type=application/json" -F "reportFile=@report.txt;type=text/plain"  http://localhost:8080/MyApp/lab/saveReport

Spring Roo 2.0.0.M3 包括對 REST API 自動腳手架的支持。

有關完整信息,請參閱參考手冊中的REST API

請注意,M3 版本生成的工件可能會在較新版本中發生變化,因此如果您使用 RC1 或更高版本打開它,您的項目可能不會自動升級。

願原力與你同在。

當參數用 @RequestPart 注釋時,該部分的內容通過 HttpMessageConverter 傳遞,以解決方法參數並記住請求部分的“Content-Type”。 這類似於@RequestBody 根據常規請求的內容解析參數所做的工作。

因此,我們可以將@Requestbody 解析為@RequestPart 作為“abaghel”,並且reportData 需要是一個json 文件。

接收 json 對象和通用文件的 post 方法示例:

public ResponseEntity<Resource> postGenerateReport(@RequestPart GenerateReportDTO, generateReportDTO, @RequestPart MultipartFile jxhtmlReport)

對於PostMan設置(或 curl 或任何其他 REST 測試實用程序),您只需添加帶有 2 個元素的表單數據請求:

  1. Key:generateReportDTOValue:帶有 .json 擴展名的文件(以及與對象兼容的內容)
  2. Key:jxhtmlReportValue:任何文件。

GL

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM