java封装全局异常处理类

1.当我们在写业务代码的时候,经常会用到异常处理,但是如果我们每次都用try catch来处理业务逻辑的话,就会看起来非常繁琐,代码冗余,不处理的话后台会报错,这时候就需要封装一下,然后处理代码的时候一行代码就可以返回给前台。

异常处理

@RestControllerAdvice 这个注解的作用是拦截异常并统一处理

在spring 3.2中,新增了@ControllerAdvice 注解,可以用于定义@ExceptionHandler、@InitBinder、@ModelAttribute,并应用到所有@RequestMapping中。

1.创建 RRExceptionHandler,并添加 @ControllerAdvice注解。

/**

* @program: xiaowu

* @description: 异常处理

* @author: Wu

* @create: 2020-08-28 10:34

**/

@RestControllerAdvice

public class RRExceptionHandler {

private Loggerlogger = LoggerFactory.getLogger(getClass());

    /**

    * 处理自定义异常

    */

    @ExceptionHandler(RRException.class)

public RhandleRRException(RRException e){

R r =new R();

        r.put("code", e.getCode());

        r.put("msg", e.getMessage());

        return r;

    }

@ExceptionHandler(NoHandlerFoundException.class)

public RhandlerNoFoundException(Exception e) {

logger.error(e.getMessage(), e);

        return R.error(404, "路径不存在,请检查路径是否正确");

    }

@ExceptionHandler(DuplicateKeyException.class)

public RhandleDuplicateKeyException(DuplicateKeyException e){

logger.error(e.getMessage(), e);

        return R.error("数据库中已存在该记录");

    }

@ExceptionHandler(AuthorizationException.class)

public RhandleAuthorizationException(AuthorizationException e){

logger.error(e.getMessage(), e);

        return R.error("没有权限,请联系管理员授权");

    }

@ExceptionHandler(Exception.class)

public RhandleException(Exception e){

logger.error(e.getMessage(), e);

        return R.error();

    }

}

2.创建RRException类,并继承RuntimeException类,并编写一些构造方法

package com.example.xiaowu.exception;

public class RRExceptionextends RuntimeException{

private static final long serialVersionUID =1L;

    private Stringmsg;

    private int code =500;

    public RRException(String msg) {

super(msg);

        this.msg = msg;

    }

public RRException(String msg, Throwable e) {

super(msg, e);

        this.msg = msg;

    }

public RRException(String msg, int code) {

super(msg);

        this.msg = msg;

        this.code = code;

    }

public RRException(String msg, int code, Throwable e) {

super(msg, e);

        this.msg = msg;

        this.code = code;

    }

public StringgetMsg() {

return msg;

    }

public void setMsg(String msg) {

this.msg = msg;

    }

public int getCode() {

return code;

    }

public void setCode(int code) {

this.code = code;

    }

}

接口返回的实体封装  R

package com.example.xiaowu.utils;

import org.apache.http.HttpStatus;

import java.util.HashMap;

import java.util.Map;

/**

* @program: xiaowu

* @description: 返回数据

* @author: Wu

* @create: 2020-08-28 10:39

**/

public class Rextends HashMap {

private static final long serialVersionUID =1L;

    public R() {

put("code", 0);

        put("msg", "success");

    }

public static Rerror() {

return error(HttpStatus.SC_INTERNAL_SERVER_ERROR, "未知异常,请联系管理员");

    }

public static Rerror(String msg) {

return error(HttpStatus.SC_INTERNAL_SERVER_ERROR, msg);

    }

public static Rerror(int code, String msg) {

R r =new R();

        r.put("code", code);

        r.put("msg", msg);

        return r;

    }

public static Rok(String msg) {

R r =new R();

        r.put("msg", msg);

        return r;

    }

public static Rok(Map map) {

R r =new R();

        r.putAll(map);

        return r;

    }

public static Rok() {

return new R();

    }

public Rput(String key, Object value) {

super.put(key, value);

return this;

    }

}

接口测试

测试

返回值

返回值

跟前端约定好code,就可以友好操作了

                              

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值