×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Java
Posted by: digas
Added: Sep 26, 2014 3:11 PM
Views: 25
Tags: spring
  1. @Controller
  2. @RequestMapping(value="/example")
  3. public class ExampleController {
  4.  
  5.         @RequestMapping(value = "", method = RequestMethod.POST)
  6.         @ResponseBody
  7.         public void example() throws Exception{
  8.                 System.out.println("in the example function");
  9.                 throw new Exception("a new Exception");
  10.         }
  11.  
  12.         @ExceptionHandler(Exception.class)
  13.         @ResponseBody
  14.         public Map<String,String> errorResponse(Exception ex, HttpServletResponse response){
  15.                 Map<String,String> errorMap = new HashMap<String,String>();
  16.                 errorMap.put("errorMessage",ex.getMessage());
  17.  
  18.                 StringWriter sw = new StringWriter();
  19.                 PrintWriter pw = new PrintWriter(sw);
  20.                 ex.printStackTrace(pw);
  21.                 String stackTrace = sw.toString();
  22.  
  23.                 errorMap.put("errorStackTrace", stackTrace);
  24.                 response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
  25.  
  26.                 return errorMap;
  27.  
  28.         }
  29. }
  30.