【发布时间】:2014-08-01 12:11:46
【问题描述】:
我正在使用Spring MVC 4 和@RestController 来接收表单帖子。
我已经按照here 给出的提示完成了它,它与Spring Test MockMvc 配合得很好。
但是,我现在想用curl 将内容发布到我的服务器,但我找不到被接受的方法。我总是得到以下异常:
org.springframework.web.bind.MissingServletRequestParameterException:必需的字符串参数“文件”不存在
我尝试将Spring 中的@Consumes 和curl 中的-H 选项混合使用,但似乎不相关。
那么,给定以下@RestController,应该执行什么curl 命令来发布内容?
@RestController
@RequestMapping(value = ONE_COLLECTION)
public class OneCollectionController {
@RequestMapping(method = RequestMethod.POST )
public RESTDocumentListElement uploadDocument( @RequestParam("file") MultipartFile file, @RequestPart("data") NewDocumentData documentData ) throws IOException {
// -- code here --
}
}
我尝试过的最后一个命令(但出现异常):
curl http://host/oneCollection -X POST -F "file=@./myFile.txt" -H "Content-Type: multipart/form-data" -F 'data={"name"="myName"}'
工作Spring TestMockMvc代码:
// ...
MockMultipartFile firstFile = new MockMultipartFile("file", "dummyFile.txt", "text/plain", "blahblah".getBytes());
MockMultipartFile jsonFile = new MockMultipartFile("data", "", "application/json", TestUtil.convertObjectToJsonString(documentData).getBytes() );
ResultActions result = mockMvc.perform(fileUpload(ONE_COLLECTION)
.file(firstFile)
.file(jsonFile)
);
// ...
【问题讨论】:
标签: spring-mvc post curl file-upload multipartform-data