SpringBoot服务中自定义异常

本文介绍了一种基于枚举类的异常处理机制,包括定义异常枚举类、异常结果响应类及自定义异常类,并展示了如何配置控制层异常拦截,最后通过一个简单的控制器进行测试。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

1.定义异常枚举类

2.定义异常结果响应类

3.自定义异常类 

4.配置控制层异常拦截

5.测试


1.定义异常枚举类

说明:枚举类用来存放会出现异常信息,方便服务中多次使用和快速查找(下面为示例)

package com.hhmt.delivery.utils.exception;

/**
 * 辉煌明天
 * FileName: ErrorCode
 * Author:   huachun
 * email: huachun_w@163.com
 * Date:     2021/11/22 17:13
 * Description: 错误码
 */
public enum ErrorCode {

    PARAMS_NOT_FOUND("CPA.0010001", "Important parameters cannot be found!"),

    REQ_PARAMS_EXCEPTION("CPA.0010011", "Request parameter exception!"),
    RES_EXCEPTION("CPA.0010012", "In response to abnormal!");


    private String code;
    private String msg;

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    ErrorCode(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }
}

2.定义异常结果响应类

异常信息响应类,可以通过构造器赋值

package com.hhmt.delivery.utils;

import com.hhmt.delivery.utils.exception.ServiceCustomerException;

/**
 * 辉煌明天
 * FileName: ErrorInfo
 * Author:   huachun
 * email: huachun_w@163.com
 * Date:     2021/12/1 19:58
 * Description: ErrorInfo
 */
public class ErrorInfo {
    private String code;
    private String msg;

    public String getCode() {
        return code;
    }


    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public ErrorInfo(ServiceCustomerException exception) {
        this.code = exception.getCode();
        this.msg = exception.getMsg();
    }
}

3.自定义异常类 

package com.hhmt.delivery.utils.exception;

import lombok.Data;

/**
 * 辉煌明天
 * FileName: ServiceCustomerException
 * Author:   huachun
 * email: huachun_w@163.com
 * Date:     2021/12/2 15:28
 * Description:
 */
@Data
public class ServiceCustomerException extends RuntimeException {

    private String code;
    private String msg;

    public ServiceCustomerException(ErrorCode errorCode) {
        super();
        this.code = errorCode.getCode();
        this.msg = errorCode.getMsg();
    }


}

4.配置控制层异常拦截

配置拦截器拦截服务自定义异常

package com.hhmt.delivery.handler;

import com.hhmt.delivery.utils.ErrorInfo;
import com.hhmt.delivery.utils.exception.ServiceCustomerException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

/**
 * 辉煌明天
 * FileName: ControllerAdviceProcessor
 * Author:   huachun
 * email: huachun_w@163.com
 * Date:     2021/12/2 15:22
 * Description:
 */

@RestControllerAdvice
public class ControllerAdviceProcessor {

    @ExceptionHandler(ServiceCustomerException.class)
    public ErrorInfo handleException(HttpServletRequest request, ServiceCustomerException ex) {
        return new ErrorInfo(ex);
    }

}

5.测试

创建一个简单的控制器

package com.hhmt.delivery.test;

import com.hhmt.delivery.utils.exception.ErrorCode;
import com.hhmt.delivery.utils.exception.ServiceCustomerException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 辉煌明天
 * FileName: TestExceptionController
 * Author:   huachun
 * email: huachun_w@163.com
 * Date:     2021/12/20 14:28
 * Description:
 */

@RestController
@RequestMapping(value = "/test-ex")
public class TestExceptionController {

    @GetMapping(value = "/ex-01")
    public <T> T testEx() {

        try {
            System.out.println(1 / 0);
        } catch (Exception exception) {
            System.out.println(exception);
            throw new ServiceCustomerException(ErrorCode.RES_EXCEPTION);
        }

        return null;
    }


}

通过postman访问可以看到就会返回枚举类信息

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值