import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
/**
* 全局处理异常
*/
@ControllerAdvice
public class GlobalExceptionHandler {
private Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);
@ExceptionHandler(Exception.class)
@ResponseBody
// @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public String globalExceptionCatchHandler(HttpServletRequest request,Exception e){
logger.error("出现全局异常了"+e.getMessage(),e.fillInStackTrace());
e.printStackTrace();
//TODO 记录日志
String method = request.getMethod();
String contextPath = request.getContextPath();
Map<String, String[]> parameterMap = request.getParameterMap();
String parameterJson = JSON.toJSONString(parameterMap);
logger.error("time:"+System.currentTimeMillis());
logger.error("method:"+method);
logger.error("contextPath:"+contextPath);
logger.error("parameterJson:"+parameterJson);
Map<String,Object> map = Maps.newHashMap();
map.put("code","e");
map.put("msg","请求异常,请联系....");
return JSON.toJSONString(map);
}
}
全局处理异常
最新推荐文章于 2020-07-11 05:42:02 发布