Spring REST - Can a RestTemplate consume multipart/mixed?

匿名 (未验证) 提交于 2019-12-03 08:57:35

问题:

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?

回答1:

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.



回答2:

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



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!