I'm using RestTemplate on the client and Spring MVC/Jackson on the server-side to stream JSON to the client. However I'm wondering how I can inform the client that a server-side exception occurred?
Can I use the @ExceptionHandler annotation on a method to stream back a simple JSON string and let RestTemplate handle it? I guess not since RestTemplate expects an Object type (eg. Collection<Station>). It will fail to marshall/unmarshall it.
Code:@RequestMapping(value = "/station/{code}") @ResponseBody public Collection<Station> validateStation(@PathVariable String code) { return getService().retrieveStations(Arrays.asList(new String[] { code })); }I considered injecting an errorHandler into the RestTemplate, but how could this object be of any help as I want to stream the JSON directly to the AJAX client and I'm not in the proper context?Code:@ExceptionHandler(ServiceException.class) public void handleException(ServiceException e, HttpServletResponse r) { try { r.setContentType("application/json"); final String json = new JSONObject().put("error", true).put("errorMessage", e.getMessage()).toString(); IOUtils.streamBack(json, r); } catch (Exception e1) { log.error(e1.getMessage(), e); } }
I'm really stuck on this although it might be pretty straightforward ;-).
Your input is much appreciated.


Reply With Quote
