【问题标题】:How to call MultipartFile Spring REST URL with RestTemplate如何使用 RestTemplate 调用 MultipartFile Spring REST URL
【发布时间】:2013-12-09 10:13:24
【问题描述】:

当我尝试使用 Spring Template 基本测试方法调用以下 MultipartFile 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)

测试方法:

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());
    }

例外:

[http://test.com:8080/DMW-skeleton-1.0/media/uploadMultipartFile/{token}/{title}/{trailId}/{wpId}] 中的变量值数量无效:预期为 4;得到 0

【问题讨论】:

  • 谢谢!!您问题中的代码帮助我解决了 400 bad request 的问题 - Rest Client for Image uploading by RestTemplate。
  • 我有相同的代码,但它给出了 415 Unsupported Media Type。你能告诉我这里缺少什么吗?

标签: java spring rest spring-mvc


【解决方案1】:

消息很清楚,您没有指定任何提交的路径参数。您只需提供将作为请求正文发送的地图。

更改您的调用以将这些参数作为方法调用的最后一部分。

// 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);

【讨论】:

  • @Deinum。是的,现在我明白了。非常感谢您的完整回答。玩得开心!
猜你喜欢
  • 2019-08-03
  • 1970-01-01
  • 2018-02-01
  • 2017-07-01
  • 1970-01-01
  • 2015-05-19
  • 1970-01-01
  • 2019-06-18
  • 1970-01-01
相关资源
最近更新 更多