SpringBoot 封装统一API返回格式对象 标准化开发 请求封装 统一格式处理

统一HTTP请求代码

javascript 复制代码
public class HttpCode
{
    /**
     * 操作成功
     */
    public static final int SUCCESS = 200;

    /**
     * 对象创建成功
     */
    public static final int CREATED = 201;

    /**
     * 请求已经被接受
     */
    public static final int ACCEPTED = 202;

    /**
     * 操作已经执行成功,但是没有返回数据
     */
    public static final int NO_CONTENT = 204;

    /**
     * 资源已被移除
     */
    public static final int MOVED_PERM = 301;

    /**
     * 重定向
     */
    public static final int SEE_OTHER = 303;

    /**
     * 资源没有被修改
     */
    public static final int NOT_MODIFIED = 304;

    /**
     * 参数列表错误(缺少,格式不匹配)
     */
    public static final int BAD_REQUEST = 400;

    /**
     * 未授权
     */
    public static final int UNAUTHORIZED = 401;

    /**
     * 访问受限,授权过期
     */
    public static final int FORBIDDEN = 403;

    /**
     * 资源,服务未找到
     */
    public static final int NOT_FOUND = 404;

    /**
     * 不允许的http方法
     */
    public static final int BAD_METHOD = 405;

    /**
     * 资源冲突,或者资源被锁
     */
    public static final int CONFLICT = 409;

    /**
     * 不支持的数据,媒体类型
     */
    public static final int UNSUPPORTED_TYPE = 415;

    /**
     * 系统内部错误
     */
    public static final int ERROR = 500;

    /**
     * 接口未实现
     */
    public static final int NOT_IMPLEMENTED = 501;

    /**
     * 系统警告消息
     */
    public static final int WARN = 601;
}

统一接口返回

javascript 复制代码
public class R   extends HashMap<String, Object> {

    /** 状态码 */
    private static final String CODE = "code";

    /** 返回内容 */
    private static final String MSG = "msg";

    /** 数据对象 */
    private static final String DATA = "data";

    private static final String TOTAL="total";

    /**全参构造*/
    public R(int code, String msg, Object data)
    {
        super.put(CODE, code);
        super.put(MSG, msg);
        super.put(DATA, data);
    }


    /**分页构造*/
    public R(int code, String msg, Object data,int total)
    {
        super.put(CODE, code);
        super.put(MSG, msg);
        super.put(DATA, data);
        super.put(TOTAL, total);
    }


    /**
     * 返回成功消息
     * @param msg 返回内容
     * @param data 数据对象
     * @return 成功消息
     */
    public static R success(String msg, Object data)
    {
        return new R(HttpCode.SUCCESS, msg, data);
    }


    /**
     * 返回失败消息
     * @param msg 返回内容
     * @param data 数据对象
     * @return 成功消息
     */
    public static R error(String msg, Object data)
    {
        return new R(HttpCode.ERROR, msg, data);
    }

    /**
     * 返回警告消息
     * @param msg
     * @param data
     * @return
     */
    public static R warn(String msg, Object data)
    {
        return new R(HttpCode.WARN, msg, data);
    }


    /**
     * 分页获取参数
     * @param data
     * @param total
     * @return 分页数据
     */
    public static R page(Object data,int total){
        return new R(200,"获取成功",data,total);
    }

}

接口测试

javascript 复制代码
@RequestMapping("/user")
public R user() {
    List<Integer> list =new ArrayList<>();
    list.add(12);
    list.add(134);
    R result = R.page(list,500);
    result.put("abc",123);
    return result;
}
javascript 复制代码
{"msg":"获取成功","total":500,"code":200,"abc":123,"data":[12,134]}
相关推荐
南风lof4 分钟前
源码赏析:Java线程池中的那些细节
java·源码阅读
pengyu5 分钟前
【Java设计原则与模式之系统化精讲:零】 | 编程世界的道与术(理论篇)
java·后端·设计模式
Aurora_NeAr6 分钟前
深入浅出Docker
后端
程序员岳焱11 分钟前
16.Java Annotation注解:元数据与代码增强
java·后端·编程语言
瀚海澜生13 分钟前
redis系列(2)——AOF日志和RDB快照
后端
保持学习ing29 分钟前
黑马Java面试笔记之 集合篇(算法复杂度+ArrayList+LinkedList)
java·笔记·算法·面试
Htht11136 分钟前
【Qt】之【Get√】【Bug】通过值捕获(或 const 引用捕获)传进 lambda,会默认复制成 const
数据库·bug
想带你从多云到转晴1 小时前
02. java: 类与对象
java·开发语言
酷爱码1 小时前
Java -jar命令运行外部依赖JAR包的深度场景分析与实践指南
java·python·jar
hstar95271 小时前
三十三、面向对象底层逻辑-SpringMVC九大组件之HandlerExceptionResolver接口设计
java·spring·设计模式·架构