I have the below controller method which is uploading files to my server. I would like to return JSON for Plupload's status. However the response seems to be sent back as a dispatch rather than a json @ResponseBody. Any Ideas?
private static final String RESP_SUCCESS = "{\"jsonrpc\" : \"2.0\", \"result\" : \"success\", \"id\" : \"id\"}";
private static final String RESP_ERROR = "{\"jsonrpc\" : \"2.0\", \"error\" : {\"code\": 101, \"message\": \"Failed to upload file.\"}, \"id\" : \"id\"}";
@RequestMapping(method = RequestMethod.POST)
public String uploadItem(@RequestBody MultipartFile file,
@RequestParam String name,
@RequestParam(required = false, defaultValue = "-1") int chunks,
@RequestParam(required = false, defaultValue = "-1") int chunk) {
Media media = new Media();
try {
Path path = Paths.get("/Users/username/Desktop/Test", file.getOriginalFilename());
media.setContentType(file.getContentType());
media.setFileName(file.getOriginalFilename());
media.setFileSize(file.getSize());
media.setFilePath(path.toString());
if (media.getContentType().contains("image")) {
Image image = new Image();
image.setImagePath(path.toString());
imageDao.save(image);
}
byte[] bytes = file.getBytes();
Files.write(path, bytes, StandardOpenOption.CREATE);
mediaDao.save(media);
return RESP_SUCCESS;
} catch (IOException e) {
e.printStackTrace();
}
return RESP_ERROR;
}
}
Throws the following error:
WARN - No mapping found for HTTP request with URI [/{"jsonrpc" : "2.0", "result" : "success", "id" : "id"}] in DispatcherServlet with name 'cr'
I think your method should be annotated with @ResponseBody.
@RequestMapping(method = RequestMethod.POST)
@ResponseBody
public String uploadItem(@RequestBody MultipartFile file,
@RequestParam String name,
@RequestParam(required = false, defaultValue = "-1") int chunks,
@RequestParam(required = false, defaultValue = "-1")
This is how Spring checks for Json conversion:
Spring will handle the JSON conversion automatically.