SpringBoot defines an elegant global unified Restful API response framework four

At present, we seem to have solved all the problems and achieved our ideal effect as follows

But it is not ideal when the business error returns as follows

There is no need to return  reuqestanderrorMsg

Also, if the business is very complicated, will there be confusion and duplication of error code allocation and use? How to avoid and solve this problem?

At this time, I thought of using business error constants instead of error codes, which is more literate and further abstracts the public interface module of error constants

package cn.soboys.springbootrestfulapi.common.error;

import java.util.List;

/**
 * @author 公众号 程序员三时
 * @version 1.0
 * @date 2023/5/9 20:14
 * @webSite https://github.com/coder-amiao
 * 定义错误常量 代替错误码,避免业务复杂错误码分配重复等问题
 */
public interface CommonErrorConstant {
    /**
     * 公共错误码定义
     */
    public static final String InvalidRequest = "InvalidRequest";
    public static final String InvalidArgument = "InvalidArgument";
    public static final String NotFound = "NotFound";
    public static final String UnknownError = "UnknownError";
    public static final String OK = "OK";
    public static final String FAIL = "FAIL";



    /**
     * 其他自定义业务错误码
     */



}

General error code

package cn.soboys.springbootrestfulapi.common.error;

import cn.soboys.springbootrestfulapi.common.resp.ResultCode;

/**
 * @author 公众号 程序员三时
 * @version 1.0
 * @date 2023/5/2 21:36
 * @webSite https://github.com/coder-amiao
 */
public enum CommonErrorCode implements ResultCode {

    /**
     * 错误请求
     */
    INVALID_REQUEST(false, CommonErrorConstant.InvalidRequest, "Invalid request, for reason: "),
    /**
     * 参数验证错误
     */
    INVALID_ARGUMENT(false, CommonErrorConstant.InvalidArgument, "Validation failed for argument "),
    /**
     * 未找到资源
     */
    NOT_FOUND(false, CommonErrorConstant.NotFound, "Resource  not found."),
    /**
     * 未知错误
     */
    UNKNOWN_ERROR(false, CommonErrorConstant.UnknownError, "Unknown server internal error.");


    CommonErrorCode(Boolean success, String code, String message) {
        this.success = success;
        this.code = code;
        this.message = message;

    }

    /**
     * 响应是否成功
     */
    private Boolean success;
    /**
     * 响应状态码
     */
    private String code;
    /**
     * 响应信息
     */
    private String message;


    @Override
    public String getCode() {
        return code;
    }

    @Override
    public String getMessage() {
        return message;
    }

    @Override
    public boolean getSuccess() {
        return success;
    }


}

normal request

bad request

  1. Adjusted Business Exceptions

  2. Tuning Unknown Error Exception

The corresponding code has been uploaded and updated on github

Let me leave a question for thinking, if the interface error needs to be internationalized, how should it be implemented?

Finally: If you don’t want to experience the feeling of not being able to find information when learning, no one answering questions, and giving up after persisting for a few days, here I will share with you some learning resources for automated testing, hoping to give you some guidance on the way forward. Come to help, friends can get it for free if they need it 【保证100%免费】

Collection of software testing interview questions

Our advanced study of automated testing must be to find a high-paying job. The following interview questions are the latest interview materials from first-line Internet companies such as Ali, Tencent, and Byte, and some Byte bosses have given authoritative answers. After completing this set of interview materials, I believe everyone can find a satisfactory job.

How to get the video file:

Guess you like

Origin blog.csdn.net/m0_75277660/article/details/130625725