Restful style of API designing broaden my horizon by standardizing the rule for API.
must be something new into server, which means creating a new resource.
Must be updating existing resources in server.
Must delete a exist resource in server, if not so, ignore its operation.
Obviously, it is read only operation.
But most of the uploading APIs are designed to use POST, even in springmvc’s org.springframework.test.web.servlet.request.MockMvcRequestBuilders.fileUpload, it use POST method only. I think this is not a good practise, especially for a person who want to insist on using Restful style.
Other than using POST, what we could do to achieve this is like below, idea is part from SOF:
Extends CommonsMultipartResolver to enable multipart resolvation other than POST.
In springmvc application context file, add the last class we created:
Because we introduced CommonsMultipartResolver, which uses 2 apache jars, we also need to import them in pom file:
Now seems all configuration are done, time to start coding for unit test:
This should be pretty satisfying, because we not only stick to the Restful style, but also upload file successfully.
Notice the boundary parameter in content type is mandatory. For people do not know what boundary is, here are some posts.
Hope this post could help someone!