springboot异常拦截器
mvc异常拦截器
package com.eaos.util.exception;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
@Configuration
public class GolbalException implements HandlerExceptionResolver {
@Override
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
Exception ex) {
ModelAndView mv = new ModelAndView();
mv.addObject("errorInfo", "全局异常处理" + ex.getMessage());
System.out.println(ex.getMessage());
System.out.print("异常栈:");
return mv;
}
}
前后端分离拦截器
package com.eaos.util.exception;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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 com.alibaba.fastjson.JSON;
@ControllerAdvice
@ResponseBody
public class GlobalExceptionHandler {
private static Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);
@ExceptionHandler(value = Exception.class)
public void allExceptionHandel(HttpServletRequest request,HttpServletResponse response, Exception e) throws IOException {
logger.error(e.getMessage(), e);
System.out.println("系统异常:"+e.getMessage());
response.setContentType("text/html;charset=utf-8");
response.setCharacterEncoding("utf-8");
PrintWriter writer = response.getWriter();
Map<String, Object> map = new HashMap<>();
map.put("code", "505");
map.put("msg", "系统异常");
writer.write(JSON.toJSONString(map));
writer.flush();
writer.close();
}
}
全局异常处理类
package com.videos.video.common.exception;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.videos.video.common.result.Result;
import com.videos.video.common.result.ResultEnum;
import com.videos.video.common.result.ResultUtil;
import com.videos.video.common.util.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import javax.security.auth.login.LoginException;
import java.util.List;
@Slf4j
@RestControllerAdvice
public class ExceptionConfig {
@ExceptionHandler(MissingServletRequestParameterException.class)
public Result<String> parameterMissingExceptionHandler(MissingServletRequestParameterException e) {
log.error("忽略参数异常:{},{}", e.getMessage(),e);
return ResultUtil.failure(ResultEnum.PARAM_ERROR,e.getMessage());
}
@ExceptionHandler(HttpMessageNotReadableException.class)
public Result<String> parameterBodyMissingExceptionHandler(HttpMessageNotReadableException e) {
log.error("缺少请求体异常:{},{}", e.getMessage(),e);
return ResultUtil.failure(ResultEnum.PARAM_ERROR,e.getMessage());
}
@ExceptionHandler(MethodArgumentNotValidException.class)
public Result<String> parameterExceptionHandler(MethodArgumentNotValidException e) {
log.error("参数效验异常处理器:{},{}", e.getMessage(),e);
BindingResult exceptions = e.getBindingResult();
if (exceptions.hasErrors()) {
List<ObjectError> errors = exceptions.getAllErrors();
if (!errors.isEmpty()) {
FieldError fieldError = (FieldError) errors.get(0);
return ResultUtil.failureMsg(fieldError.getDefaultMessage());
}
}
return ResultUtil.failure(ResultEnum.PARAM_ERROR,e.getMessage());
}
@ExceptionHandler({BusinessException.class})
public Result<String> paramExceptionHandler(BusinessException e) {
log.error("自定义异常:{},{}", e.getMessage(),e);
if (!StringUtils.isBlank(e.getMessage())) {
return ResultUtil.failure(ResultEnum.PARAM_ERROR.setMsg(e.getMessage()));
}
return ResultUtil.failure(ResultEnum.PARAM_ERROR);
}
@ExceptionHandler({Exception.class})
public Result<String> otherExceptionHandler(Exception e) {
log.error("其他异常:{},{}", e.getMessage(),e);
if (!StringUtils.isEmpty(e.getMessage())) {
return ResultUtil.failure(ResultEnum.SERVICE_FAILURE.setMsg(e.getMessage()));
}
return ResultUtil.failure(ResultEnum.SERVICE_FAILURE);
}
@ExceptionHandler(value = WxPayException.class)
public Result<String> hanlerException(WxPayException wxPayException) {
log.error("微信异常:{},{}", wxPayException.getMessage(),wxPayException);
return ResultUtil.failure(ResultEnum.WX_PAY_EXCEPTION.setMsg(wxPayException.getReturnMsg()));
}
@ExceptionHandler(value = LoginException.class)
public Result<String> loginException(LoginException loginException) {
log.error("登录异常:{},{}", loginException.getMessage(),loginException);
return ResultUtil.failure(ResultEnum.LOGIN_FAILURE_200402.setMsg(loginException.getMessage()));
}
}