Home  >  Forum  >  Spring
Post New Query

Spring 4 Rest service file upload - Getting Expected MultipartHttpServletRequest: is a MultipartResolver configured?


joined on
September 13,2016
Asked on September 13,2016
I am working on a spring rest service to upload a file. However I am getting the following exception when I try to submit the POST request from HTML file

exception stacktrace
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalArgumentException: Expected MultipartHttpServletRequest: is a MultipartResolver configured?
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:980)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:870)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:844)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1290)
at [internal classes]

Controller Class snippet
@RestController
@RequestMapping(value = "/restService")
// Max uploaded file size (here it is 20 MB)
//@MultipartConfig(fileSizeThreshold = 20971520)
public class RestServiceController {
@RequestMapping(value = "/fileUpload",method = RequestMethod.POST)
//@Consumes(MediaType.MULTIPART_FORM_DATA)
//@Produces(MediaType.TEXT_PLAIN)
public @ResponseBody String uploadFile(@RequestParam("uploadedFile") MultipartFile uploadedFileRef){
System.out.println("Entering RestServiceController.uploadFile");
applicationContext.xml
  <mvc:annotation-driven />
<context:component-scan base-package="com.suraj.springrest" />
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
     <property name="maxUploadSize">
         <value>20971520</value>
     </property>
</bean> 
web.xml
<servlet>
<servlet-name>SpringRestWebservice</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/SpringRestWebservice-servlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>SpringRestWebservice</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
HTML file
<form method="POST" enctype="multipart/form-data"
      action="http://localhost:9080/SpringRestWebservice/restService/fileUpload">
   File to upload: <input type="file" name="uploadedFile"><br />
   <input type="submit" value="Upload">
</form>

Not sure if I am missing anything. Please help
joined on
November 18,2013
Replied on September 13,2016

Looking into your code, it seems that you have configured wrong contextConfigLocation in web.xml. You need to test by changing code as below.

<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</init-param>

joined on
November 18,2013
Replied on September 13,2016

You can also configure it as follows.

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
joined on
September 13,2016
Replied on September 14,2016

That was it. Thanks for pointing it out.

Write Answer









Copyright ©2017 concretepage.com, all rights reserved |Privacy Policy | Contact Us