8.HTTP Method Conversion

As explained before, REST architecture recommends using all methods defined in HTTP, but HTML based on browser supports only two, GET and POST. PUT and DELETE can be used with JavaScript but require a complicated coding process. Or, simply do a POST with the 'real' method as an additional parameter modeled as a hidden input field in an HTML form that defines the actual HTTP Method.

Spring 3 provides HiddenHttpMethodFilter and supports the conversion of HTTP Method by finding hidden input parameter that defines the actual HTTP Method. If you add HiddenHttpMethodFilter in web.xml, HTTP Method is changed to '_method' value when HTTP Method is POST and parameter called '_method' exists. To use parameter name other than '_method', define using methodParam property.

In Spring, <form:form> adds automatically hidden input field that defines the actual HTTP Method, making it more convenient to use.

<form:form method="delete">
    <input type="submit" value="Delete Movie"/>
</form:form>
When the above is written in JSP, internally, "_method=delete" is transferred using POST.

Notes for using HiddenHttpMethodFilter

When using HiddenHttpMethodFilter, note that the previous MultipartResolver configuration for file upload should change because HiddenHttpMethodFilter does not run properly when enctype of form is defined as 'multipart/form-data' for file upload.

Define MultipartFilter before HiddenHttpMethodFilter in web.xml, and set MultipartResolver as 'filterMultipartResolver' Bean name in root Application Context of Spring for HiddenHttpMethodFilter to run properly.

The following is the defining of MultipartFilter and HiddenHttpMethodFilter in web.xml.

<filter>
    <filter-name>multipartFilter</filter-name>
    <filter-class>org.springframework.web.multipart.support.MultipartFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>multipartFilter</filter-name>
    <url-pattern>/springrest/*</url-pattern>
</filter-mapping>
<filter>
    <filter-name>httpMethodFilter</filter-name>
    <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>httpMethodFilter</filter-name>
    <url-pattern>/springrest/*</url-pattern>
</filter-mapping>

The following is MultipartResolver configuration defined in context-springrest-multipart.xml.

<bean id="filterMultipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <property name="maxUploadSize">
        <value>10000000</value>
    </property>
</bean>
When MultipartResolver Bean is defined in a name other than 'filterMultipartResolver', 'multipartResolverBeanName' is set using <init-param> when defining MultipartFilter in web.xml.