I have a method in my controller, look like this:
@RequestMapping(value = VideoSvcApi.VIDEO_DATA_PATH, method = RequestMethod.POST)
public @ResponseBody
VideoStatus setVideoData(
@PathVariable(VideoSvcApi.ID_PARAMETER) long id,
@RequestParam(value = VideoSvcApi.DATA_PARAMETER) MultipartFile videoData,
HttpServletResponse response) {
Video video = null;
for (Video v : videos) {
if (v.getId() == id) {
video = v;
break;
}
}
if (video == null) {
throw new VideoNotFoundException(id);
} else {
try {
videoFileManager.saveVideoData(video,
videoData.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
and the custom exception look like this:
@ResponseStatus(value = HttpStatus.NOT_FOUND)
private class VideoNotFoundException extends RuntimeException {
private static final long serialVersionUID = 1L;
public VideoNotFoundException(long id) {
super("Video with id " + id + " not found");
}
}
when I hit some path with an id that does not exists, the response is like this:
{
"timestamp":1407263672355,
"error":"Not Found",
"status":404,
"message":""
}
my question is... how can I set a custom message in the response, but manteining the rest of the json structure?
I know that I can use the "reason" attribute in the annotation (in the custom exception), but doing this I always will return the same message, and I want to display a message like: "Video with id X not found"
Thanks!