Announcement Announcement Module
No announcement yet.
Problem with @RequestPart with mixed multipart request, Spring MVC 3.1.2 Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problem with @RequestPart with mixed multipart request, Spring MVC 3.1.2

    Hi all,
    I'm developing a RESTful service based on Spring MVC 3.1.2. I'm facing a problem with a controller handling mixed multipart HTTP request, with a first part with XML formatted data and a second part with a file.
    It matches quite well the situation presented in the doc here: , so I designed my controller method like this:

    @RequestMapping(value = "/message",  method = RequestMethod.POST, 
                consumes = {"multipart/form-data", "multipart/mixed"})
    @ResponseStatus( HttpStatus.CREATED )
    public String mixedrequest(
                @RequestPart("message") Message message, 
                @RequestPart("file") MultipartFile file) { 
    and the HTTP multipart request the controller have to handle is something like:

    POST	/message
    Header:	Content-type: multipart/mixed;  boundary=ARCFormBoundaryvgrvnph55ewmi
       Content-Disposition: form-data; name="message";
       Content-Type: application/xml;
         <subject> Test message </subject>
         <body> Hello world </body>
        Content-Disposition: form-data; name="file"
        Content-Type: application/jpeg
        image bytes...
    To my understanding, using the @RequestPart annotation I would expect the XML multipart section to be evaluated depending on its Content-Type and finally un-marshalled into my Message class (I'm using Jaxb2, the marshaller/unmarhaller is properly configured in the application context and the procedure is working fine for all the other controller methods when I pass the XML data as body and use the @RequestBody annotation).

    But what is actually happening is that, although the file is correctly found and parsed as MultipartFile, the "message" part is never seen and the request is always failing, not matching the controller method signature.

    DEBUG [DispatcherServlet:819] DispatcherServlet with name 'rest' processing POST request for [/message]
    DEBUG [CommonsMultipartResolver:259] Found multipart file [file] of size 199 bytes with original filename [picture], stored in memory
    DEBUG Request part 'message' not found.
    I reproduced the problem with several clients type and I am confident the format of the multipart request is ok.

    A closer look to the HTTP request once it is received by the controller:

    INFO  [LogInterceptor:54] POST Request received: http://localhost:8080/message
    DEBUG [LogInterceptor:56] |-- Headers : 5
    DEBUG [LogInterceptor:59] |    |-- host : localhost:8080
    DEBUG [LogInterceptor:59] |    |-- connection : keep-alive
    DEBUG [LogInterceptor:59] |    |-- content-length : 1487
    DEBUG [LogInterceptor:59] |    |-- content-type : multipart/form-data; boundary=----WebKitFormBoundaryQKvPGi7pRBwAViO2
    DEBUG [LogInterceptor:59] |    |-- accept : application/xml
    DEBUG [LogInterceptor:63] |--  Parameters  : 1
    DEBUG [LogInterceptor:66] |    |-- message : 
    <input_message> <subject>Test message</subject>  <body> Hello world </body> </input_message>
    shows that actually the message has been transformed in normal request parameter.
    I'm wondering if this is the expected effect of the CommonMultipartResolver ( which is properly configured in the context, together with commons-fileupload.jar in the classpath.)
    Or if this could in some way affect the @RequestPart expected behavior, at least as explained from the doc linked above...

    I found the introduction @RequestPart a big plus for development of complex RESTful services with Spring MVC, providing the possibility to handle mixed multipart request with the same logic as for normal request, re-using JSR-303 for validation, etc. But so far I had no luck with it ...

    Any advice would be much appreciated.
    Best regards and thanks in advance,

  • #2
    The CommonsMultipartResolver does nothing more then delegate the parsing of multipart elements in the request to commons-fileupload so how the request is parsed and what becomes a parameter or multipart file is inside commons-fileupload.

    If you are on a Servlet 3.0 container you could try the standard servlet 3.0 file upload support and see if that changes anything.


    • #3
      I had this problem when I used StandardServletMultipartResolver.

      I changed this by a CommonsMultipartResolver and that works !

      	public MultipartResolver multipartResolver()
      		// return new StandardServletMultipartResolver();
      		return new CommonsMultipartResolver();
      Last edited by micolator; Apr 29th, 2013, 03:32 AM.