非常好用的RetEntity类

76 阅读2分钟

非常好用的RetEntity类

package com.mz.container.demo.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonFormat;
import org.springframework.http.HttpStatus;
import org.springframework.util.Assert;

import java.util.Date;

public final class RetEntity<T> {
    @JSONField(
            ordinal = 1
    )
    private int code;
    @JSONField(
            ordinal = 2
    )
    private boolean status;
    @JSONField(
            ordinal = 3
    )
    private String message;
    @JSONField(
            ordinal = 4
    )
    private T body;
    @JSONField(
            ordinal = 5
    )
    @JsonFormat(
            pattern = "yyyy-MM-dd HH:mm:ss.SSS",
            timezone = "GMT+8"
    )
    private Date timestamp;

    public RetEntity() {
    }

    public static RetEntity error() {
        return error((String)null);
    }
    public static RetEntity error(String message) {
        return error((Integer)null, message);
    }

    public static RetEntity error(Integer code, String message) {
        if (code == null) {
            code = HttpStatus.INTERNAL_SERVER_ERROR.value();
        }

        if (message == null) {
//            message = DictUtil.getMessageNameByValue(code);
        }

        return build(code, false, message);
    }

    public static RetEntity ok() {
        return ok((String)null);
    }

    public static RetEntity ok(String message) {
        return ok((Integer)null, message);
    }

    public static RetEntity ok(Integer code, String message) {
        if (code == null) {
            code = HttpStatus.OK.value();
        }

//        if (message == null) {
//            message = DictUtil.getMessageNameByValue(code);
//        }

        return build(code, true, message);
    }

    public static RetEntity build(int code, boolean status, String message) {
        return (new RetEntity()).setCode(code).setStatus(status).setMessage(message).setTimestamp(new Date());
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this, true);
    }

    public static RetEntity convert(String jsonString) throws IllegalArgumentException {
        Assert.hasLength(jsonString, "jsonString is null or empty");

        try {
            return (RetEntity)JSON.parseObject(jsonString, RetEntity.class);
        } catch (Exception var2) {
            throw new IllegalArgumentException("jsonString did not comply with the format");
        }
    }

    public int getCode() {
        return this.code;
    }

    public boolean isStatus() {
        return this.status;
    }

    public String getMessage() {
        return this.message;
    }

    public T getBody() {
        return this.body;
    }

    public Date getTimestamp() {
        return this.timestamp;
    }

    public RetEntity<T> setCode(int code) {
        this.code = code;
        return this;
    }

    public RetEntity<T> setStatus(boolean status) {
        this.status = status;
        return this;
    }

    public RetEntity<T> setMessage(String message) {
        this.message = message;
        return this;
    }

    public RetEntity<T> setBody(T body) {
        this.body = body;
        return this;
    }

    public RetEntity<T> setTimestamp(Date timestamp) {
        this.timestamp = timestamp;
        return this;
    }

    @Override
    public boolean equals(Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof RetEntity)) {
            return false;
        } else {
            RetEntity<?> other = (RetEntity)o;
            if (this.getCode() != other.getCode()) {
                return false;
            } else if (this.isStatus() != other.isStatus()) {
                return false;
            } else {
                label49: {
                    Object this$message = this.getMessage();
                    Object other$message = other.getMessage();
                    if (this$message == null) {
                        if (other$message == null) {
                            break label49;
                        }
                    } else if (this$message.equals(other$message)) {
                        break label49;
                    }

                    return false;
                }

                Object this$body = this.getBody();
                Object other$body = other.getBody();
                if (this$body == null) {
                    if (other$body != null) {
                        return false;
                    }
                } else if (!this$body.equals(other$body)) {
                    return false;
                }

                Object this$timestamp = this.getTimestamp();
                Object other$timestamp = other.getTimestamp();
                if (this$timestamp == null) {
                    if (other$timestamp != null) {
                        return false;
                    }
                } else if (!this$timestamp.equals(other$timestamp)) {
                    return false;
                }

                return true;
            }
        }
    }

    @Override
    public int hashCode() {
        boolean PRIME = true;
        int result = 1;
        result = result * 59 + this.getCode();
        result = result * 59 + (this.isStatus() ? 79 : 97);
        Object $message = this.getMessage();
        result = result * 59 + ($message == null ? 43 : $message.hashCode());
        Object $body = this.getBody();
        result = result * 59 + ($body == null ? 43 : $body.hashCode());
        Object $timestamp = this.getTimestamp();
        result = result * 59 + ($timestamp == null ? 43 : $timestamp.hashCode());
        return result;
    }
}

需要添加一下依赖

        <!--fastjson        -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.47</version>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

使用案例

    @Test
    public void test03(){
        RetEntity entity=test04();

        int code = entity.getCode();
        System.out.println(code);

        String message = entity.getMessage();
        System.out.println(entity.getMessage());

        Object body = entity.getBody();
        System.out.println(body);
        
        System.out.println(entity);
    }

    public RetEntity test04() {
        try {
            int a=10;
            int b=10;
            int c=a/b;
            return RetEntity.ok().setMessage("成功了").setBody(c);
        }catch (Exception e){
            return RetEntity.error().setMessage("失败了").setBody(e.toString());
        }
    }

运行结果:

image-20211109194525960

凑字凑字凑字凑字凑字凑字凑字凑字凑字凑字凑字凑字凑字凑字凑字凑字凑字凑字凑字凑字凑字凑字凑字凑字