WebExceptionHandler.java 1.0 KB

12345678910111213141516171819202122232425262728
  1. package com.steerinfo.dil.config;
  2. import com.steerinfo.framework.controller.RESTfulResult;
  3. import org.apache.log4j.Logger;
  4. import org.springframework.web.bind.annotation.ControllerAdvice;
  5. import org.springframework.web.bind.annotation.ExceptionHandler;
  6. import org.springframework.web.bind.annotation.ResponseBody;
  7. import java.text.ParseException;
  8. @ControllerAdvice
  9. public class WebExceptionHandler {
  10. static final Logger log = Logger.getLogger(WebExceptionHandler.class);
  11. @ExceptionHandler(value =Exception.class)
  12. @ResponseBody
  13. public RESTfulResult exceptionHandler(Exception e){
  14. log.error("全局异常捕获:"+e);
  15. e.printStackTrace();
  16. if(e instanceof NullPointerException){
  17. return new RESTfulResult("500", "操作失败:缺乏必要参数!", e);
  18. }else if(e instanceof ParseException){
  19. return new RESTfulResult("500", "操作失败:格式转换异常!", e);
  20. }
  21. return new RESTfulResult("500", "操作失败:"+e.getMessage(), e);
  22. }
  23. }