Upgrading to Spring 5 broke RestTemplate MultipartFile upload

邮差的信 提交于 2020-07-21 05:19:48

问题


I upgraded from Spring 4.3.16 to Spring 5.0.7. When trying to upload a file using the restTemplate I started to get a "400 - Bad Request". After messing around the only difference in behavior I noticed was removing requestEntity from the exchange call and the error (bad request) goes away, however it fails because the file is not present to be uploaded.

Any help would be greatly appreciated!

The client:

    public <T> ResponseEntity<T> uploadMultipartFile(String requestParamName, byte[] byteArray, String fileName,
        Class<T> responseType) {

    ByteArrayResource byteArrayAsResource = new ByteArrayResource(byteArray) {
        @Override
        public String getFilename() {
            return fileName;
        }
    };

    MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
    map.add(requestParamName, byteArrayAsResource);

    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.MULTIPART_FORM_DATA);

    HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(map, headers);

    return getRestTemplate().exchange(buildUriWithVariables(new HashMap<>()), HttpMethod.POST, requestEntity, responseType);
}

And the controller:

@RequestMapping("/multipleUpload")
public ModelAndView multipleUpload(HttpServletRequest request, HttpServletResponse response,
        @RequestParam(value = "files", required = false) MultipartFile[] files,
        @ModelAttribute("document") Document document,
        BindingResult result) {}

It looks like Apache is complaining about the request body:

[Wed Jul 18 11:02:29.705758 2018] [:error] [pid 1313:tid 140177850750720] [client ] ModSecurity: Access denied with code 400 (phase 2). Match of "eq 0" against "REQBODY_ERROR" required. [file "/etc/modsecurity/modsecurity.conf"] [line "54"] [id "200001"] [msg "Failed to parse request body."] [data "Multipart: Invalid boundary in C-T (characters)."] [severity "CRITICAL"] [hostname ""] [uri "/restless/documentUpload/multipleUpload"] [unique_id "W09WhX8AAAEAAAUhs7IAAABI"]

来源:https://stackoverflow.com/questions/51390193/upgrading-to-spring-5-broke-resttemplate-multipartfile-upload

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