简体   繁体   中英

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. How can I make this correct. Thanks.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM