Andremoniy Andremoniy - 1 year ago 286
Java Question

How to PUT multipart/form-data using Spring MockMvc?

I have controller's method with

method, which receives multipart/form-data:

@RequestMapping(value = "/putIn", method = RequestMethod.PUT)
public Foo updateFoo(HttpServletRequest request,
@RequestBody Foo foo,
@RequestParam("foo_icon") MultipartFile file) {

and I want to test it using
. Unfortunately
creates essentially instance of
which has

super(HttpMethod.POST, urlTemplate, urlVariables)

Surely I can I can not create my own implementation of
, say

public MockPutMultipartHttpServletRequestBuilder(String urlTemplate, Object... urlVariables) {
super(HttpMethod.PUT, urlTemplate, urlVariables);

has package-local constructor.

But I'm wondering is any more convenient Is any way to do this, may be I missed some existent class or method for doing it?

Answer Source

This is unfortunately currently not supported in Spring MVC Test, and I don't see a work-around other than creating your own custom MockPutMultipartHttpServletRequestBuilder and copying-n-pasting code from the standard implementation.

For what it's worth, Spring MVC also does not support PUT requests for file uploads by default either. The Multipart resolvers are hard coded to accept only POST requests for file uploads -- both for Apache Commons and the standard Servlet API support.

If you would like Spring to support PUT requests in addition, feel free to open a ticket in Spring's JIRA issue tracker.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download