【问题标题】:how can we accept request body with multiparts?我们如何接受多部分的请求正文?
【发布时间】:2019-11-06 04:38:46
【问题描述】:

我正在尝试使用请求正文和四个多部分文件进行发布请求。

我已经分享了 api

@PostMapping(value = "/deductee-master", consumes = MediaType.ALL_VALUE)
public ResponseEntity <DeducteeMaster> createDeductee(@RequestBody DeducteeMasterDTO deducteeMasterDTO, @RequestParam("trcFile") MultipartFile trcFile, @RequestParam("tenFFile") MultipartFile tenFFile, @RequestParam("wpeFile") MultipartFile wpeFile, @RequestParam("noPEFile") MultipartFile noPEFile)
throws InvalidKeyException, URISyntaxException, StorageException, IOException {
    DeducteeMaster result = deducteeMasterService.save(deducteeMasterDTO, trcFile, tenFFile, wpeFile, noPEFile);
    return ResponseEntity.created(new URI("/api/deductee-master/" + result.getKey().getId()))
        .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getKey().getId().toString()))
        .body(result);
}
{
    "timestamp": "2019-06-24T11:04:40.076+0000",
    "status": 415,
    "error": "Unsupported Media Type",
    "message": "Content type 'multipart/form-data;boundary=--------------------------603218794646006873131102;charset=UTF-8' not supported",
    "path": "/api/deductee-master"
}

【问题讨论】:

    标签: java spring-boot file-upload postman multipartform-data


    【解决方案1】:

    使用@RequestPart,而且由于您尝试同时发送RequestBodyFiles,您必须将其附加到FormData 以发送数据。

    @RequestMapping(value = "/foo", method = RequestMethod.POST, consumes = "multipart/form-data")
    public ResponseEntity<DeducteeMaster> createDeductee(@RequestPart("data") DeducteeMasterDTO deducteeMasterDTO,
                @RequestPart("trcFile") MultipartFile trcFile,
                @RequestPart("tenFFile") MultipartFile tenFFile,
                @RequestPart("wpeFile") MultipartFile wpeFile,
                @RequestPart("noPEFile") MultipartFile noPEFile)
                throws InvalidKeyException, URISyntaxException, StorageException, IOException {
    
         //LOGIC
         return // ...;
    
        }
    

    如果您想使用 CurL 进行测试

    curl -i -H "Content-Type:multipart/form-data" 
                 -F "data =@test.json;type=application/json" 
                 -F "trcFile =@test.csv;type=application/csv"
                 -F "tenFFile =@test1.csv;type=application/csv"
                 -F "wpeFile =@test2.csv;type=application/csv"
                 -F "noPEFile =@test3.csv;type=application/csv"
                 http://localhost:8080/foo
    

    如果您通过 Angular、React 或任何 javascript 语言发送发布请求,请尝试使用 FormData。将数据附加到 formData

    【讨论】:

    • 实际上我在 dto 中有大约 35 个数据字段以及这四个多部分,我如何接受数据和多部分。
    • 为了测试我正在使用 swagger。
    • curl -i -H "Content-Type:multipart/form-data" -F "deducteeMasterDTO =@realpath(C:\Users\Admin\Desktop\data.json");type=application/json" -F "trcFile =@realpath(C:\Users\Admin\Desktop\CognitiveComplexity.pdf);type=application/pdf" -F "tenFFile =@realpath(C:\Users\Admin\Desktop\CognitiveComplexity.pdf);type=application/pdf" -F "wpeFile =@realpath(C:\Users\Admin\Desktop\CognitiveComplexity.pdf);type=application/pdf" -F "noPEFile =@realpath(C:\Users\Admin\Desktop\CognitiveComplexity.pdf);type=application/pdf" http://localhost:8080/api/deductee-master
    • 我面临问题:: curl: (3) Port number ended with '\' curl: (3) Port number ended with '\' curl: (3) Port number ended with '\' curl: (3) Port number ended with '\'
    • 问题出在 '\' 上,你能不能把 @realPath 去掉,然后转到 CMD 的桌面目录,然后用 deducteeMasterDTO=@data.json;type=application/json 对其他数据做同样的事情跨度>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-28
    • 2020-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-10
    • 2017-11-24
    相关资源
    最近更新 更多