Java项目如何为Controller设置统一的返回格式呢?

欣喜 SpringBoot 发布时间:2024-02-18 09:19:53 阅读数:474 1
下文笔者讲述java代码设置统一返回格式的方法及示例分享,如下所示

定义统一返回格式的实现思路

我们只需定义一个统一类
      设置相应的字段,即可方便统一样式返回
例:springBoot设置统一返回格式
@Data
@Builder
@AllArgsConstructor
public class ResultDto<T> implements Serializable{
 
    private static final long serialVersionUID = 1L;
    /**
     * 返回码
     */
    private ResultCode code;
    /**
     * 返回消息
     */
    private String message;
    /**
     * 数据数
     */
    private long total;
    /**
     * 返回数据
     */
    private T data;
 
    public ResultDto(){this.total = Long.valueOf(0L);}
 
    public ResultDto(ResultCode code){this.code = code;}
 
    public ResultDto(ResultCode code, T data){
        super();
        this.code = code;
        this.data = data;
        this.message = code.getMessage();
    }
 
    public String getCode(){
        return this.code.getCode();
    }
 
    public String getCodeMessage(){
        return this.code.getMessage();
    }
    public static  <T> ResultDto<T> ok(){
        return new ResultDto<T>(ResultCode.OK);
    }
 
    public static <T> ResultDto ok(T data){
        return ResultDto.builder()
                .code(ResultCode.OK)
                .data(data)
                .message(ResultCode.OK.getMessage())
                .build();
    }
 
    public static <T> ResultDto ok(T data, long total) {
        return ResultDto.builder()
                .code(ResultCode.OK)
                .data(data)
                .total(total)
                .message(ResultCode.OK.getMessage())
                .build();
    }
 
    public static <T> ResultDto ok(String message){
        return ResultDto.builder()
                .code(ResultCode.OK)
                .message(message)
                .build();
    }
 
    public static <T> ResultDto ok(String message, T data){
        return ResultDto.builder()
                .code(ResultCode.OK)
                .message(message)
                .data(data)
                .build();
    }
 
    public static <T> ResultDto error(){
        return ResultDto.builder()
                .code(ResultCode.ERROR)
                .message(ResultCode.ERROR.getMessage())
                .data(null)
                .build();
    }
 
    public static <T> ResultDto error(String message){
        return ResultDto.builder()
                .code(ResultCode.ERROR)
                .message(message == null || message.isEmpty() ? ResultCode.ERROR.getMessage() : message)
                .data(null)
                .build();
    }
 
    public boolean isSuccess(){
        if (this.getCode() == ResultCode.OK.name()){
            return true;
        }
        return false;
    }
}

返回状态码枚举类

@Getter
public enum ResultCode {
 
    OK("200", "成功"),
    ERROR("201", "失败"),
    PARAM_ERROR("202", "参数不正确"),
    SERVICE_ERROR("203", "服务异常"),
    DATA_ERROR("204","数据异常"),
    NOT_LOG_IN("205","未登录"),
    NO_ACCESS("206", "没有权限"),
    TOKEN_ERROR("207", "token错误");
 
    private String code;
    private String message;
 
    ResultCode(String code,String message){
        this.code = code;
        this.message = message;
    }
 
    public String getCode(){return this.code;}
 
    public String getMessage(){return this.message;}
 
    public static ResultCode codeOf(String code){
        ResultCode status = resolve(code);
        if (status == null){
            throw new IllegalArgumentException("No matching constant for [" + code + "]");
        }
        return status;
    }
 
    public static ResultCode resolve(String statusCode){
        for (ResultCode resultCode : values()){
            if (resultCode.code == statusCode){
                return resultCode;
            }
        }
        return null;
    }
 
    public String toString(){
        return this.code + " " + name();
    }
}
版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

本文链接: https://www.Java265.com/JavaFramework/SpringBoot/202402/7969.html

最近发表

热门文章

好文推荐

Java265.com

https://www.java265.com

站长统计|粤ICP备14097017号-3

Powered By Java265.com信息维护小组

使用手机扫描二维码

关注我们看更多资讯

java爱好者