1 changed files with 61 additions and 0 deletions
@ -0,0 +1,61 @@ |
|||||
|
package cn.chjyj.szwh.controller; |
||||
|
|
||||
|
import cn.chjyj.szwh.exception.ChException; |
||||
|
import com.alibaba.fastjson2.JSONObject; |
||||
|
import org.apache.commons.logging.Log; |
||||
|
import org.apache.commons.logging.LogFactory; |
||||
|
import org.springframework.web.bind.annotation.ControllerAdvice; |
||||
|
import org.springframework.web.bind.annotation.ExceptionHandler; |
||||
|
import org.springframework.web.bind.annotation.ResponseBody; |
||||
|
|
||||
|
/** |
||||
|
* 全局异常控制器 |
||||
|
*/ |
||||
|
@ControllerAdvice |
||||
|
public class GlobalErrorController { |
||||
|
//日志初始化
|
||||
|
private static final Log log = LogFactory.getLog(GlobalErrorController.class); |
||||
|
|
||||
|
private JSONObject jsonObject=new JSONObject(); |
||||
|
/** |
||||
|
* 异常信息输出 |
||||
|
* @return |
||||
|
*/ |
||||
|
@ExceptionHandler(ChException.class) |
||||
|
@ResponseBody |
||||
|
public JSONObject aisHandler(ChException ais){ |
||||
|
log.error("Exception:"+ais); |
||||
|
jsonObject.put("code",500); |
||||
|
jsonObject.put("msg",ais.getMsg()); |
||||
|
return jsonObject; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 空指针异常 |
||||
|
* @param e |
||||
|
* @return |
||||
|
*/ |
||||
|
@ExceptionHandler(value =NullPointerException.class) |
||||
|
@ResponseBody |
||||
|
public JSONObject exceptionHandler(NullPointerException e){ |
||||
|
log.error("空指针异常:"+e.getMessage()); |
||||
|
String msg = e.getMessage()==null?"module error!":e.getMessage(); |
||||
|
jsonObject.put("code",500); |
||||
|
jsonObject.put("msg",msg); |
||||
|
return jsonObject; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 常规异常信息 |
||||
|
* @param ex |
||||
|
* @return |
||||
|
*/ |
||||
|
@ExceptionHandler(value = Exception.class) |
||||
|
@ResponseBody |
||||
|
public JSONObject exceptionHandler(Exception ex){ |
||||
|
log.error("异常信息:"+ex); |
||||
|
jsonObject.put("code",500); |
||||
|
jsonObject.put("msg",ex.getMessage()); |
||||
|
return jsonObject; |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue