【问题标题】:Spring - RestTemplate - Multipart FileSpring - RestTemplate - 多部分文件
【发布时间】:2017-05-24 09:23:46
【问题描述】:

这个控制器工作正常

@Controller
public class FileUploadController {
....
@PostMapping("/convert")
public void fileUpload(@RequestParam("file") MultipartFile file, 
     RedirectAttributes redirectAttributes, HttpServletResponse response) {

现在我想通过 RestTemplate 从另一个 spring 项目中调用这个控制器。我尝试了很多东西,但注意到作品。这是我最后的代码:

@Controller
public class FileController {
....
@PostMapping("/convert")
public void fileUpload(@RequestParam("file") MultipartFile file, RedirectAttributes redirectAttributes,
        HttpServletResponse response) throws Exception {


    ArrayList<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>(
            Arrays.asList(new FormHttpMessageConverter(),new MappingJackson2HttpMessageConverter(), new ResourceHttpMessageConverter()));

    RestTemplate template = restTemplate();

    template.setMessageConverters(converters);

    HttpHeaders header = new HttpHeaders();
    header.setContentType(MediaType.MULTIPART_FORM_DATA);

    MultiValueMap<String, Object> multipartRequest = new LinkedMultiValueMap<>();
    multipartRequest.add("file", file);

    HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(multipartRequest, header);

    template.postForObject("http://localhost:8080/convert", requestEntity, String.class);

}

如果调用 FileUploadController(通过邮递员),它可以工作。如果如果调用 FileController 我得到这个异常

"exception": 
"org.springframework.http.converter.HttpMessageNotWritableException",
"message": "Could not write request: no suitable HttpMessageConverter found for request type [org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile]",
"path": "/convert"

【问题讨论】:

    标签: java spring spring-mvc multipart


    【解决方案1】:

    看看这里的答案,应该正是你要找的:Attempting to test rest service with multipart file

    关于使用RestTemplate 将多部分文件发布到休息服务的问题。

    基本上,您要做的就是模拟文件上传。你可以试试这样的:

    MultiValueMap<String, Object> parameters = new LinkedMultiValueMap<String, Object>();
    parameters.add("file", new FileSystemResource("file.jpg"));
    
    HttpHeaders headers = new HttpHeaders();
    headers.set("Content-Type", "multipart/form-data");
    headers.set("Accept", "text/plain");
    
    String result = restTemplate.postForObject(
        "http://host:port/path", 
        new HttpEntity<MultiValueMap<String, Object>>(parameters, headers), 
        String.class);
    

    【讨论】:

    • 您能否提供此问题与您发布的问题的关系。请参阅stackoverflow.com/help/how-to-answer 了解社区指南
    • @harshavmb 完成
    • 与我的代码的唯一区别是 parameters.add("file", new FileSystemResource("file.jpg"));"。而不是“multipartRequest.add("file", file);"。如果我以这种方式更改我的代码,请求将被传递到第一个控制器。伟大的!但是现在我成为响应“无法提取响应:没有找到适合响应类型 [class java.lang.String] 的合适 HttpMessageConverter”的响应的例外......
    • 可能是您的客户端无法理解服务器收到的响应类型。由于您期望的响应是一个简单的字符串,请尝试将响应类型添加到 @PostMapping 符号,如下所示:@PostMapping(path="/convert", produces=MediaType.TEXT_PLAIN_VALUE)
    猜你喜欢
    • 1970-01-01
    • 2014-05-19
    • 2020-03-18
    • 2021-05-29
    • 1970-01-01
    • 1970-01-01
    • 2020-09-24
    • 2018-01-06
    • 2014-10-31
    相关资源
    最近更新 更多