繁体   English   中英

使用Rest模板将Multipartfile发布到Rest Service

[英]Post Multipartfile to Rest Service using rest template

我正在使用Spring 3和RestTemplate。 我基本上有两个应用程序,其中一个必须将值发布到另一个应用程序。 通过休息模板。

当要发布的值是String时,它可以完美工作,但是当我必须发布混合和复杂的参数(如MultipartFiles)时,我会收到一个转换器异常。

例如,我有这个:

App1-PostController:

@RequestMapping(method = RequestMethod.POST)
    public String processSubmit(@ModelAttribute UploadDTO pUploadDTO, BindingResult pResult) throws URISyntaxException, IOException {
        URI uri = new URI("http://localhost:8080/app2/file/receiver");
        MultiValueMap<String, Object> mvm = new LinkedMultiValueMap<String, Object>();
        mvm.add("param1", "TestParameter");
        mvm.add("file", pUploadDTO.getFile()); // MultipartFile
        Map result = restTemplate.postForObject(uri, mvm, Map.class);
        return "redirect:postupload";
    }

另一方面,我有另一个Web应用程序(App2),它从App1接收参数。

App2-ReceiverController

@RequestMapping(value = "/receiver", method = { RequestMethod.POST })
    public String processUploadFile(
            @RequestParam(value = "param1") String param1,
            @RequestParam(value = "file") MultipartFile file) { 
        if (file == null) {
            System.out.println("Shit!... is null");
        } else {
            System.out.println("Yes!... work done!");
        }
        return "redirect:postupload";
    }

我收到以下错误

org.springframework.http.converter.HttpMessageNotWritableException: Could not write request: no suitable HttpMessageConverter found for request type [org.springframework.web.multipart.commons.CommonsMultipartFile]
    at org.springframework.http.converter.FormHttpMessageConverter.writePart(FormHttpMessageConverter.java:292)
    at org.springframework.http.converter.FormHttpMessageConverter.writeParts(FormHttpMessageConverter.java:252)
    at org.springframework.http.converter.FormHttpMessageConverter.writeMultipart(FormHttpMessageConverter.java:242)
    at org.springframework.http.converter.FormHttpMessageConverter.write(FormHttpMessageConverter.java:194)
    at org.springframework.http.converter.FormHttpMessageConverter.write(FormHttpMessageConverter.java:1)
    at org.springframework.web.client.RestTemplate$HttpEntityRequestCallback.doWithRequest(RestTemplate.java:588)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:436)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:415)
    at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:294)
    at com.yoostar.admintool.web.UploadTestController.create(UploadTestController.java:86)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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