springboot 统一异常处理

public class GlobalErrException extends Exception{


    private IErrorInfo iErrorInfo;


    public GlobalErrException(IErrorInfo info) {
        iErrorInfo = info;
    }


    public IErrorInfo getiErrorInfo() {
        return iErrorInfo;
    }


    public void setiErrorInfo(IErrorInfo iErrorInfo) {
        this.iErrorInfo = iErrorInfo;
    }
}






package com.zte.daas.common.exception;




import com.zte.daas.common.exception.errorinfo.IErrorInfo;
import com.zte.daas.common.responseobj.ResultBody;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;


import javax.servlet.http.HttpServletRequest;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;




@RestControllerAdvice
public class GlobalErrExceptionHandle {
    private static final Logger log = LoggerFactory.getLogger(GlobalErrExceptionHandle.class);






    @ExceptionHandler(value = GlobalErrException.class)
    public ResultBody busiErrorHandlerOverJson(HttpServletRequest request,
                                           GlobalErrException exception) {
        IErrorInfo errorInfo = exception.getiErrorInfo();
        log.error("busiErrorHandlerOverJson:{}:{}",errorInfo.getCode(),errorInfo.getMgs());
        ResultBody result = new ResultBody(errorInfo);
        return result;
    }






    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public ResultBody validErrorHandlerOverJson(HttpServletRequest request,
                                                MethodArgumentNotValidException exception) {
        BindingResult result=  exception.getBindingResult();
        Map resultMap= new HashMap();
        List<String> resultList = new ArrayList();
        for (ObjectError objectError : result.getAllErrors())
        {
            resultMap= new HashMap();
            // result.
            String code = objectError.getCodes()[1];


            resultList.add(code.substring(code.indexOf(".") + 1)+":"+objectError.getDefaultMessage());


        }




        ResultBody rsp = new ResultBody("8888",String.join("\n",resultList));
        return rsp;
    }


    @ExceptionHandler(value = Exception.class)
    public ResultBody sysErrorHandlerOverJson(HttpServletRequest request,
                                              Exception exception) {
        log.error("Exception:",exception);
        ResultBody result = new ResultBody("9999",exception.getMessage());
        return result;
    }
}






package com.zte.daas.common.exception.errorinfo;




public interface IErrorInfo {


    public String getCode();


    public String getMgs() ;
}






package com.zte.daas.webapp.exception;


import com.zte.daas.common.config.SpringContextUtil;
import com.zte.daas.common.exception.errorinfo.IErrorInfo;
import com.zte.daas.common.locale.LocaleMessageSourceBean;




public enum BussinessErrorInfo implements IErrorInfo {


    REGIST_NOT_EXIST("200001","bussinesserror.rigistnotexist"),




    ;


    private String code;
    private String mgs;


    BussinessErrorInfo(String code,String mgs){
        this.code = code;
        this.mgs = mgs;
    }


    @Override
    public String getCode() {
        return code;
    }


    @Override
    public String getMgs() {


        LocaleMessageSourceBean lmb=(LocaleMessageSourceBean) SpringContextUtil.getBean("localeResolver");
        return lmb.getMessage(mgs);
    }

}


统一异常处理后,事物 回滚的处理“

@Transactional(rollbackFor = Exception.class)”




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值