How to call MultipartFile Spring REST URL with RestTemplate

15,270

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);
Share:
15,270
Channa
Author by

Channa

Think before code

Updated on June 12, 2022

Comments

  • Channa
    Channa almost 2 years

    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

    • Mital Pritmani
      Mital Pritmani over 8 years
      Thanks !! Code in your question helped me to resolve my issue of 400 bad request - Rest client for Image uploading by RestTemplate.
    • Hardik Bhalani
      Hardik Bhalani over 8 years
      I am having the same code, but it is giving 415 Unsupported Media Type. can you tell what I am missing here.
  • Channa
    Channa over 10 years
    @Deinum. Yes now I got the point. Many thanks for your worth full answer. Have a nice time !