2017-03-17 77 views
0

我想创建一个URI,其中MultipartFile和表单对象(bean)都是必需的。具有@RequestPart参数的表单对象导致404

的方法用下面的代码工作:

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

而下面卷曲命令:

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

但只要我试图通过一个bean来替换字符串,我得到一个404响应。

修改后的代码,不工作,用豆:

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

添加每个部分的Content-Type头在卷曲似乎并没有帮助:

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

我大概错过了一些让Spring认为请求没有映射到这个方法的东西,但是到目前为止我无法得到什么。

+0

不应该在你的卷曲字符串中使用'Content-Type'吗? –

+0

不,'type'用于指定多部分请求的部件的内容类型。请参见[cURL手册页](https://curl.haxx.se/docs/manpage.html#-F)。 – kagmole

+0

在你的请求映射中你没有指定'/ files'部分 - 它应该如何映射? –

回答

0

无论出于何种原因,引述在卷曲指挥一切,使用bash的作出这个工作。

curl -X 'POST' \ 
    -F '[email protected]:\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 "[email protected]:\path\to\file;type=multipart/form-data"^
    -F "form={\"x\":0,\"y\":0,\"width\":512,\"height\":512};type=application/json"^
    "http://localhost/files" 
相关问题