Optimized code

This commit is contained in:
2021-02-07 18:21:08 +08:00
parent 9c102220db
commit c869d9cc20
11 changed files with 311 additions and 37 deletions

View File

@@ -22,6 +22,15 @@
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
</dependencies>
<build>

View File

@@ -0,0 +1,54 @@
package io.github.ehlxr.did.common;
/**
* 自定义状态码
*
* @author ehlxr
* @since 2020/3/18.
*/
public enum Code {
/**
* 成功
*/
SUCCESSFUL(200, "success"),
/**
* 未知异常
*/
UNKNOWN_EXCEPTION(600, "系统异常,请联系管理员");
private static final Code[] CODES = Code.values();
private final int code;
private final String message;
Code(int code, String message) {
this.code = code;
this.message = message;
}
public static Code code(int code) {
for (Code c : CODES) {
if (code == c.getCode()) {
return c;
}
}
return null;
}
public int getCode() {
return code;
}
public String getMessage() {
return message;
}
@Override
public String toString() {
return "Code{" +
"code=" + code +
", message='" + message + '\'' +
'}';
}
}

View File

@@ -0,0 +1,150 @@
package io.github.ehlxr.did.common;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import io.netty.util.internal.StringUtil;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Objects;
/**
* 统一输出结果集
*
* @author ehlxr
* @since 2020/3/18.
*/
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Result<T> implements Serializable {
private static final long serialVersionUID = -2758720512348727698L;
/**
* 响应编码
*/
private int code;
/**
* 消息,如错误消息
*/
private String message;
/**
* 数据内容
*/
private T data;
private Result() {
}
private Result(int code, T data, String message) {
this.code = code;
this.data = data;
this.message = message;
}
public static <T> Result<T> success(T data, String message) {
return new Result<>(Code.SUCCESSFUL.getCode(), data, message);
}
public static <T> Result<T> success(T data) {
return success(data, null);
}
public static <T> Result<T> success() {
return success(null);
}
public static <T> Result<T> of(int c, T d, String m) {
return new Result<>(c, d, m);
}
public static <T> Result<T> of(Code c, T d, String m) {
return new Result<>(c.getCode(), d, m);
}
public static <T> Result<T> fail(Code code, String message) {
return of(code.getCode(), null, message);
}
public static <T> Result<T> fail(String message) {
return fail(Code.UNKNOWN_EXCEPTION.getCode(), message);
}
public static <T> Result<T> fail(int code, String message) {
return of(code, null, message);
}
public static <T> Result<T> fail(Code code) {
return fail(code, code.getMessage());
}
public static <T> Result<T> fail(Throwable e) {
return of(Code.UNKNOWN_EXCEPTION.getCode(), null, String.format("%s: %s", e.getClass().getSimpleName(), e.getMessage()));
}
public String getMessage() {
// return StringUtil.isNullOrEmpty(m) ? c.getMessage() : m;
if (StringUtil.isNullOrEmpty(message)) {
Code code;
try {
code = Code.code(this.code);
} catch (Exception e) {
return message;
}
return Objects.isNull(code) ? "" : code.getMessage();
}
return message;
}
public void setMessage(String message) {
this.message = message;
}
public int getCode() {
// return Objects.nonNull(c) ? c.getCode() : Code.UNKNOWN_EXCEPTION.getCode();
return code;
}
public void setCode(int code) {
this.code = code;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Result<?> result = (Result<?>) o;
return code == result.code &&
Objects.equals(message, result.message) &&
Objects.equals(data, result.data);
}
@Override
public String toString() {
try {
ObjectMapper om = new ObjectMapper();
// 取消时间的转化格式, 默认是时间戳
om.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
// 设置时间格式
om.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
om.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
om.configure(SerializationFeature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED, false);
return om.writeValueAsString(this);
} catch (JsonProcessingException e) {
return "";
}
}
}

View File

@@ -31,8 +31,8 @@ public class SdkProto {
return rqid;
}
public void setRqid(int rqid) {
this.rqid = rqid;
public static SdkProtoBuilder newBuilder() {
return new SdkProtoBuilder();
}
public long getDid() {
@@ -50,4 +50,35 @@ public class SdkProto {
", did=" + did +
'}';
}
public void setRqid(int rqid) {
if (rqid > 0) {
this.rqid = rqid;
}
}
public static final class SdkProtoBuilder {
private int rqid;
private long did;
private SdkProtoBuilder() {
}
public SdkProtoBuilder rqid(int rqid) {
this.rqid = rqid;
return this;
}
public SdkProtoBuilder did(long did) {
this.did = did;
return this;
}
public SdkProto build() {
SdkProto sdkProto = new SdkProto();
sdkProto.setRqid(rqid);
sdkProto.setDid(did);
return sdkProto;
}
}
}