Can we use multipart and @RequestBody together in spring?

后端 未结 4 1029
醉酒成梦
醉酒成梦 2021-02-20 06:00

I want to create a API which can have parameter as multipart file and JSON object (@RequestBody). Please find following snippet while calling this API. I am getting HTTP 415 Uns

4条回答
  •  青春惊慌失措
    2021-02-20 06:42

    You can use @RequestPart like below. This will support both json object and multipart file.

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

    In order to test it using curl you can create one file for your json part (reportData). Say for example you create "mydata.json" file and paste your json payload in it. And say your reportFile is "report.txt". Now you can send request using curl like below.

    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
    

提交回复
热议问题