使用 @RequestPart 参数的表单 object 结果为 404

Form object with @RequestPart parameter results in 404

我正在尝试创建一个 URI,其中需要 MultipartFile 和表单 object (bean)。

该方法适用于以下代码:

@RequestMapping(method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public void create(@RequestPart MultipartFile file,
                   @RequestPart String form) {
    // ...
}

以及以下 curl 命令:

curl -X POST http://localhost/files \
  -F file=@E:\path\to\file \
  -F form={"x":0,"y":0,"width":512,"height":512}

但是当我尝试用 bean 替换字符串时,我收到了 404 响应。

修改后的代码,不工作,有一个 bean:

@RequestMapping(method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public void create(@RequestPart MultipartFile file,
                   @RequestPart ClipForm form) {
    // ...
}

curl 中的每个部分添加 Content-Type header 似乎没有帮助:

curl -X POST http://localhost/files \
  -F file=@E:\path\to\file;type=multipart/form-data \
  -F form={"x":0,"y":0,"width":512,"height":512};type=application/json

我可能遗漏了一些让 Spring 认为请求未映射到此方法的东西,但到目前为止我无法得到什么。

无论出于何种原因,在 curl 命令中引用所有内容并使用 bash 使此工作正常。

curl -X 'POST' \
     -F 'file=@E:\path\to\file;type=multipart/form-data' \
     -F 'form={"x":0,"y":0,"width":512,"height":512};type=application/json' \
     'http://localhost/files'

我猜某个字符被 Windows cmd 误解了。


编辑

如果您想让它在 Windows cmd 中运行,您将需要使用 double quotes and escaped double quotes。它不会使用像 bash.

这样的单引号
curl -X "POST" ^
     -F "file=@E:\path\to\file;type=multipart/form-data" ^
     -F "form={\"x\":0,\"y\":0,\"width\":512,\"height\":512};type=application/json" ^
     "http://localhost/files"