I'm using Spring-MVC to build restful service. When serving a POST request using multipart-form, I want to limit the file size posted. Normally the approach is as follow:
<web-app version="3.0"...
//...other code omitted
<servlet>
<servlet-name>restful</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/restful-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<multipart-config>
<max-file-size>500</max-file-size>
</multipart-config>
</servlet>
And my restful-context.xml is like:
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<mvc:annotation-driven>
<mvc:message-converters>
<beans:bean
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
</mvc:message-converters>
</mvc:annotation-driven>
<beans:bean
class="org.springframework.web.multipart.support.StandardServletMultipartResolver"
id="multipartResolver" />
//...
</beans:beans>
Above is what PRO SPRING 4 tells me to do. But the max-file-size does not take effect. I tried posting a very large file(near 1GB), and no exception was thrown. Any one helps?
Later I removed the DispatcherServlet configuration from web.xml, instead I tried the annotaion style using @MultipartConfig, as follow(thanks to @RE350):
@WebServlet(loadOnStartup = 1, name = "restful", urlPatterns = { "/restful/*" }, initParams = { @WebInitParam(name = "contextConfigLocation", value = "/WEB-INF/spring/appServlet/restful-context.xml") })
@MultipartConfig(maxFileSize = 500)
public class MyDispatcherServlet extends DispatcherServlet {}
And it workded!!! So why the web.xml way does not work? Waiting for your answer.