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
MultipartFilterto enable file uploads beforeHiddenHttpMethodFilterin your web.xml (as written inHiddenHttpMethodFilterAPI docs, see NOTE).Also:
(from MF’s docs, emphasis mine)
Also,
MultipartResolver‘s bean name must befilterMultipartResolverin 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:CommonsMultipartResolverwhich supports only POST method by default.