springboot整合之统一结果返回

导读:本篇文章讲解 springboot整合之统一结果返回,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

特别说明:本次项目整合基于idea进行的,如果使用Eclipse可能操作会略有不同,不过总的来说不影响。

springboot整合之如何选择版本及项目搭建

springboot整合之版本号统一管理 

springboot整合mybatis-plus+durid数据库连接池

springboot整合swagger

springboot整合mybatis代码快速生成

springboot整合之统一结果返回

springboot整合之统一异常处理

springboot整合之Validated参数校验 

springboot整合之logback日志配置

springboot整合pagehelper分页

springboot整合本地缓存

springboot整合redis + redisson

springboot整合elasticsearch

springboot整合rabbitMq

springboot整合canal实现缓存一致性

springboot整合springSecurity(前后端不分离版本)

一、为什么要对结果进行统一的返回

可能有很多小伙伴不理解为什么我们要对结果进行统一的返回,我们之前那种直接返回结果不也挺好的吗?
springboot整合之统一结果返回

类似于上面这种,我们是直接就给接口调用方返回了用户信息,这样也不能说有问题,但是不够优雅。比如说现在用户请求参数错了,我们该怎么返回? 

springboot整合之统一结果返回

 这样以来,接口调用方还要根据内容去判断我们的返回结果,如果说不同的错误返回的信息又是不一样的,比如现在这个返回“当前用户不存在,参数错误”,另一个接口返回“权限不足,请申请权限后操作!”,那这样对接口调用方来说简直就是痛苦面具。

所以为了我们的接口调用返回结果更加优雅,我们可以采取统一返回结果。我们在结果里面加上状态码,消息以及返回的数据。这样接口调用方就能够根据我们的返回状态码来判定结果的状态,然后根据状态去判定显示结果还是显示错误提示。这样就会显得优雅一点。具体实现也很简单。下面我们就一步一步来实现一下。

二、准备需要的目录结构

我们首先在项目里新建下面的几个包,以备后用。简单说明一下:common就是打算以后把公共的类就放到这里面来,enums就是放我们用到的枚举类,response就是放我们的返回结果类,utils存放工具类。

springboot整合之统一结果返回

三、创建统一返回结果类

按照我们上面说的,这个类有三个成员属性,分别是:status用来标记请求状态,msg用来返回消息,如果成功就返回“OK”,失败就返回错误消息提示。data用来返回数据。这里面我还增加了一个常量 SUCCESS,用来表示200,表示请求成功的状态。定义为了public类型,后面方便我们在别的类调用。本来也打算把401,403,404,500这些常见的状态也都定义进来的。但是考虑了一下,打算把常见的错误状态做成枚举,避免大家重复定义,用起来更加简单方便一点。

package com.example.springbootdemo.common.response;

import com.example.springbootdemo.common.enums.ErrorCodeEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * @description: Result 统一返回结果<br>
 * @date: 2022/12/27 0027 下午 1:09 <br>
 * @author: William <br>
 * @version: 1.0 <br>
 */
@Data
@ApiModel(value="统一返回结果对象", description="")
public class Result {

    /**
     *操作成功
     */
    public static final int SUCCESS = 200;

    /**
     * 响应业务状态
     */
    @ApiModelProperty(value = "响应业务状态")
    private Integer status;

    /**
     * 响应消息
     */
    @ApiModelProperty(value = "响应消息")
    private String msg;

    /**
     * 响应中的数据
     */
    @ApiModelProperty(value = "响应中的数据")
    private Object data;



    /**
     * description: Result  默认无参构造<br>
     * @version: 1.0
     * @date: 2022/12/27 0027 下午 1:21
     * @author: William
     */
    public Result() {

    }

    /**
     * description: Result  只包含返回数据构造方法<br>
     * @version: 1.0
     * @date: 2022/12/27 0027 下午 1:22
     * @author: William
     * @param data   需要返回数据
     */
    public Result(Object data) {
        this.status = SUCCESS;
        this.msg = "OK";
        this.data = data;
    }

    /**
     * description: Result 包含所有参数的构造方法<br>
     * @version: 1.0
     * @date: 2022/12/27 0027 下午 1:23
     * @author: William
     * @param status    状态
     * @param msg       消息
     * @param data      数据
     */
    public Result(Integer status, String msg, Object data) {
        this.status = status;
        this.msg = msg;
        this.data = data;
    }

    public static Result ok() {
        return new Result(null);
    }

    public static Result ok(Object data) {
        return new Result(data);
    }

    /**
     * description: error 异常时返回方法<br>
     * @version: 1.0
     * @date: 2022/12/27 0027 下午 1:25
     * @author: William
     * @param errorCodeEnum   异常枚举对象
     * @return com.example.springbootdemo.common.response.Result
     */
    public static Result error(ErrorCodeEnum errorCodeEnum) {
        return new Result(errorCodeEnum.getValue(), errorCodeEnum.getMessage(), "");
    }

    public static Result build(Integer status, String msg) {
        return new Result(status, msg, null);
    }

    public static Result build(Integer status, String msg, Object data) {
        return new Result(status, msg, data);
    }

}

四、创建统一异常枚举类

package com.example.springbootdemo.common.enums;


import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

/**
 * description:  错误信息枚举类规定:编写时按照模块分类,每个模块写到一起,便于修改和维护返回状态根据Http状态码定义来 <br>
 * @version: 1.0
 * @date: 2022/12/27 0027 下午 1:18
 * @author: William
 */
@NoArgsConstructor
@Getter
@AllArgsConstructor
public enum ErrorCodeEnum {

    //公共模块
    UNAUTHORIZED(401,"未认证"),
    FORBIDDEN(401,"未授权"),
    ILLEGAL_VALUE(404,"参数非法"),
    OPERATION_FAILED(404,"操作失败");

