Restful style of API designing broaden my horizon by standardizing the rule for API.
POST
must be something new into server, which means creating a new resource.
PUT
Must be updating existing resources in server.
DELETE
Must delete a exist resource in server, if not so, ignore its operation.
GET
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:
MultipartResovler
Extends CommonsMultipartResolver to enable multipart resolvation other than POST.
In springmvc application context file, add the last class we created:
1 2 3 4 5 6 7 8 9
@Bean //bean name must be exactly "multipartResolver" public MultipartResolver multipartResolver() { ExtendedMultipartResolveremr=newExtendedMultipartResolver(); //notice this parameter is also important to configure cmr.setMaxUploadSize(9999999); return emr; }
JAR
Because we introduced CommonsMultipartResolver, which uses 2 apache jars, we also need to import them in pom file:
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!