跳至主要內容

framework-api

杨轩-国实信息大约 2 分钟framework

git地址:

http://10.16.202.103:8089/component/component-ser/gosci-tech-framework-api

返回实体类定义

定义系统返回状态编码

public enum SystemCodeEnum {

    SUCCESS(200, "成功的响应结果"),
    UNAUTHORIZED(401, "未经授权的访问"),
    SYSTEM_ERROR(500, "服务器内部错误");

    int code;
    String msg;

    SystemCodeEnum(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public int code() {
        return code;
    }

    public String msg() {
        return msg;
    }
}

统一返回实体包装类

/**
 * @author Hydra
 */
@Data
@ApiModel("系统响应体")
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class Result<T> {

    private static final String FAILD_MSG = "FAIL";

    private static final String SUCCESS_MSG = "OK";

    /**
     * 接口响应结果描述
     */
    @ApiModelProperty(value = "接口响应结果描述", required = true, example = SUCCESS_MSG)
    private String msg;

    /**
     * 系统响应码
     *
     * @see SystemCodeEnum
     */
    @ApiModelProperty(value = "系统响应码", required = true, example = "200")
    private Integer code;

    /**
     * 接口响应的数据
     */
    @ApiModelProperty(value = "接口的响应数据", required = true)
    private T data;

    /**
     * 成功的接口响应
     *
     * @param data
     * @param <T>
     * @return
     */
    public static <T> Result<T> success(T data) {
        Result<T> result = new Result<>();
        result.setCode(SystemCodeEnum.SUCCESS.code());
        result.setData(data);
        result.setMsg(SystemCodeEnum.SUCCESS.msg());
        return result;
    }

    /**
     * 添加返回信息
     *
     * @param data
     * @param message
     * @return
     */
    public static <T> Result<T> success(T data, String message) {
        Result<T> result = new Result<>();
        result.setCode(SystemCodeEnum.SUCCESS.code());
        result.setData(data);
        result.setMsg(message);
        return result;
    }

    /**
     * 成功的接口响应
     *
     * @return
     */
    public static <T> Result<T> success() {
        return success(null);
    }

    /**
     * 失败的接口响应
     *
     * @param <T>
     * @return
     */
    public static <T> Result<T> faild() {
        Result<T> result = new Result<>();
        result.setCode(SystemCodeEnum.SYSTEM_ERROR.code());
        result.setData(null);
        result.setMsg(SystemCodeEnum.SYSTEM_ERROR.msg());
        return result;
    }

    /**
     * 失败的接口响应
     *
     * @param codeEnum
     * @param <T>
     * @return
     */
    public static <T> Result<T> failed(SystemCodeEnum codeEnum) {
        Result<T> result = new Result<>();
        result.setCode(codeEnum.code());
        result.setData(null);
        result.setMsg(codeEnum.msg());
        return result;
    }

    /**
     * 失败的接口响应
     *
     * @param msg
     * @param <T>
     * @return
     */
    public static <T> Result<T> failed(String msg) {
        Result<T> result = new Result<>();
        result.setCode(SystemCodeEnum.SYSTEM_ERROR.code());
        result.setData(null);
        result.setMsg(msg);
        return result;
    }

    /**
     * @param message
     * @param errorcode
     * @return
     */
    public static <T> Result<T> failed(String message, Integer errorcode) {
        Result<T> result = new Result<>();
        result.setCode(errorcode);
        result.setData(null);
        result.setMsg(message);
        return result;
    }

    /**
     * 判定响应结果是否正确
     */
    public boolean isSuccess() {
        if (Objects.isNull(this.code)) {
            return false;
        }
        return this.code == SystemCodeEnum.SUCCESS.code();
    }

}

使用

查询输出格式

Result使用泛型规范返回类型

@GetMapping(value="/getById/{id}")
public Result<SystemUserVO> getById(
        HttpServletRequest request,
        HttpServletResponse response,
        @PathVariable("id") Integer id ) {
    SystemUserVO userVO = systemUserService.getById(id);
    return Result.success(userVO);
}

@PostMapping(value="/list")
public Result<List<SystemUserVO>> list(
        HttpServletRequest request,
        HttpServletResponse response,
        @RequestBody SystemUserDTO systemUserDTO) {
    List<SystemUserVO> list=systemUserService.list(systemUserDTO);
    return Result.success(list);
}

操作输出格式

Result中泛型使用Void即可:

@PostMapping(value="/save")
public Result<Void> save(
        @RequestBody SystemUserDTO systemUserDTO) {
    return systemUserService.save(systemUserDTO)?
            Result.success():  //操作成功情况
            Result.faild(); //操作失败情况
}

结果返回

成功默认结果

{
    "msg": "成功的响应结果",
    "code": 200,
    "data": null,
    "success": true
}

失败默认结果

{
    "msg": "服务器内部错误",
    "code": 500,
    "data": null,
    "success": false
}

包含实体对象的返回结果:

{
    "msg": "成功的响应结果",
    "code": 200,
    "data": {
        "id": 1,
        "name": "Hydra",
        "age": 18
    },
    "success": true
}