简体   繁体   English

如何使用RestTemplate调用MultipartFile Spring REST URL

[英]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.

相关问题 如何通过受Keycloak保护的Spring restTemplate调用Rest端点 - How to call rest endpoint via Spring restTemplate that is protected by Keycloak Spring 启动 - 如何在 rest 应用程序中验证 Multipartfile - Spring boot - How to validate Multipartfile in rest application 无法使用Spring RestTemplate将混合了MultiPartFile的POST参数发送到Rest Service - Unable to send POST parameters with mix of MultiPartFile to Rest Service using spring RestTemplate Rest服务中的Spring RestTemplate postForObject调用 - Spring RestTemplate postForObject call within Rest service MultipartFile + String 作为请求参数在 Spring 中使用 RestTemplate - MultipartFile + String as Request Param using RestTemplate in Spring 如何使用Spring Rest调用URL? - How to call a URL using spring rest? 如何在 Spring 中复制 MultipartFile? - How to copy a MultipartFile in Spring? 当构造RestTemplate时无法控制时,如何在春季模拟Rest调用的响应 - How to mock response of Rest call in spring when you can't controll when the RestTemplate is constructed 没有RestTemplate的Rest调用 - Rest call without RestTemplate 使用Spring 4 restTemplate(Java Client和RestController)上传MultipartFile列表 - Uploading a List of MultipartFile with Spring 4 restTemplate (Java Client & RestController)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM