forked from plusone/plusone-commons
UnifiedResponse 的 status 修改为 code,类型为 String;构建 error 时必须传 code。
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2023-2024 the original author or authors.
|
||||
* Copyright 2023-2025 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -17,109 +17,107 @@
|
||||
package xyz.zhouxy.plusone.commons.model.dto;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.function.BooleanSupplier;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import xyz.zhouxy.plusone.commons.util.AssertTools;
|
||||
|
||||
/**
|
||||
* 统一结果,对返回给前端的数据进行封装。
|
||||
*
|
||||
* <p>
|
||||
* <b>SUCCESS: 2000000</b>
|
||||
* </p>
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
*/
|
||||
public abstract class UnifiedResponse {
|
||||
public class UnifiedResponse<T> {
|
||||
|
||||
private Object status;
|
||||
private String code;
|
||||
private String message;
|
||||
|
||||
private @Nullable Object data;
|
||||
private @Nullable T data;
|
||||
|
||||
public static UnifiedResponse success() {
|
||||
return new SuccessResult();
|
||||
// ================================
|
||||
// #region - Constructors
|
||||
// ================================
|
||||
|
||||
private UnifiedResponse(String code, @Nullable String message) {
|
||||
this(code, message, null);
|
||||
}
|
||||
|
||||
public static UnifiedResponse success(@Nullable String message) {
|
||||
return new SuccessResult(message);
|
||||
}
|
||||
|
||||
public static UnifiedResponse success(@Nullable String message, @Nullable Object data) {
|
||||
return new SuccessResult(message, data);
|
||||
}
|
||||
|
||||
public static UnifiedResponse error(@Nullable String message) {
|
||||
return new ErrorResult(message);
|
||||
}
|
||||
|
||||
public static UnifiedResponse error(@Nullable String message, @Nullable Object data) {
|
||||
return new ErrorResult(message, data);
|
||||
}
|
||||
|
||||
public static UnifiedResponse error(Object status, @Nullable String message) {
|
||||
return new ErrorResult(status, message);
|
||||
}
|
||||
|
||||
public static UnifiedResponse error(Object status, @Nullable String message, @Nullable Object data) {
|
||||
return new ErrorResult(status, message, data);
|
||||
}
|
||||
|
||||
public static UnifiedResponse error(Object status, Throwable e) {
|
||||
return new ErrorResult(status, e);
|
||||
}
|
||||
|
||||
public static UnifiedResponse of(Object status, @Nullable String message) {
|
||||
return new CustomResult(status, message);
|
||||
}
|
||||
|
||||
public static UnifiedResponse of(Object status, @Nullable String message, @Nullable Object data) {
|
||||
return new CustomResult(status, message, data);
|
||||
}
|
||||
|
||||
public static UnifiedResponse of(final boolean isSuccess,
|
||||
final Supplier<SuccessResult> successResult, final Supplier<ErrorResult> errorResult) {
|
||||
AssertTools.checkNotNull(successResult, "Success supplier must not be null.");
|
||||
AssertTools.checkNotNull(errorResult, "Error supplier must not be null.");
|
||||
return isSuccess ? successResult.get() : errorResult.get();
|
||||
}
|
||||
|
||||
public static UnifiedResponse of(final BooleanSupplier isSuccess,
|
||||
final Supplier<SuccessResult> successResult, final Supplier<ErrorResult> errorResult) {
|
||||
AssertTools.checkNotNull(isSuccess, "Conditions for success must not be null.");
|
||||
AssertTools.checkNotNull(successResult, "Success supplier must not be null.");
|
||||
AssertTools.checkNotNull(errorResult, "Error supplier must not be null.");
|
||||
return isSuccess.getAsBoolean() ? successResult.get() : errorResult.get();
|
||||
}
|
||||
|
||||
protected UnifiedResponse(Object status, @Nullable String message) {
|
||||
setStatus(status);
|
||||
setMessage(message);
|
||||
}
|
||||
|
||||
protected UnifiedResponse(Object status, @Nullable String message, @Nullable Object data) {
|
||||
setStatus(status);
|
||||
setMessage(message);
|
||||
setData(data);
|
||||
}
|
||||
|
||||
private void setStatus(Object status) {
|
||||
this.status = Objects.requireNonNull(status);
|
||||
}
|
||||
|
||||
private void setMessage(@Nullable String message) {
|
||||
private UnifiedResponse(String code, @Nullable String message, @Nullable T data) {
|
||||
this.code = Objects.requireNonNull(code);
|
||||
this.message = message == null ? "" : message;
|
||||
}
|
||||
|
||||
private void setData(@Nullable Object data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
// Constructors end
|
||||
// ================================
|
||||
// #endregion - Constructors
|
||||
// ================================
|
||||
|
||||
// Getters
|
||||
public static final String SUCCESS_CODE = "2000000";
|
||||
private static final String DEFAULT_SUCCESS_MSG = "SUCCESS";
|
||||
|
||||
public Object getStatus() {
|
||||
return status;
|
||||
// ================================
|
||||
// #region - success
|
||||
// ================================
|
||||
|
||||
public static UnifiedResponse<Void> success() {
|
||||
return new UnifiedResponse<>(SUCCESS_CODE, DEFAULT_SUCCESS_MSG);
|
||||
}
|
||||
|
||||
public static UnifiedResponse<Void> success(@Nullable String message) {
|
||||
return new UnifiedResponse<>(SUCCESS_CODE, message);
|
||||
}
|
||||
|
||||
public static <T> UnifiedResponse<T> success(@Nullable String message, @Nullable T data) {
|
||||
return new UnifiedResponse<>(SUCCESS_CODE, message, data);
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion - success
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - error
|
||||
// ================================
|
||||
|
||||
public static UnifiedResponse<Void> error(String code, @Nullable String message) {
|
||||
return new UnifiedResponse<>(code, message);
|
||||
}
|
||||
|
||||
public static <T> UnifiedResponse<T> error(String code, @Nullable String message, @Nullable T data) {
|
||||
return new UnifiedResponse<>(code, message, data);
|
||||
}
|
||||
|
||||
public static UnifiedResponse<Void> error(String code, Throwable e) {
|
||||
return new UnifiedResponse<>(code, e.getMessage());
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion - error
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - of
|
||||
// ================================
|
||||
|
||||
public static UnifiedResponse<Void> of(String code, @Nullable String message) {
|
||||
return new UnifiedResponse<>(code, message);
|
||||
}
|
||||
|
||||
public static <T> UnifiedResponse<T> of(String code, @Nullable String message, @Nullable T data) {
|
||||
return new UnifiedResponse<>(code, message, data);
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion - of
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - Getters
|
||||
// ================================
|
||||
|
||||
public String getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
@@ -127,16 +125,18 @@ public abstract class UnifiedResponse {
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public Object getData() {
|
||||
public T getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
// Getters end
|
||||
// ================================
|
||||
// #endregion - Getters
|
||||
// ================================
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("{status: %s, message: \"%s\", data: %s}",
|
||||
transValue(this.status), this.message, transValue(this.data));
|
||||
return String.format("{code: \"%s\", message: \"%s\", data: %s}",
|
||||
this.code, this.message, transValue(this.data));
|
||||
}
|
||||
|
||||
private static String transValue(Object value) {
|
||||
@@ -148,62 +148,4 @@ public abstract class UnifiedResponse {
|
||||
}
|
||||
return String.valueOf(value);
|
||||
}
|
||||
|
||||
protected static class SuccessResult extends UnifiedResponse {
|
||||
public static final String SUCCESS_STATUS = "2000000";
|
||||
|
||||
private static final String DEFAULT_SUCCESS_MSG = "SUCCESS";
|
||||
|
||||
SuccessResult() {
|
||||
super(SUCCESS_STATUS, DEFAULT_SUCCESS_MSG);
|
||||
}
|
||||
|
||||
SuccessResult(@Nullable String message) {
|
||||
super(SUCCESS_STATUS, message);
|
||||
}
|
||||
|
||||
SuccessResult(@Nullable String message, @Nullable Object data) {
|
||||
super(SUCCESS_STATUS, message, data);
|
||||
}
|
||||
}
|
||||
|
||||
protected static class ErrorResult extends UnifiedResponse {
|
||||
public static final String DEFAULT_ERROR_STATUS = "9999999";
|
||||
|
||||
ErrorResult(@Nullable String message) {
|
||||
super(DEFAULT_ERROR_STATUS, message);
|
||||
}
|
||||
|
||||
ErrorResult(@Nullable String message, @Nullable Object data) {
|
||||
super(DEFAULT_ERROR_STATUS, message, data);
|
||||
}
|
||||
|
||||
ErrorResult(Object status, @Nullable String message) {
|
||||
super(status, message);
|
||||
}
|
||||
|
||||
ErrorResult(Object status, @Nullable String message, @Nullable Object data) {
|
||||
super(status, message, data);
|
||||
}
|
||||
|
||||
ErrorResult(Object status, Throwable e) {
|
||||
super(status, Objects.requireNonNull(e).getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 自定义结果
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
*/
|
||||
protected static class CustomResult extends UnifiedResponse {
|
||||
|
||||
CustomResult(Object status, @Nullable String message) {
|
||||
super(status, message);
|
||||
}
|
||||
|
||||
CustomResult(Object status, @Nullable String message, @Nullable Object data) {
|
||||
super(status, message, data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user