I am using Spring MVC to expose some RESTful web services. One of the operations calls for a RESTful PUT operation when a form is submitted.
However, the form is no ordinary form in that it contains a file input along with regular inputs such as text and checkboxes.
I have configured Spring to work with RESTful PUT and DELETE by adding the HiddenHttpMethodFilter
in the web.xml
. In my forms, i have a hidden _method
parameter being sent as well.
All this works fine for DELETE, PUT without file upload, etc. When I try to do a PUT with file upload and form data, it gives me a 405
.
HTTP Status 405 - Request method 'POST' not supported
My controller method looks like this:
@RequestMapping(value = "/admin/cars/{carId}", method = PUT, headers = "content-type=multipart/form-data")
public String updateCar(@PathVariable("carId") String carId, CarForm form) {
// save and return the right view.
}
My HTML form looks like this:
<form action="<c:url value='/admin/cars/${car.id}'/>" method="POST" enctype="multipart/form-data">
<input type="hidden" name="_method" value="PUT" />
<input type="text" name="carName" value="${car.name}" />
<input type="file" name="photo" />
<input type="submit" />
</form>
Is what I am trying to achieve doable at all using PUT? If so, how to make Spring MVC understand that?
Add MultipartFilter
to enable file uploads before HiddenHttpMethodFilter
in your web.xml (as written in HiddenHttpMethodFilter
API docs, see NOTE).
Also:
Note: This filter is an alternative to using
DispatcherServlet
'sMultipartResolver
support, for example for web applications with custom web views which do not use Spring's web MVC, or for custom filters applied before a Spring MVCDispatcherServlet
(e.g.HiddenHttpMethodFilter
). In any case, this filter should not be combined with servlet-specific multipart resolution.
(from MF's docs, emphasis mine)
Also, MultipartResolver
's bean name must be filterMultipartResolver
in order to MultipartFilter run properly (or must be set via <init-param>
).
EDIT:
As I expected in my last comment, there's an issue with (Actually, The method in the isMultipartContent comes in as POST even though it is a PUT as the MultipartFilter is declared before the HiddenHttpMethodFilter. as OP notes.) Below is extednded class with slighlty modified static method it uses originally:CommonsMultipartResolver
which supports only POST method by default.
public class PutAwareCommonsMultipartResolver extends CommonsMultipartResolver {
private static final String MULTIPART = "multipart/";
@Override
public boolean isMultipart(HttpServletRequest request) {
return request != null && isMultipartContent(request);
}
/**
* Utility method that determines whether the request contains multipart
* content.
*
* @param request The servlet request to be evaluated. Must be non-null.
*
* @return <code>true</code> if the request is multipart; {@code false}
* otherwise.
*
* @see ServletFileUpload#isMultipartContent(HttpServletRequest)
*/
public static final boolean isMultipartContent(HttpServletRequest request) {
final String method = request.getMethod().toLowerCase();
if (!method.equals("post") && !method.equals("put")) {
return false;
}
String contentType = request.getContentType();
if (contentType == null) {
return false;
}
if (contentType.toLowerCase().startsWith(MULTIPART)) {
return true;
}
return false;
}
}