Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RESTful PUT with file upload and form data in Spring MVC

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?

like image 504
adarshr Avatar asked Sep 27 '12 08:09

adarshr


People also ask

Is Spring MVC supports only CommonsMultipartResolver for uploading file?

Spring provides file upload support using MultiPartResolver interface and provides two out-of-box implementations for that. 1. To use with Apache Commons . Spring CommonsMultipartResolver is a MultipartResolver implementation for use with Apache Commons FileUpload.

How would you handle the situation where a user uploads a very large file through a form in your Spring MVC application?

A new attribute "maxSwallowSize" is the key to deal this situation. It should happen when you upload a file which is larger than 2M. Because the 2M is the default value of this new attribute .

How do you send the multipart file and JSON data to spring boot?

To pass the Json and Multipart in the POST method we need to mention our content type in the consume part. And we need to pass the given parameter as User and Multipart file. Here, make sure we can pass only String + file not POJO + file. Then convert the String to Json using ObjectMapper in Service layer.


2 Answers

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's MultipartResolver 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 MVC DispatcherServlet (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 CommonsMultipartResolver which supports only POST method by default. (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:

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;
    }

}
like image 78
Xaerxess Avatar answered Nov 06 '22 18:11

Xaerxess


Actually, PUT receives a binary stream and not multipart data. You can use org.springframework.web.filter.HiddenHttpMethodFilter to make it work.

like image 24
Michael-O Avatar answered Nov 06 '22 17:11

Michael-O