MultipartFile is null when I use StandardServletMultipartResolver

25 Views Asked by At

So recently updated to spring boot 3 which meant CommonsMultipartResolver() was removed and what I found was that the replacement was StandardServletMultipartResolver().

However when using the StandardServletMultipartResolver().resolveMultipart to replace the CommonsMultipartResolver().resolveMultipart the MultipartHttpServletRequest I get in return always has empty multipartFiles when there is a value when using CommonsMultipartResolver().resolveMultipart

Code snippet here, this is used for a junit test where mocking the files. When using CommonsMultipartResolver the serveltRequest.multipfiles will have values inside. However when changed to StandardServletMutipartResolver it will return empty.

MultipartHttpServletRequest servletRequest = adaptToMultipartRequest(request);
private static MultipartHttpServletRequest adaptToMultipartRequest(ClientHttpRequest request) {
            MockClientHttpRequest source = (MockClientHttpRequest) request;
            MockHttpServletRequest target = new MockHttpServletRequest();
            target.setContent(source.getBodyAsBytes());
            source.getHeaders().forEach((name, values) -> values.forEach(v -> target.addHeader(name, v)));
            return new CommonsMultipartResolver().resolveMultipart(target);
}

When tracing through StandardServletMutipartResolver I can see where things goes wrong because target.parts is not set anywhere and tracing into the library that is used to determine what get put into the multipartFiles for the serveltRequest. However the exact same input going through CommonsMultipartResolver will output a value into multipartFiles for serveltRequest but I can't trade that part of the code in my IDE as it just says source not found. Anyone know what the difference between the 2 are and how to fix it? Thanks!

0

There are 0 best solutions below