Spring Cloud实体校验和异常统一处理

采用@ControllerAdvice+@ExceptionHandler方式的全局的异常处理

1.先定义一个全局的异常类,继承Exception ,里面有code属性,表示请求的响应状态码

/**
  * @ClassName GlobalException
  * @Description 全局异常 
  **/
 public class GlobalException extends Exception implements java.io.Serializable{
    private int code;
    public void setCode(int code) {
       this.code = code;
    }
    public int getCode() {
       retun code;
    }
    public GlobalException(GlobalException e) {
         super(e.getMessage());
         this.code = e.getCode();
     }
    public GlobalException(String message) {
         super(message);
     }
    public GlobalException(String message, int code) {
         super(message);
         this.code = code;
     }
 }

2.定义一个全局异常处理Handler类,使用自己的统一返回类返回

import com.sany.customException.GlobalException;
import com.sany.utils.ResultVoUtil;
import com.sany.vo.ResultVo;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@ControllerAdvice
public class GlobalExceptionHandler {

 /**
 * 参数校验统一返回
 * @param e
 * @return
 */
 @ExceptionHandler(MethodArgumentNotValidException.class)
 @ResponseBody
 public ResultVo handler(MethodArgumentNotValidException e){
 //简化错误提示
     StringBuffer msg = new StringBuffer();
     List<FieldError> fieldErrors = e.getBindingResult().getFieldErrors();
     for (FieldError fieldError : fieldErrors) {
     msg.append(fieldError.getDefaultMessage()+" ");
     }
 return ResultVoUtil.error(e.hashCode(),msg.toString(),null);
 }

 /**
 * 自定义异常处理 一个Controller下多个@ExceptionHandler上的异常类型不能出现一样的,否则运行时抛异常.
 * @param e
 * @return
 * @throws Exception
 */
 @ExceptionHandler(GlobalException.class)
 @ResponseBody
 public ResultVo jsonErrorHandler(HttpServletRequest req, GlobalException e) throws Exception {
     ResultVo r = new ResultVo();
     r.setMsg(e.getMessage());
     r.setCode(e.getCode());
     r.setData(null);
     return r;
 }
 /**
 * 系统异常处理
 * @param req
 * @param e
 * @return
 * @throws Exception
 */
 @ExceptionHandler(Exception.class)
 @ResponseBody
 public ResultVo defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception {
     ResultVo r = new ResultVo();
     r.setMsg(e.getMessage());
     r.setCode(e.hashCode());
     r.setData(null);
     return r;
 }
}

3.通常情况下,全局Handler我们会放在公共模块,子服务启动加载时可能加载不到。最简单的方法是在启动类使用@ComponentScan指定包名(别忘记添加启动类路径,这个注解扫描时自己的项目就不会被扫描到,所以要把自己项目的包路径写进来所以上面扫描了两个路径。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cc_南柯一梦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值