Spring 带有@RequestPart参数的表单对象将导致404
我正在尝试创建一个URI,其中需要Spring 带有@RequestPart参数的表单对象将导致404,spring,spring-mvc,curl,spring-boot,multipartform-data,Spring,Spring Mvc,Curl,Spring Boot,Multipartform Data,我正在尝试创建一个URI,其中需要多部分文件和表单对象(bean) 该方法使用以下代码: @RequestMapping(method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE) public void create(@RequestPart MultipartFile file, @RequestPart String form) { // ... }
多部分文件
和表单对象(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中为每个部分添加内容类型标题似乎没有帮助:
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'
我猜一个字符被Windowscmd误解了
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"
编辑 如果希望它在Windowscmd中工作,则需要使用。它不会使用像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"
卷曲字符串中的
type
不应该是Content-type
吗?否,type
用于在多部件请求的情况下指定部件的内容类型。请参阅。在请求映射中,您没有指定“/files”部分-如何映射它?还可以使用-vv查看整个消息的实际内容类型(而不是边界)@BorisTreukhov“/files”映射由封装的@RestController
完成。使用curl-v
,我发现我正确地获得了具有正确边界的多部分/表单数据的全局内容类型。