springboot全局异常和参数异常json输出

针对不同业务异常返回不同的json数据

import javax.servlet.http.HttpServletRequest;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;

import com.ghgcn.support.web.BaseController;

import org.jflame.toolkit.common.bean.CallResult;
import org.jflame.toolkit.common.bean.CallResult.ResultEnum;
import org.jflame.toolkit.exception.BusinessException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**

  • 全局异常处理器(只处理json输出)

*/
@ControllerAdvice
public class GlobalExceptionHandler {

private final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
private CallResult<?> error = new CallResult<>(ResultEnum.SERVER_ERROR);

/**
 * 处理参数验证异常
 * 
 * @param request
 * @param e
 * @return
 */
@ExceptionHandler({ BindException.class,MethodArgumentNotValidException.class,ConstraintViolationException.class,
        MissingServletRequestParameterException.class })
@ResponseBody
public CallResult<?> handValidateException(HttpServletRequest request, Exception e) {
    CallResult<?> result = new CallResult<>(ResultEnum.PARAM_ERROR);
    log.warn(e.getMessage());
    if (e instanceof MethodArgumentNotValidException) {
        BaseController.convertError(((MethodArgumentNotValidException) e).getBindingResult(), result);
    } else if (e instanceof BindException) {
        BaseController.convertError(((BindException) e).getBindingResult(), result);
    } else if (e instanceof ConstraintViolationException) {
        ConstraintViolationException cve = (ConstraintViolationException) e.getCause();
        String errMsg = "";
        for (ConstraintViolation<?> cv : cve.getConstraintViolations()) {
            errMsg = errMsg + cv.getMessage() + ";";
        }
        result.setMessage(errMsg);
    }
    return result;
}

@ExceptionHandler({ BusinessException.class })
@ResponseBody
public CallResult<?> handBusinessException(HttpServletRequest request, BusinessException e) {
    log.error("", e);
    if (e.getStatus() != 0) {
        return new CallResult<>(e.getStatus(), e.getMessage());
    }
    return error;
}

@ExceptionHandler({ Exception.class })
@ResponseBody
public CallResult<?> handException(HttpServletRequest request, Exception e) {
    log.error("", e);
    if (e.getCause() instanceof ConstraintViolationException) {
        ConstraintViolationException cve = (ConstraintViolationException) e.getCause();
        String errMsg = "";
        for (ConstraintViolation<?> cv : cve.getConstraintViolations()) {
            errMsg = errMsg + cv.getMessage() + ";";
        }
        return CallResult.paramError(errMsg);
    }
    return error;
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值