新增 UnifiedResponse 工厂 UnifiedResponses。

This commit is contained in:
2025-02-20 20:26:21 +08:00
parent ec4efe5f0f
commit faab942e13
4 changed files with 722 additions and 117 deletions

View File

@@ -22,10 +22,6 @@ import javax.annotation.Nullable;
/**
* 统一结果,对返回给前端的数据进行封装。
*
* <p>
* <b>SUCCESS: 2000000</b>
* </p>
*
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
*/
public class UnifiedResponse<T> {
@@ -39,11 +35,11 @@ public class UnifiedResponse<T> {
// #region - Constructors
// ================================
private UnifiedResponse(String code, @Nullable String message) {
UnifiedResponse(String code, @Nullable String message) {
this(code, message, null);
}
private UnifiedResponse(String code, @Nullable String message, @Nullable T data) {
UnifiedResponse(String code, @Nullable String message, @Nullable T data) {
this.code = Objects.requireNonNull(code);
this.message = message == null ? "" : message;
this.data = data;
@@ -53,65 +49,6 @@ public class UnifiedResponse<T> {
// #endregion - Constructors
// ================================
public static final String SUCCESS_CODE = "2000000";
private static final String DEFAULT_SUCCESS_MSG = "SUCCESS";
// ================================
// #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
// ================================

View File

@@ -0,0 +1,91 @@
/*
* 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package xyz.zhouxy.plusone.commons.model.dto;
import javax.annotation.Nullable;
/**
* UnifiedResponse 工厂
*
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 1.0.0
* @see UnifiedResponse
*/
public class UnifiedResponses {
public static final String SUCCESS_CODE = "2000000";
public static final String DEFAULT_SUCCESS_MSG = "SUCCESS";
// ================================
// #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
// ================================
protected UnifiedResponses() {
}
}