实现一个简单的全局异常处理

项目地址

开发中一般常常需要使用try catch来处理异常,使用全局异常,只需要在项目开发初期设置一次,之后就都不需要手动处理异常

初期准备

使用Spring脚手架快速构建一个SpringBoot的Web项目
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

代码编写

准备一个返回给前端的类

public class MyResponse {
    private boolean ret;
    private String detail;
    private Object content;

    public boolean isRet() {
        return ret;
    }

    public void setRet(boolean ret) {
        this.ret = ret;
    }

    public String getDetail() {
        return detail;
    }

    public void setDetail(String detail) {
        this.detail = detail;
    }

    public Object getContent() {
        return content;
    }

    public void setContent(Object content) {
        this.content = content;
    }

    public static MyResponse getErrorResp(){
        MyResponse resp = new MyResponse();
        resp.setRet(false);
        resp.setDetail("file");
        return resp;
    }

    public static MyResponse getErrorResp(String detail){
        MyResponse resp = new MyResponse();
        resp.setRet(false);
        resp.setDetail(detail);
        return resp;
    }

    public static MyResponse getSuccess(){
        MyResponse resp = new MyResponse();
        resp.setRet(true);
        resp.setDetail("success");
        return resp;
    }

    public static MyResponse getSuccess(Object obj){
        MyResponse resp = new MyResponse();
        resp.setRet(true);
        resp.setDetail("success");
        resp.setContent(obj);
        return resp;
    }
}

关键,全局异常处理类

@RestControllerAdvice
public class MyException {
    private static Logger logger = LoggerFactory.getLogger(MyException.class);

    @ExceptionHandler(value = Exception.class)
    public MyResponse exceptionHandler(Exception e){
        logger.error("错误原因:",e);
        return MyResponse.getErrorResp(e.getMessage());
    }
}

编写Controller和Service层,进行简单测试

controller层

@RestController
public class HomePageController {

    @Autowired
    private GetUser getUser;

    @GetMapping("user/{id}")
    public MyResponse getUser(@PathVariable String id, @RequestBody Map<String,Object> map) throws Exception {
        String username=String.valueOf(map.get("username"));
        return getUser.invoke(username,id);
    }
}

service层

@Service
public class GetUser {
    public MyResponse invoke(String username,String id) throws Exception {
        if(!username.equals("admin")){
            throw new Exception("登录用户不是管理员");
        }
        if(!id.equals("1")){
            throw new Exception("不存在此用户");
        }
        User user = new User();
        user.setId(id);
        user.setUsername("张三");
        return MyResponse.getSuccess(user);
    }
}

万能的实体类User

public class User {
    private String id;
    private String username;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }
}

启动项目,开始测试

在这里插入图片描述
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值