[项目经验]——Springboot优雅返回结果

本文介绍了如何在前后端分离的项目中通过封装ResultBean类来简化返回结果的处理。文章详细展示了ResultBean的构造方法、状态码枚举类HttpStatus的定义以及在Controller中的使用方式,提供了成功和失败返回结果的示例,旨在提高代码的可读性和复用性。此外,还提出可以通过BaseController进行进一步的抽象,以提高控制器的代码简洁度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文为若依项目学习笔记

前后端分离项目中,需要返回结果来进行交互,如果我们每次都要自己写一堆返回的逻辑,无疑过于麻烦,于是这里我们可以将返回结果进行封装,方便我们使用

封装返回结果


public class ResultBean extends HashMap<String, Object>
{
    private static final long serialVersionUID = 1L;

    /** 状态码 */
    public static final String CODE_TAG = "code";

    /** 返回内容 */
    public static final String MSG_TAG = "msg";

    /** 数据对象 */
    public static final String DATA_TAG = "data";

    /**
     * 初始化一个新创建的 ResultBean 对象,使其表示一个空消息。
     */
    public ResultBean()
    {
    }

    /**
     * 初始化一个新创建的 ResultBean 对象
     *
     * @param code 状态码
     * @param msg 返回内容
     */
    public ResultBean(int code, String msg)
    {
        super.put(CODE_TAG, code);
        super.put(MSG_TAG, msg);
    }

    /**
     * 初始化一个新创建的 ResultBean 对象
     *
     * @param code 状态码
     * @param msg 返回内容
     * @param data 数据对象
     */
    public ResultBean(int code, String msg, Object data)
    {
        super.put(CODE_TAG, code);
        super.put(MSG_TAG, msg);
        if (StringUtils.isNotNull(data))
        {
            super.put(DATA_TAG, data);
        }
    }

    /**
     * 返回成功消息
     *
     * @return 成功消息
     */
    public static ResultBean success()
    {
        return ResultBean.success("操作成功");
    }

    /**
     * 返回成功数据
     *
     * @return 成功消息
     */
    public static ResultBean success(Object data)
    {
        return ResultBean.success("操作成功", data);
    }

    /**
     * 返回成功消息
     *
     * @param msg 返回内容
     * @return 成功消息
     */
    public static ResultBean success(String msg)
    {
        return ResultBean.success(msg, null);
    }

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

    /**
     * 返回错误消息
     *
     * @return
     */
    public static ResultBean error()
    {
        return ResultBean.error("操作失败");
    }

    /**
     * 返回错误消息
     *
     * @param msg 返回内容
     * @return 警告消息
     */
    public static ResultBean error(String msg)
    {
        return ResultBean.error(msg, null);
    }

    /**
     * 返回错误消息
     *
     * @param msg 返回内容
     * @param data 数据对象
     * @return 警告消息
     */
    public static ResultBean error(String msg, Object data)
    {
        return new ResultBean(HttpStatus.ERROR, msg, data);
    }

    /**
     * 返回错误消息
     *
     * @param code 状态码
     * @param msg 返回内容
     * @return 警告消息
     */
    public static ResultBean error(int code, String msg)
    {
        return new ResultBean(code, msg, null);
    }
}

返回状态码

package com.qhd.constant;


/**
 * 返回状态码
 *
 */
public class HttpStatus
{
    /**
     * 操作成功
     */
    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;
}

controller中调用

1、成功
  • 不带返回结果
   public ResultBean login(){
    
        return ResultBean.success();
    }
  • 带返回结果的
public ResultBean login(){
    ResultBean res = ResultBean.success();
    res.put("token",123456);
    return res;
}
2、失败
  • 什么都不带的
 public ResultBean login(){
    
         return ResultBean.error();
    }
  • 带错误消息的
 public ResultBean login(){
    
         return ResultBean.error("出错啦");
    }
  • 带状态码的
 public ResultBean login(){
    	//可以自己写
        // return ResultBean.error(500,"出错啦");
        //或者使用我们封装好的状态码,这样使用起来更加规范
       return ResultBean.error(HttpStatus.NOT_FOUND,"出错啦")
    }
使用技巧

为了使用起来更加方便,我们还可以让controller继承一个BaseController

/**
 * web层通用数据处理
 *
 */
public class BaseController {
    /**
     * 响应返回结果
     *
     * @param rows 影响行数
     * @return 操作结果
     */
    protected ResultBean toResult(int rows)
    {
        return rows > 0 ? ResultBean.success() : ResultBean.error();
    }

    /**
     * 响应返回结果
     *
     * @param result 结果
     * @return 操作结果
     */
    protected ResultBean toResult(boolean result)
    {
        return result ? success() : error();
    }

    /**
     * 返回成功
     */
    public ResultBean success()
    {
        return ResultBean.success();
    }

    /**
     * 返回失败消息
     */
    public ResultBean error()
    {
        return ResultBean.error();
    }

    /**
     * 返回成功消息
     */
    public ResultBean success(String message)
    {
        return ResultBean.success(message);
    }

    /**
     * 返回失败消息
     */
    public ResultBean error(String message)
    {
        return ResultBean.error(message);
    }


}

然后我们就可以这样使用

public class UserController extends BaseController {
    //成功
	 public ResultBean test1(){
       return success();
    }
    //失败
	 public ResultBean test2(){
       return error();
    }
    
    //根据数据库返回的操作后影响的行数来返回是否成功
     public ResultBean test3(){
       int rows = userService.test();
       return toResult(rows);
    }
    
}

到此我们就会发现返回结果是如此简单

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一定会去到彩虹海的麦当

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值