Spring @RequestPart 多部分/混合 对象错误

3
我正在尝试使用RequestParts上传带有额外参数的文件。我已成功上传文件;但是,当我尝试添加额外参数时,我会收到错误响应。
我的控制器:
@RequestMapping(value = "/v1/cases/{caseId}/file", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public Success uploadFile(
        @RequestPart(value="file") MultipartFile file,
        @RequestPart(value="fileParameters") FileParameters fileParameters) throws FileNotFoundException, IOException {

我尝试以两种不同的方式进行POST,但出现了不同的错误:
1)
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="file"; filename="myFile"
Content-Type: 


----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="fileParameters"

{"filePassword":"testPassword", "configuration":{}, "target":null}
----WebKitFormBoundaryE19zNvXGzXaLvS5C

这个错误的信息是:
The server is refusing to service the request because the entity of the request is in a format not supported by the requested resource for the requested method. See 'supportedMediaTypes' in 'additionalInfo' for a list of supported types

2)

----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="file"; filename="myFile"
Content-Type: 


----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="fileParamters[filePassword]"

testPassword
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="fileParamters[configuration]"

{}
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="fileParamters[target]"

null
----WebKitFormBoundaryE19zNvXGzXaLvS5C

它返回以下错误:
"rootExceptionClass": "org.springframework.web.multipart.support.MissingServletRequestPartException",
"rootExceptionMessage": "Required request part 'keyParameters' is not present."

我假设第一种方法是正确的;然而,该应用程序支持JSON,所以我不确定我在配置方面缺少什么。我需要在请求中添加什么才能使它正常工作,或者我在消息转换器中漏掉了什么。
注意:不确定这是否重要,但我正在使用Postman测试端点。

嗨,你解决了吗?我也卡在同样的问题上。我看了很多解决方案,但似乎都不起作用。 - mushfek0001
不幸的是,我从来没有这样做过。我只是将请求体作为JSON字符串传递给请求参数,并使用Jackson将其转换为所需的对象。远非理想。 - user856354
旧问题,不确定是否是问题,但仍然会留下评论。您的“fileParameters”作为表单数据发布。也许尝试发布为application/json。 - TrueCoke
2个回答

0

我曾经遇到过和你一样的问题!我把@RequestPart改成了@Multipart,问题得到了解决。希望对你有所帮助!


0

添加

内容类型:application/json;

在以下行的下方

Content-Disposition: form-data; name="fileParameters"

明确如何解析您的参数

请参阅Spring文档此处


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接