Spring MVC and multiprocessing

I am using Spring MVC 4 and I have a controller with the mapping / method given below:

@RequestMapping(value = "/me/bio", method = RequestMethod.POST, consumes = { "multipart/form-data" })
@ResponseBody
public JsonResponse<Boolean> saveProfileBio1(Account account, @RequestPart("file") MultipartFile file, @RequestPart("profile") @Valid ProfileBio profileBio) throws ValidationException, IOException {
...//code here
}

When I submit a multi-page form data request, it fails with an HTTP 400 Bad request with the error "org.springframework.web.multipart.support.MissingS ervletRequestPartException:" The profile profile of the requested part is "missing"

The following is a raw request:

------WebKitFormBoundarynU961NKt3K534rCg
Content-Disposition: form-data; name="profile"
{"profileName":"Zack Smith","profileDescription":"xxx","profileWebLink" :"www.abc","profilePictureUrl":"https://s3.amazonaws.com/xxx-images/default.png","profileTitle":"CTO1"}
------WebKitFormBoundarynU961NKt3K534rCg
Content-Disposition: form-data; name="file"; filename="2013-11-16 21.19.59.jpg"
Content-Type: image/jpeg 

As you can see, the request clearly has a part of the “profile”. From my debugging, the problem is that in the profile request part there is no “Content Type” set, and DefaultMultipartHttpServletRequest has the following method, which requires it to be set, and if it returns null, the whole request fails with the error above.

@Override
public HttpHeaders getMultipartHeaders(String paramOrFileName) {
String contentType = getMultipartContentType(paramOrFileName);
if (contentType != null) {
 HttpHeaders headers = new HttpHeaders();
 headers.add(CONTENT_TYPE, contentType);
 return headers;
}
else {
 return null;
}
}

, FormData , , - , Spring, -, .

, ?

+4
2

:

  • : JSON Blob FormData, . : Blob ( angular js):

    var formData = new FormData();
    formData.append('profile', new Blob([angular.toJson(profile)], {
      type: "application/json"}
    ));
    
  • ( ): getMultipartHeaders DefaultMultipartHttpServletRequest spring. CommonsMultipartResolver, (- ):

    new DefaultMultipartHttpServletRequest() {
    
        @Override
        public HttpHeaders getMultipartHeaders(String paramOrFileName) {
            // your code here
        }
    }
    
+2

, @RequestPart @RequestParam. doc @RequestPart, (, MultipartFile), HttpMessageConverter. , MultipartResolver bean. a CommonsMultipartResolver.

0

All Articles