春季启动RestTemplate-多部分/混合 [英] Spring boot RestTemplate - multipart/mixed

查看:104
本文介绍了春季启动RestTemplate-多部分/混合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具有一个REST API,该API仅接受内容类型为multipart/mixed的内容.

Have a REST API which only accepts content type multipart/mixed.

尝试使用restTemplate并生成内容类型为multipart/mixed的REST请求. 如果我发表评论,则setContentType restTemplate默认为multipart/form-data.

Trying to use restTemplate and generate REST request with content type multipart/mixed. If I comment setContentType restTemplate defaults to multipart/form-data.

setContentType(MediaType.parseMediaType("multipart/mixed"))

但是,没有运气,我可以如何调用API生成多部分/混合请求的任何示例?

But no luck, any example how I can call API generating multipart/mixed request?

也许有帮助

HttpHeaders publishHeaders = new HttpHeaders();
publishHeaders.set(HEADER_TABLEAU_AUTH, token);
publishHeaders.setContentType(MediaType.parseMediaType("multipart/mixed"));
String response;
LinkedMultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
String payload = "<tsRequest>\n" +
        ............................
       "</tsRequest>";
map.add(TABLEAU_PAYLOAD_NAME, payload);
map.add("tableau_datasource", new FileSystemResource("/extract/test.tde"));
HttpEntity<LinkedMultiValueMap<String, Object>> entity = new HttpEntity<>(map, publishHeaders);
try {
response = restTemplate.postForObject(url + PUBLISH_DATASOURCE_SINGLE_CHUNK, entity, String.class, siteId);
} catch (RestClientException restEx) {
   log.error(....);
   throw restEx;
}

推荐答案

因此,不幸的是,使用"spring-web-4.3.12.RELEASE.jar"中的Springs RestTemplate的当前实现确实无法解决您的问题. ".在所有情况下,它都假定多部分数据的唯一类型是"multipart/form-data :,因此不会重新识别请求的多部分性质.

So, unfortunately, there really is no way to solve your problem with the current implementation of Springs RestTemplate from "spring-web-4.3.12.RELEASE.jar". It assumes in all cases that the only type of multipart data is "multipart/form-data:, and so it does not recoignize the multipart nature of your request.

org.springframework.http.converter.FormHttpMessageConverter:第247-272行

org.springframework.http.converter.FormHttpMessageConverter: lines 247-272

@Override
@SuppressWarnings("unchecked")
public void write(MultiValueMap<String, ?> map, MediaType contentType, HttpOutputMessage outputMessage)
        throws IOException, HttpMessageNotWritableException {

    if (!isMultipart(map, contentType)) {
        writeForm((MultiValueMap<String, String>) map, contentType, outputMessage);
    }
    else {
        writeMultipart((MultiValueMap<String, Object>) map, outputMessage);
    }
}


private boolean isMultipart(MultiValueMap<String, ?> map, MediaType contentType) {
    if (contentType != null) {
        return MediaType.MULTIPART_FORM_DATA.includes(contentType);
    }
    for (String name : map.keySet()) {
        for (Object value : map.get(name)) {
            if (value != null && !(value instanceof String)) {
                return true;
            }
        }
    }
    return false;
}

如果您查看私有方法"isMultipart"的第一部分,您将看到:

If you look at the first part of the private method "isMultipart", you will see this:

    if (contentType != null) {
        return MediaType.MULTIPART_FORM_DATA.includes(contentType);
    }

它会检查您是否已声明"multipart/form-data",但您声明的是"multipart/mixed",因此失败.

It check to see if you have declared "multipart/form-data", but yours is "multipart/mixed", so it fails.

在许多其他方面它也可能会失败,但这是问题的根源.

There are various other points at which it may also fail, but that's the root of the problem.

如果您仍要使用RestTemplate,唯一的解决方案是实现自己的消息转换器,该消息转换器可以识别所需的媒体类型,并将其添加到模板消息转换器中.

The only solution if you want to still use RestTemplate is to implement your own message converter that recognizes the desired media type, and add it to the templates message converters.

您还可以编写自己的RestTemplate变体,方法是对其进行扩展,复制粘贴和修改,或者从头开始创建一个客户端,该客户端使用一些更基本的功能,例如apache HttpClient(甚至是我想的CORE java).

You could also write your own variation of RestTemplate by extending it, copy-paste and modify, or creating a client from scratch that uses something a bit more basic like apaches HttpClient (or even CORE java I suppose).

这篇关于春季启动RestTemplate-多部分/混合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