@Controller @RequestMapping(value="/example") public class ExampleController { @RequestMapping(value = "", method = RequestMethod.POST) @ResponseBody public void example() throws Exception{ System.out.println("in the example function"); throw new Exception("a new Exception"); } @ExceptionHandler(Exception.class) @ResponseBody public Map errorResponse(Exception ex, HttpServletResponse response){ Map errorMap = new HashMap(); errorMap.put("errorMessage",ex.getMessage()); StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); ex.printStackTrace(pw); String stackTrace = sw.toString(); errorMap.put("errorStackTrace", stackTrace); response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value()); return errorMap; } }