gpt4 book ai didi

java - 我们如何接受多部分的请求主体?

转载 作者:行者123 更新时间:2023-12-02 04:00:40 25 4
gpt4 key购买 nike

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

我已经分享了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"
}

最佳答案

使用@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 语言发送 post 请求,请尝试使用 FormData.Append 将数据添加到 formData

关于java - 我们如何接受多部分的请求主体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56735435/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com