Spring REST - Can a RestTemplate consume multipart/mixed?

半城伤御伤魂 提交于 2019-12-04 09:50:22

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

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