This is related to this issue:
https://issues.openmrs.org/browse/RESTWS-523
I created simple controller test as shown below.
@Test
public void create_shouldAcceptAndStoreClobDataViaPost() throws Exception{
long before = getAllCount();
System.out.println(before);
File file = new File("omod-1.9/src/test/resources/formResourcefile.txt");
Assert.assertTrue(file.exists());
MockMultipartFile toUpload = new MockMultipartFile("value", "formresource.txt", "text/plain",
OpenmrsUtil.getFileAsBytes(file));
MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
request.setRequestURI(getBaseRestURI()+getURI());
request.setMethod(RequestMethod.POST.name());
request.setContentType("multipart/form-data");
request.addHeader("Content-Type", "multipart/form-data");
request.addFile(toUpload);
MockHttpServletResponse response = handle(request);
}
Below is the controller handling the request.
@Controller
@RequestMapping(value = "/rest/" + RestConstants.VERSION_1 + "/clobdata")
public class ClobDatatypeStorageController extends BaseRestController{
@Autowired
private RestService restService;
@RequestMapping(method = RequestMethod.POST, headers = {"Content-Type=multipart/form-data"})
@ResponseBody
public Object create(@RequestBody MultipartFile file, HttpServletRequest request, HttpServletResponse response)
throws IOException{
DatatypeService ds = Context.getService(DatatypeService.class);
RequestContext context = RestUtil.getRequestContext(request, response);
DelegatingCrudResource<ClobDatatypeStorage> res =
(DelegatingCrudResource) restService.getResourceByName(buildResourceName("clobdata"));
Object created = res.create(file, context);
return RestUtil.created(response, created);
}
}
When I run the test I get an error org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'multipart/form-data' not supported
. See the complete stack trace [here][2]. It seems the code does not even get to the point of calling the associated controller before throwing the error. On debugging I saw something more interesting. Although the error says ‘multipart/form-data’ not supported, the list of supported media includes it!! (See a screenshot of debugger details below)
I noticed rest module has not been designed to handle files and may be this could be the problem. Otherwise what am I doing wrong? Is there a better way to implement this ticket?
[1]: [2]: org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'multip - Pastebin.com