通用返回类Result
前言:Java项目搭建时,常常需要去封装一个通用型的Result工具类,下面就是我自己封装的常用的返回类,可以直接使用。(有部分Swagger注解,使用时可忽略)
第一步、创建ReusltUtils工具类
- package com.code.walker.utils;
- import com.code.walker.constant.ResultCode;
- import io.swagger.annotations.ApiModel;
- import io.swagger.annotations.ApiModelProperty;
- import lombok.*;
- import lombok.experimental.Accessors;
- import java.io.Serializable;
- /**
- * @author ProsperousEnding-fhl
- * @create 2023-07-20-15:16
- */
- @Getter
- @Setter
- @NoArgsConstructor
- @AllArgsConstructor
- @Accessors(chain = true)
- @ApiModel(value = "响应信息体")
- public class ResultUtils<T> implements Serializable {
- /**
- * 响应码
- */
- @Getter
- @Setter
- @ApiModelProperty(value = "响应标记:成功标记=0,失败1")
- private Integer code;
- /**
- * 响应信息
- */
- @Getter
- @Setter
- @ApiModelProperty(value = "响应信息")
- private String message;
- /**
- * 响应数据
- */
- @Getter
- @Setter
- @ApiModelProperty(value = "响应数据")
- private T data;
- @Getter
- @Setter
- @ApiModelProperty(value = "返回状态")
- private boolean status;
- private ResultUtils(ResultCode resultCode,T data,boolean status){
- this.code = resultCode.getCode();
- this.message = resultCode.getMessage();
- this.data = data;
- this.status=status;
- }
- /**
- * 无数据成功返回
- *
- * @return
- */
- public static <T>ResultUtils success(){
- return new ResultUtils<T>(ResultCode.SUCCESS.getCode(),ResultCode.SUCCESS.getMessage(),null,true);
- }
- /**
- * 带数据返回
- */
- public static <T> ResultUtils success(T data){
- return new ResultUtils<T>(ResultCode.SUCCESS.getCode(),ResultCode.SUCCESS.getMessage(),data,true);
- }
- /**
- * 失败
- */
- public static <T>ResultUtils fail(){
- return new ResultUtils<T>(ResultCode.FAIL.getCode(),ResultCode.FAIL.getMessage(), null,false);
- }
- /**
- * 失败
- */
- public static <T> ResultUtils fail(T data){
- return new ResultUtils<T>(ResultCode.FAIL.getCode(),ResultCode.FAIL.getMessage(), data,false);
- }
- @Override
- public String toString() {
- return "ResultUtils [code=" + code + ", message=" + message + ", data=" + data + "]";
- }
- }
复制代码 第二步 创建常用的报错信息类(在日常开发中可不写,将ReusltUtils中的ResultCode的代码换成常量即可)
注:主要是为了美观以及修改方便,所以去单独的封装一个常量信息类
- /**
- * @author ProsperousEnding-fhl
- * @create 2023-07-20-15:46
- */
- @Getter
- public enum ResultCode {
- /**
- * 成功
- */
- SUCCESS(200, "成功"),
- FAIL(1000, "失败"),
- FAILED(400, "请求失败"),
- NOT_FOUND(404, "未找到"),
- SERVER_ERROR(500, " 服务器内部出错 "),
- /**
- * 错误参数
- */
- PARAM_IS_INVALID(1001, "参数无效"),
- PARAM_IS_BLANK(1002, "参为空"),
- PARAM_TYPE_ERROR(1003, "参数类型错误"),
- PARAM_NOT_COMPLETE(1004, "参数缺失"),
- /**
- * 用户错误
- */
- USER_NOT_LOGIN_IN(2001, "用户未登录"),
- USER_LOGIN_ERROR(2002, "账号不存在或者密码错误"),
- USER_ACCOUNT_FORBIDDEN(2003, "账户被禁用"),
- USER_NOT_EXISTS(2004, "用户不存在"),
- USER_HAS_EXISTED(2005, "用户已存在");
- /**
- * 代码
- */
- private final Integer code;
- /**
- * 信息
- */
- private final String message;
- private ResultCode(Integer code, String message) {
- this.code = code;
- this.message = message;
- }
- }
复制代码 第三步、调用方法以及返回样式
使用时可以直接使用ResultUtils.方法的方式- public Result getUser() {
- User user1=new User();
- user1.setName("codeTalker")
- return Result.success(user1);
- }
复制代码- {
- "code": 200,
- "message": "成功",
- "data":{
- "name":"codeTalker"
- }
- "status": true
- }
复制代码 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |