Java 如何使用 RestTemplate 调用 MultipartFile Spring REST URL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20467965/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 02:09:35  来源:igfitidea点击:

How to call MultipartFile Spring REST URL with RestTemplate

javaspringrestspring-mvc

提问by Channa

When I try to call following MultipartFile Spring REST url with my Spring Template base Test method, I got following exception. How can I make this correct. Thanks.

当我尝试使用我的 Spring 模板基础测试方法调用以下 MultipartFile Spring REST url 时,出现以下异常。我怎样才能做到这一点。谢谢。

Spring REST URL:

春季 REST 网址:

 @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 MappingHymansonHttpMessageConverter());

        // 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; got 0

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

采纳答案by M. Deinum

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