Hi, I am trying to use @ResponseBody to facilitate marshaling of RESTful services (see code snippet below for example). However, I want to make sure that when an exception is encountered that the response is still in the requested format (JSON, XML, etc.). It appears that the default behavior renders a standard HTML error page.
Is there a recommended way to accomplish this? Since I haven't been able to find anything I have just written an aspect that catches any unhandled exceptions and returns our RestResponse class (see below). However, that results in a 200 response code which is obviously incorrect.
I feel like I'm probably headed down the wrong path here so thought I'd better ask to see if anyone has a recommendation about a better way to solve this problem.
Thanks,
Scott
Controller Method:
Aspect:Code:@RequestMapping(value="{listId}", method = RequestMethod.GET) public @ResponseBody RestResponse getDatabase(@PathVariable("listId") Integer listId) { return new RestResponse(listOfLists.get(listId)); }
Code:@Pointcut("execution(samples.mvc.ajax.account.RestResponse *(..))") public void anyRestMethodExecution() {} @Around("anyRestMethodExecution()") public Object handleRestErrors(ProceedingJoinPoint pjp) { try { return pjp.proceed(); } catch (Throwable e) { return new RestResponse(RestResponse.StatusEnum.ERROR, e.getMessage()); } }


Reply With Quote
