003-SpringSecurity-Demo 统一响应类

SpringSecurity-Demo 统一响应类

前言

实施

代码已推送至 Gitee 仓库:springdecurity-demo

完成后的项目截图如下:

  1. domain中新建 Result 实体类
  2. Result 实体类内容如下:
java 复制代码
package com.junjiu.springboot3.domain;

import lombok.Data;

import java.io.Serializable;

/**
 * program: security-demo
 * ClassName: Result
 * description:
 *
 * @author: 君九
 * @create: 2026-04-02 21:15
 * @version: 1.0
 **/
@Data
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 状态码:200成功,500失败
     */
    private int code;

    /**
     * 消息
     */
    private String msg;

    /**
     * 数据
     */
    private T data;

    /**
     * 无参构造
     */
    public Result() {
    }

    /**
     * 全参构造
     */
    public Result(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    // ====================== 成功返回 ======================
    public static <T> Result<T> success() {
        return new Result<>(200, "操作成功", null);
    }

    public static <T> Result<T> success(T data) {
        return new Result<>(200, "操作成功", data);
    }

    public static <T> Result<T> success(String msg, T data) {
        return new Result<>(200, msg, data);
    }

    public static <T> Result<T> success(int code, String msg) {
        return new Result<>(code, msg, null);
    }

    // ====================== 失败返回 ======================
    public static <T> Result<T> error() {
        return new Result<>(500, "操作失败", null);
    }

    public static <T> Result<T> error(String msg) {
        return new Result<>(500, msg, null);
    }

    public static <T> Result<T> error(int code, String msg) {
        return new Result<>(code, msg, null);
    }

    public static <T> Result<T> error(String msg, T data) {
        return new Result<>(500, msg, data);
    }
}
  1. 功能验证
    完善 HelloController 控制类
  2. 打开浏览器,访问请求,进行功能验证。
相关推荐
To_OC1 天前
LC 128 最长连续序列:别上来就排序,O (n) 解法才是这题的灵魂
javascript·算法·leetcode
kyriewen1 天前
我用 50 行代码重写了 React Router 核心,终于搞懂了前端路由原理
前端·javascript·react.js
人活一口气1 天前
Spring Boot与AIGC的完美结合:从零搭建智能内容生成平台
java·spring boot·aigc
Asize1 天前
HTML5 Canvas 基础:从按帧动画到 ECharts 数据可视化
前端·javascript·canvas
像我这样帅的人丶你还1 天前
Java 后端详解(三):全局异常处理与 JPA 数据库映射
java·后端
默_笙1 天前
🎄 后端给我一堆扁平数据,我 10 行代码把它变成了树
前端·javascript
前端Hardy1 天前
又一个 AI 神器火了!
前端·javascript·后端
NE_STOP1 天前
vibe Coding -- 小项目实战
java
PBitW1 天前
GPT训练我的第二天,我表示不过如此!!!😕😕😕
前端·javascript·面试
kyriewen2 天前
白宫直接给 OpenAI 下了限制令,GPT-5.6 不能随便放出来了
前端·javascript·面试