• 接口统一响应


    统一接口响应
    定义状态接口
    package com.smile.cms.utils.response;
    
    /**
     * 相应的状态接口
     */
    public interface IErrorCode {
        /**
         * 返回状态码
         */
        int getCode();
    
        /**
         * 返回提示信息
         */
        String getMessage();
    }
    
    
    定义枚举类型
    package com.smile.cms.utils.response;
    
    public enum ResponseCode implements IErrorCode{
        SUCCESS(200,"成功"),
    
        ERROR(400,"失败"),
    
        UNAUTHORIZED(401,"身份验证失败"),
    
        VALIDATE_FAILED(402,"参数校验异常"),
    
        FORBIDDEN(403,"无权限访问"),
    
        NOT_FOUND(404,"路由未找到"),
    
        METHOD_NOT_ALLOWED (405,"请求方法不支持"),
    
        HTTP_TOO_MANY_REQUESTS(429,"接口频率限制"),
    
        HTTP_INTERNAL_SERVER_ERROR(500,"系统错误");
    
        /**
         * 状态码
         */
        private int code;
    
        /**
         * 提示消息
         */
        private String message;
    
        private ResponseCode(int code,String message)
        {
            this.code = code;
            this.message = message;
        }
    
        @Override
        public int getCode() {
            return code;
        }
    
        @Override
        public String getMessage() {
            return message;
        }
    }
    
    接口响应数据
    package com.smile.cms.utils.response;
    
    /**
     * 接口相应
     * @param <T>
     */
    public class ApiResponse <T>{
        private int code;
    
        private String message;
    
        private T data;
    
        private ApiResponse() {}
    
        public ApiResponse(T data,int code,String message)
        {
            this.data = data;
    
            this.code = code;
    
            this.message = message;
        }
    
        public long getCode() {
            return code;
        }
    
        public void setCode(int code) {
            this.code = code;
        }
    
        public String getMessage() {
            return message;
        }
    
        public void setMessage(String message) {
            this.message = message;
        }
    
        public T getData() {
            return data;
        }
    
        public void setData(T data) {
            this.data = data;
        }
    
        public static <T> ApiResponse<T> success()
        {
            return new ApiResponse<T>(null,ResponseCode.SUCCESS.getCode(), ResponseCode.SUCCESS.getMessage());
        }
    
        public static <T> ApiResponse<T> success(String message)
        {
            return new ApiResponse<T>(null,ResponseCode.SUCCESS.getCode(), message);
        }
    
        public static <T> ApiResponse<T> success(String message,int code)
        {
            return new ApiResponse<T>(null,code, message);
        }
    
        public static <T> ApiResponse<T> success(String message,int code,T data)
        {
            return new ApiResponse<T>(data,code, message);
        }
    
        public static <T> ApiResponse<T> error()
        {
            return new ApiResponse<T>(null,ResponseCode.ERROR.getCode(), ResponseCode.SUCCESS.getMessage());
        }
    
        public static <T> ApiResponse<T> error(String message)
        {
            return new ApiResponse<T>(null,ResponseCode.ERROR.getCode(), message);
        }
    
        public static <T> ApiResponse<T> error(String message,int code)
        {
            return new ApiResponse<T>(null,code, message);
        }
    
        public static <T> ApiResponse<T> error(String message,int code,T data)
        {
            return new ApiResponse<T>(data,code, message);
        }
    }
    
    
  • 相关阅读:
    C++字符串(srtring)反转
    字典(Dictionary)
    畅通工程
    子串计算
    神奇的口袋
    SLT 优先队列 哈弗曼树最小带权路径
    大数阶乘
    整数拆分
    A+B (带有,的数字)
    Hdu 1232 畅通工程
  • 原文地址:https://www.cnblogs.com/ywjcqq/p/16084537.html
Copyright © 2020-2023  润新知