    int value;
    String message;
}

五、修改测试接口

到上面第四步完成我们的统一结果返回就算封装好了,接下来我们就能够使用我们自己定义的统一返回结果类了。我们来修改一下userController,把之前的返回字符串改为返回同意结果返回。

这里有个点跟大家讲一下,因为我这就是demo,并不是真正的业务,我就把查询用户放到了controller里面了,如果是正式项目,大家把查询还是要放到业务层service里面去做。

package com.example.springbootdemo.controller;


import cn.hutool.core.bean.BeanUtil;
import com.example.springbootdemo.common.enums.ErrorCodeEnum;
import com.example.springbootdemo.common.response.Result;
import com.example.springbootdemo.entity.User;
import com.example.springbootdemo.service.UserService;
import com.example.springbootdemo.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author William
 * @since 2022-12-27
 */
@Api(tags = "用户相关接口")
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;



    /**
     * description: userInfo 过根据用户ID获取用户信息<br>
     * @version: 1.0
     * @date: 2022/12/27 0027 下午 2:30
     * @author: William
     * @param id   用户ID
     * @return com.example.springbootdemo.common.response.Result
     */
    @ApiOperation(value = "根据用户ID获取用户信息")
    @GetMapping("/{id}")
    public Result userInfo(@PathVariable("id") Integer id){
        if(id == null || id <= 0){
            return Result.error(ErrorCodeEnum.ILLEGAL_VALUE);
        }
        User user = userService.getById(id);
        if(user == null){
            return Result.error(ErrorCodeEnum.ILLEGAL_VALUE);
        }
        return Result.ok(user);
    }

}

六、启动测试

操作完成后我们就可以启动测试了。

springboot整合之统一结果返回

 启动成功,接下来我们打开swagger来测试一把。首先我们输入一个不存在的用户试一下(我这里把前两条数据给删除了,所以输入1和2是不存在的,你们看一下自己的数据库进行测试就好)

springboot整合之统一结果返回

 可以看到当前的结果是

{
  “status”: 404,
  “msg”: “参数非法”,
  “data”: “”
}

这样看起来是不是就舒服多了。然后我们再来看一下正常的

springboot整合之统一结果返回

 怎么样是不是体验感一下就上来了。

七、遗留问题解决

。。。突然返现一个严重的问题,就是大家有没有发现我们竟然把用户密码给返回出来了!!!这岂不是要出大问题。我们在返回用户数据的时候一定要格外的小心,因为一不小心就会发生用户信息泄露的事故。这里我们来改造一下吧。我们现在先假设前端只需要返回id和userName.

因为等下我们需要将用户信息copy到userVo中,所以我们需要用到一个工具类,这里我给大家推荐一个我觉得非常好用的工具类,那就是hutool工具包,相信不少小伙伴都用过。两个字好用!!但是有一个问题就是这个里面工具类非常的多,如果有代码洁癖那么不太建议引入。可以自己按照要求来写就好。像我们经常用到的框架或者中间件,感兴趣的小伙伴可以研究一下源码,你会发现他们基本上都是自己封装工具类,不会引入其他jar包。这样主要是为了框架更加轻量级,不需要引入其他jar包即可使用,还有就是用到什么就封装什么,不会导致项目里面存在没有用的代码。减少代码打包的体积,以及运行的速度。言归正传,我们还是先把我们的问题给解决了。

1.引入hutool工具包

        <hutool.version>5.8.7</hutool.version>   
        <!--hutool工具-->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>${hutool.version}</version>
        </dependency>

2.新建UserVo类

springboot整合之统一结果返回

package com.example.springbootdemo.vo;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

/**
 * @description: UserVo <br>
 * @date: 2022/12/27 0027 下午 2:39 <br>
 * @author: William <br>
 * @version: 1.0 <br>
 */
@Data
@ApiModel(value = "用户信息对象")
public class UserVo implements Serializable {

    /**
     * 用户ID
     */
    @ApiModelProperty(value = "用户ID")
    private Long id;

    /**
     * 用户名
     */
    @ApiModelProperty(value = "用户名")
    private String userName;
}

3.修改测试类

package com.example.springbootdemo.controller;


import cn.hutool.core.bean.BeanUtil;
import com.example.springbootdemo.common.enums.ErrorCodeEnum;
import com.example.springbootdemo.common.response.Result;
import com.example.springbootdemo.entity.User;
import com.example.springbootdemo.service.UserService;
import com.example.springbootdemo.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author William
 * @since 2022-12-27
 */
@Api(tags = "用户相关接口")
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;



    /**
     * description: userInfo 过根据用户ID获取用户信息<br>
     * @version: 1.0
     * @date: 2022/12/27 0027 下午 2:30
     * @author: William
     * @param id   用户ID
     * @return com.example.springbootdemo.common.response.Result
     */
    @ApiOperation(value = "根据用户ID获取用户信息")
    @GetMapping("/{id}")
    public Result userInfo(@PathVariable("id") Integer id){
        if(id == null || id <= 0){
            return Result.error(ErrorCodeEnum.ILLEGAL_VALUE);
        }
        User user = userService.getById(id);
        if(user == null){
            return Result.error(ErrorCodeEnum.ILLEGAL_VALUE);
        }
        UserVo userVo = new UserVo();
        BeanUtil.copyProperties(user, userVo);
        return Result.ok(userVo);
    }

}

OK,我们再来重新启动测试一把。

springboot整合之统一结果返回

 可以看到,现在就只返回了需要的信息。

好了,到这里我们的统一返回结果就OK了。如果文章对你有所帮助的话,点赞关注一波吧~

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由半码博客整理,本文链接:https://www.bmabk.com/index.php/post/96957.html

(0)

相关推荐

半码博客——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!