Java设计RestCodeEnum枚举,实现接口统一格式返回

1.接口返回值枚举定义

    /**
     *接口返回枚举定义
     */
    public enum RestCodeEnum {
    SUCCESS("200", "处理成功"),
    ERROR("500", "系统异常");

    private String code;
    private String msg;

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

    public String getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }
 }

2.异常信息抛出返回值定义

public class RestException extends RuntimeException {
    private static final long serialVersionUID = -649224303322031383L;

    private RestCodeEnum retCode;

    public RestException() {
        super();
    }

    public RestException(RestCodeEnum retCode) {
        super(retCode.getMsg());
        this.retCode = retCode;
    }

    public RestException(RestCodeEnum retCode, String msg) {
        super(msg);
        this.retCode = retCode;
    }

    public RestCodeEnum getRetCode() {
        return this.retCode;
    }

    public void setRetCode(RestCodeEnum retCode) {
        this.retCode = retCode;
    }
}

3.接口统一返回参数定义

@Slf4j
public abstract class BaseController {
    public static final String CODE = "code";
    public static final String MSG = "msg";
    public static final String DATA = "data";

    protected static String success() {
        final Map<String, Object> result = Maps.newHashMap();
        result.put(CODE, RestCodeEnum.SUCCESS.getCode());
        result.put(MSG, RestCodeEnum.SUCCESS.getMsg());
        return JSON.toJSONString(result);
    }

    protected static String success(Object data) {
        final Map<String, Object> result = Maps.newHashMap();
        result.put(CODE, RestCodeEnum.SUCCESS.getCode());
        result.put(MSG, RestCodeEnum.SUCCESS.getMsg());
        result.put(DATA, data);
        return JSON.toJSONString(result);
    }

    protected static String error(RestCodeEnum code) {
        final Map<String, Object> result = Maps.newHashMap();
        result.put(CODE, code.getCode());
        result.put(MSG, code.getMsg());
        return JSON.toJSONString(result);
    }

    protected static String error(RestCodeEnum code, String msg) {
        final Map<String, Object> result = Maps.newHashMap();
        result.put(CODE, code.getCode());
        result.put(MSG, msg);
        return JSON.toJSONString(result);
    }
}

4.增加全局异常

/**
 * 全局异常处理器
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler extends BaseController {
    /**
     * 基础异常
     */
    @ExceptionHandler(RestException.class)
    public String baseException(RestException e) {
        return error(e.getRetCode(), e.getMessage());
    }
}

5.实例

/**
 * 直接使用error、success 需要继承 BaseController
 */
@RestController
@RequestMapping("/dataProcessing")
public class DataProcessingController extends BaseController {

    @Resource
    private JdbcTemplate jdbcTemplate;

    @PostMapping("/deleteSource")
    public String deleteSource(@RequestBody JSONObject body) {
        String sqlCode = body.getString("sqlCode");
        if (StringUtils.isBlank(sqlCode)) {
            throw new RestException(RestCodeEnum.ERROR);//异常信息直接抛出
        }
        if (jdbcTemplate.update("UPDATE `data_sql` SET `status` = '0' where `sql_code` = '" + sqlCode + "';") > 0) {
            return success();//成功信息返回
        }
        return error(RestCodeEnum.ERROR);//错误信息返回
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值