Spring REST-RestTemplate可以混合使用吗? [英] Spring REST - Can a RestTemplate consume multipart/mixed?

查看:136
本文介绍了Spring REST-RestTemplate可以混合使用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个REST服务,该服务确实响应一个zipFile和一些json数据,所有内容都在一个multipart/mixed请求中.

I want to write a REST service which does responed with a zipFile and some json data, everything in one multipart/mixed request.

服务器部分工作正常,我正在使用firefox的REST客户端对其进行测试.我的服务器发送了这样的多部分邮件

The server part works fine and i am testing it with the REST Client from firefox. My Server sends a multipart like this

--k-dXaXvCFusLVXUsg-ryiHMmkdttadgcBqi4XH

Content-Disposition: form-data; name="form"
Content-type: application/json

{"projectName":"test","signal":"true"}

--k-dXaXvCFusLVXUsg-ryiHMmkdttadgcBqi4XH
Content-Disposition: form-data; name="file2"; filename="file2.txt"
Content-type: application/octet-stream
Content-Length: 10

hallo=Welt

我知道RestTemplate可以借助MultiValueMap开箱即用地发送多部分.

I know that RestTemplate can send multiparts with the help of a MultiValueMap out of the box.

现在,我尝试使用多部分/混合响应并返回MultiValueMap

Now I tried to consume multipart/mixed responses and return a MultiValueMap

@Component
public class RestCommand 
extends AbstractLoginRestCommand<Form, MultiValueMap<String, Object>>
{
    @Override
    protected MultiValueMap<String, Object> executeInternal ( Form form )
    {
        RestTemplate restTemplate = getRestTemplate();
        MyMultiValueMap map = restTemplate.postForObject(getUrl(), form, MyMultiValueMap.class);
        return new LinkedMultiValueMap<String, Object>(map);
    }
}

class MyMultiValueMap extends LinkedMultiValueMap<String, Object>
{}

存在MyMultiValueMap可以防止类型擦除(泛型).

MyMultiValueMap exist to prevent type erasure (generics).

这给

org.springframework.web.client.RestClientException:无法提取 响应:找不到适合响应类型的HttpMessageConverter [类org.jlot.client.remote.MyMultiValueMap]和内容类型 [multipart/form-data; boundary = Rjh-fkdsI9OIyPpYwdFY7lsUIewhRSX8kE19I; charset = UTF-8] 在 org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:107) 在 org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:492)

org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [class org.jlot.client.remote.MyMultiValueMap] and content type [multipart/form-data;boundary=Rjh-fkdsI9OIyPpYwdFY7lsUIewhRSX8kE19I;charset=UTF-8] at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:107) at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:492)

FormHttpMessageConverter的Javadoc表示它可以写入但不能读取multipart/form-data.

Javadoc of FormHttpMessageConverter says it can write but not read multipart/form-data.

为什么会这样?

是否可以直接使用RestTemplate读取multipart/form-data,还是需要编写HttpMessageConverter?

Is there a way to read multipart/form-data with RestTemplate out-of-the-box or do I need to write a HttpMessageConverter?

推荐答案

我遇到了同样的问题,我认为我实现了您想要的目标. 您只需要重写表单转换器的canRead方法.以您的示例为例,下面的方法应该起作用.

I had the same issue and I think I achieved what you wanted. You just have to override the canRead method of the form converter. With your example something like below should work.

FormHttpMessageConverter formConverter = new FormHttpMessageConverter() {
    @Override
    public boolean canRead(Class<?> clazz, MediaType mediaType) {
        if (clazz == MyMultiValueMap.class) {
            return true;
        }
        return super.canRead(clazz, mediaType);
    }
};

并将此转换器添加到您的其余模板中.

And add this converter to your rest template.

这篇关于Spring REST-RestTemplate可以混合使用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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