SpringBoot统一响应模板

一、主要功能

  1. 响应消息统一
  2. 异常信息统一处理和自定义异常

二、前置条件

三、代码示例

1. ApiResult 类

响应模板类,作为前后端数据响应的模板,包括成功状态,响应码,响应消息,响应数据构成。

public class ApiResult<T> {
    private static final String SUCCESS_CODE = "200";
    private static final String FAIL_CODE = "500";

    private Boolean success;
    private String code;
    private String message;
    private T data;

    public ApiResult() {}
    
    public ApiResult(Boolean success, String code) {
        this.success = success;
        this.code = code;
    }
    
    public ApiResult(Boolean success, String code, String message) {
        this(success, code);
        this.message = message;
    }

    public ApiResult(Boolean success, String code, String message, T data) {
        this(success, code, message);
        this.data = data;
    }

    public static ApiResult<String> fail() {
        return new ApiResult<>(false, FAIL_CODE, "fail");
    }

    public static ApiResult<String> fail(String message) {
        return new ApiResult<>(false, FAIL_CODE, message);
    }

    public static ApiResult<String> fail(String code, String message) {
        return new ApiResult<>(false, code, message);
    }

    public static <T> ApiResult<T> success(T data) {
        return new ApiResult<T>(true, SUCCESS_CODE, "success", data);
    }

    public static ApiResult<String> success() {
        return new ApiResult<>(true, SUCCESS_CODE, "success");
    }
}

2. ApiException 类

异常处理类,继承RuntimeException,作为程序逻辑异常后的处理

public class ApiException extends RuntimeException {
    private static final String DEFAULT_FAIL_CODE = "555";

    private String errorCode = DEFAULT_FAIL_CODE;

    public ApiException() {
    }

    public ApiException(String message) {
        super(message);
    }

    public ApiException(String errorCode, String message) {
        super(message);
        this.errorCode = errorCode;
    }

    public ApiException(String message, Throwable cause, String errorCode) {
        super(message, cause);
        this.errorCode = errorCode;
    }

    public ApiException(Throwable cause, String errorCode) {
        super(cause);
        this.errorCode = errorCode;
    }

    public ApiException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace, String errorCode) {
        super(message, cause, enableSuppression, writableStackTrace);
        this.errorCode = errorCode;
    }

    public String getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(String errorCode) {
        this.errorCode = errorCode;
    }
}

3. ApiExceptionHandler类

实现注解接口@ControllerAdvice,对程序中的异常的处理,返回给前端信息。

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.multipart.MaxUploadSizeExceededException;

@ControllerAdvice
public class ApiExceptionHandler {

    @ExceptionHandler(ApiException.class)
    @ResponseBody
    public ApiResult<String> apiExceptionHandler(ApiException e) {
        return ApiResult.fail(e.getErrorCode(), e.getMessage());
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseBody
    public ApiResult<String> methodExceptionHandler(MethodArgumentNotValidException e) {
        return ApiResult.fail(e.getMessage());
    }

    @ExceptionHandler(IllegalArgumentException.class)
    @ResponseBody
    public ApiResult<String> illegalExceptionHandler(IllegalArgumentException e) {
        return ApiResult.fail(e.getMessage());
    }

    @ExceptionHandler(MaxUploadSizeExceededException.class)
    @ResponseBody
    public ApiResult<String> uploadExceptionHandler(MaxUploadSizeExceededException e) {
        return ApiResult.fail(e.getMessage());
    }

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ApiResult<String> otherExceptionHandler(Exception e) {
        return ApiResult.fail(e.getMessage());
    }
}


本人萌新一枚,才疏学浅,如有不足之处和错误的地方,还望各位大神指出。
欢迎大家评论讨论,一起进步。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值