Watch 1 Star 2 Fork 1

冯文议 / api-resultJavaMIT

Join us
Explore and code with more than 5 million developers,Free private repositories !:)
Sign up
API接口返回结果规范化解决方案 spread retract

https://github.com/fengwenyi/api-result

Clone or download
Cancel
Notice: Creating folder will generate an empty file .keep, because not support in Git
Loading...
README.md

Api Result

Api Result,是一套API接口返回结果规范化解决方案。它是在实际应用环境下产生了,并不断更新和完善。 结合Spring Boot进行开发,让接口响应结果变得更加规范。

中央仓库

Maven

<dependency>
  <groupId>com.fengwenyi</groupId>
  <artifactId>api-result</artifactId>
  <version>2.0.4.RELEASE</version>
</dependency>

Gradle

implementation 'com.fengwenyi:api-result:2.0.4.RELEASE'

Sample

ResultUtils

package siample.api_result.result;

import com.fengwenyi.api_result.helper.ResultApiHelper;
import com.fengwenyi.api_result.model.ResultApiModel;

/**
 * 接口响应结果封装工具类
 * @author Erwin Feng
 * @since 2020/5/26
 */
public class ResultUtils {

    /**
     * 成功,无数据
     * @return {@link ResultApiModel}
     */
    public static ResultApiModel<Integer, Void> success() {
        return ResultApiHelper.success(ResultCodeEnum.SUCCESS.getCode(), ResultCodeEnum.SUCCESS.getMessage());
    }

    /**
     * 成功,携带数据
     * @param data 响应数据
     * @param <T>  响应数据类型
     * @return {@link ResultApiModel}
     */
    public static <T> ResultApiModel<Integer, T> success(T data) {
        return ResultApiHelper.success(ResultCodeEnum.SUCCESS.getCode(), ResultCodeEnum.SUCCESS.getMessage(), data);
    }

    /**
     * 失败,返回具体的错误码和详细的错误信息
     * @param resultCodeEnum 返回码枚举类,{@link ResultCodeEnum}
     * @return {@link ResultApiModel}
     */
    public static ResultApiModel<Integer, Void> error(ResultCodeEnum resultCodeEnum) {
        return ResultApiHelper.error(resultCodeEnum.getCode(), resultCodeEnum.getMessage());
    }

}

ResultCodeEnum

package siample.api_result.result;

import lombok.Getter;

/**
 * 返回结果码枚举
 * @author Erwin Feng
 * @since 2020/5/24
 */
@Getter
public enum ResultCodeEnum {

    SUCCESS(0, "Success");

    /** 返回码 */
    private Integer code;

    /** 描述信息 */
    private String message;

    ResultCodeEnum(Integer code, String message) {
        this.code = code;
        this.message = message;
    }
}

ApiController

package siample.api_result.controller;

import com.fengwenyi.api_result.model.ResultApiModel;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import siample.api_result.result.ResultUtils;
import siample.api_result.vo.response.UserResponseVo;

import java.util.Arrays;
import java.util.List;
import java.util.UUID;

/**
 * API
 * @author Erwin Feng
 * @since 2020/5/26
 */
@RestController
@RequestMapping("/api")
public class ApiController {

    /**
     * 查询所有用户,返回所有用户列表
     * @return {@link ResultApiModel}
     */
    @GetMapping("/users")
    public ResultApiModel<Integer, List<UserResponseVo>> users() {
        List<UserResponseVo> userResponseVos = Arrays.asList(
                new UserResponseVo().setUid(UUID.randomUUID().toString()).setRealName("关羽").setNickname("云长"),
                new UserResponseVo().setUid(UUID.randomUUID().toString()).setRealName("张飞").setNickname("翼德"),
                new UserResponseVo().setUid(UUID.randomUUID().toString()).setRealName("赵云").setNickname("子龙")
        );
        return ResultUtils.success(userResponseVos);
    }

}

UserResponseVo

package siample.api_result.vo.response;

import lombok.Data;
import lombok.experimental.Accessors;

/**
 * 用户响应VO
 * @author Erwin Feng
 * @since 2020/5/26
 */
@Data
@Accessors(chain = true)
public class UserResponseVo {

    /** 用户ID */
    private String uid;

    /** 用户真实姓名 */
    private String realName;

    /** 用户昵称 */
    private String nickname;

}

响应

{
    "success":true,
    "message":"Success",
    "data":[
        {
            "uid":"e84e4f8a-741e-48dd-b874-99eb4bcf9a6d",
            "realName":"关羽",
            "nickname":"云长"
        },
        {
            "uid":"8e5a8980-92cd-48d6-922a-a39f11f0a8c3",
            "realName":"张飞",
            "nickname":"翼德"
        },
        {
            "uid":"0c273883-a7cc-408b-8a35-aff62394fd62",
            "realName":"赵云",
            "nickname":"子龙"
        }
    ],
    "code":0
}

Spring Boot结合api-result使用示例

Wiki

Api Result Wiki

版本标识说明

BUILD

开发版本:用于标识该版本正在构建或者开发中。

SNAPSHOT

预览版本:开发已经完成,开始进入测试阶段。

RELEASE

稳定版本:已发布到中央仓库。

Comments ( 0 )

Sign in for post a comment

Java
1
https://gitee.com/fengwenyi/api-result.git
git@gitee.com:fengwenyi/api-result.git
fengwenyi
api-result
api-result
master

Help Search

205735 778617b6 1899542 205747 347fc4a1 1899542