[英]How to call MultipartFile Spring REST URL with RestTemplate
When I try to call following MultipartFile Spring REST url with my Spring Template base Test method, I got following exception. 当我尝试使用基于Spring模板的Test方法调用以下MultipartFile Spring REST URL时,出现以下异常。 How can I make this correct.
我该如何纠正。 Thanks.
谢谢。
Spring REST URL: Spring REST URL:
@RequestMapping(value = "/media/uploadMultipartFile/{token}/{title}/{trailId}/{wpId}", method = RequestMethod.POST)
public @ResponseBody MediaHttp uploadMultipartFile(@RequestParam MultipartFile file,
@PathVariable String token,
@PathVariable String title,
@PathVariable String trailId,
@PathVariable String wpId,
HttpServletResponse response)
Test method: 测试方法:
try {
// Message Converters
List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
messageConverters.add(new FormHttpMessageConverter());
messageConverters.add(new SourceHttpMessageConverter<Source>());
messageConverters.add(new StringHttpMessageConverter());
messageConverters.add(new MappingJacksonHttpMessageConverter());
// RestTemplate
RestTemplate template = new RestTemplate();
template.setMessageConverters(messageConverters);
// URL Parameters
MultiValueMap<String, Object> parts = new LinkedMultiValueMap<String, Object>();
parts.add("token", "nkc2jvbrbc");
parts.add("title", "test mp4 file");
parts.add("trailId", "2");
parts.add("wpId", "7");
parts.add("file", new FileSystemResource("C:\\Users\\Public\\Pictures\\Sample Pictures\\test.mp4"));
// Post
MediaHttp result = template.postForObject(Constants.APPLICATION_URL + "/media/uploadMultipartFile/{token}/{title}/{trailId}/{wpId}", parts, MediaHttp.class);
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
Exception: 例外:
Invalid amount of variables values in [ http://test.com:8080/DMW-skeleton-1.0/media/uploadMultipartFile/ {token}/{title}/{trailId}/{wpId}]: expected 4; [ http://test.com:8080/DMW-skeleton-1.0/media/uploadMultipartFile/ {token} / {title} / {trailId} / {wpId}]中的变量值数量无效:预期为4; got 0
得到了0
The message is pretty clear, you don't specify any path parameters for submission. 消息很清楚,您没有指定任何要提交的路径参数。 You only provide a map which will be send as the body of the request.
您仅提供将作为请求正文发送的地图。
change your call to include those parameters as the last part of the method call. 更改您的调用以将那些参数包括在方法调用的最后一部分。
// URL Parameters
MultiValueMap<String, Object> parts = new LinkedMultiValueMap<String, Object>();
parts.add("file", new FileSystemResource("C:\\Users\\Public\\Pictures\\Sample Pictures\\test.mp4"));
// Post
MediaHttp result = template.postForObject(Constants.APPLICATION_URL + "/media/uploadMultipartFile/{token}/{title}/{trailId}/{wpId}", parts, MediaHttp.class, "nkc2jvbrbc", "test mp4 file", "2", "7);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.