gpt4 book ai didi

java - 上传两个文件和一个对象失败并出现错误 415

转载 作者:行者123 更新时间:2023-12-01 19:29:18 24 4
gpt4 key购买 nike

我正在尝试使用一个 http 休息调用,该调用将两个文件的输入作为 dto

@RequestMapping(name = "/my/endpoint", method = RequestMethod.POST, consumes = {MediaType.MULTIPART_FORM_DATA_VALUE,
MediaType.APPLICATION_JSON_VALUE})
public @ResponseBody
EndpointDto createEndpoint(@RequestBody MultipartFile requestFile, @RequestBody MultipartFile responseFile,
@RequestBody MyDto myDto){
return endpointService.createEndpoint(requestFile, responseFile, myDto);
}

我收到以下错误:

    org.springframework.web.HttpMediaTypeNotSupportedException: 
Content type 'multipart/form-data;boundary=----WebKitFormBoundarylq2UHYfBi0nAsRo7;charset=UTF-8' not supported

我通过 swagger 消耗端点。我认为问题在于我处理输入字段的方式,但我不知道问题是什么。

最佳答案

您可以更改 Controller 参数如下

@RequestMapping(name = "/my/endpoint", method = RequestMethod.POST)
public ResponseEntity<EndpointDTO> createEndpoint(
@RequestPart(value = "file1") MultipartFile filea,
@RequestPart(value = "file2") MultipartFile fileb,
@RequestPart MyDto myDto){
return endpointService.createEndpoint(filea, fileb, myDto);
}

您可以使用@RequestPart获取文件和DTO值。

您可以使用以下curl测试上述API

curl -v -H "Content-Type:multipart/form-data" -F "myDto=@body;type=application/json" -F "file1=@file1.txt" -F "file2=@file2.txt" -X POST http://<path>/my/endpoint

注意:这里我使用“body”文件来传递有效负载,并使用 file1.txt 和 file2.txt 来传递 MultipartFile。希望您熟悉并理解curl命令。

关于java - 上传两个文件和一个对象失败并出现错误 415,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60253991/

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