Can a RestTemplate consume 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.

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

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

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 exist to prevent type erasure (generics).

This gives

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)

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

Why is it like this?

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


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.


I use this solution at the moment:

@ResponseBody
@PostMapping(value = JlotApiUrls.PUSH, produces = "application/json")
public List<PushResultDTO> push ( 
        @PathVariable String projectName,       
        @PathVariable String versionName, 
        @RequestPart("file") MultipartFile multipartFile, 
        @RequestPart("data") @Valid PushForm pushForm 
   ) throws IOException, BindException
{
 ...
}

https://github.com/kicktipp/jlot/blob/master/jlot-web/src/main/java/org/jlot/web/api/controller/PushController.java

链接地址: http://www.djcxy.com/p/48712.html

上一篇: 如何使用angular js spring mvc上传多部分文件

下一篇: RestTemplate可以消耗多部分/混合吗?