Announcement Announcement Module
Collapse
No announcement yet.
Make multipart request Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Make multipart request

    Hello. I have such magic method:
    Code:
    @RequestMapping(method = RequestMethod.POST, value = "/createMagic", produces = "application/json")
        public @ResponseBody String createMagic(@RequestPart("magic-form") MagicForm magicForm,
                                                   @RequestPart("imageA") MultipartFile imageA, @RequestPart("imageB") MultipartFile imageB,
                                                   Principal principal) throws Exception {
    ...
    }
    And I want to test it with:
    Code:
    @Test
        public void shouldCreateQuestion() throws Exception {
            RestTemplate restTemplate = new RestTemplate();
    
            MultiValueMap<String, Object> mvm = new LinkedMultiValueMap<String, Object>();
            MagicForm magicForm = new MagicForm();
            magicForm.setCustomField("customValue");
            String formJSON = mapper.writeValueAsString(magicForm);
    
            mvm.add("magic-form", formJSON);
            mvm.add("imageA", new ClassPathResource("images/imageA.jpg"));
            mvm.add("imageB", new ClassPathResource("images/imageB.jpg"));
            URI result = restTemplate.postForLocation("http://localhost:8181/spring/user/createMagic", mvm);
        }
    Unfortunatelly I have an error for test:
    Code:
    org.springframework.web.client.HttpClientErrorException: 415 Unsupported Media Type
    and for server side:
    Code:
    DEBUG: org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver - Resolving exception from handler [public java.lang.String ToTspring.controller.UserController.createMagic(magic.form.MagicForm,org.springframework.web.multipart.MultipartHttpServletRequest,java.security.Principal) throws java.lang.Exception]: org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'text/plain;charset=ISO-8859-1' not supported
    So it looks like I need to pass application/json ? How is it possible to do?
    Thanks
Working...
X