139 lines
3.8 KiB
Java
Raw Normal View History

2023-03-13 14:26:03 +08:00
/*
* Copyright 2022-2023 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.
*/
2023-02-24 11:10:27 +08:00
package xyz.zhouxy.plusone.commons.util;
2022-11-07 17:49:27 +08:00
2023-04-03 18:01:56 +08:00
import java.util.function.BooleanSupplier;
import java.util.function.Supplier;
2023-04-15 20:16:21 +08:00
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
2022-11-07 17:49:27 +08:00
import com.fasterxml.jackson.annotation.JsonInclude;
/**
* 对返回给前端的数据进行封装
*
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
*/
@JsonInclude(JsonInclude.Include.NON_NULL)
public class RestfulResult {
public static final int SUCCESS_STATUS = 2000000;
2023-04-04 18:54:32 +08:00
public static final int DEFAULT_ERROR_STATUS = 9999999;
2022-11-07 17:49:27 +08:00
2023-04-15 20:16:21 +08:00
@Nonnull
2022-11-07 17:49:27 +08:00
private final Object status;
2023-04-15 20:16:21 +08:00
@Nonnull
2022-11-07 17:49:27 +08:00
private final String message;
2023-04-15 20:16:21 +08:00
@Nullable
2022-11-07 17:49:27 +08:00
private final Object data;
2023-04-15 20:16:21 +08:00
private RestfulResult(@Nonnull final Object status, @Nonnull final String message) {
2022-11-07 17:49:27 +08:00
this(status, message, null);
}
public static RestfulResult success() {
return new RestfulResult(SUCCESS_STATUS, "操作成功");
}
2023-04-15 20:16:21 +08:00
public static RestfulResult success(@Nonnull final String message) {
2022-11-07 17:49:27 +08:00
return new RestfulResult(SUCCESS_STATUS, message);
}
2023-04-15 20:16:21 +08:00
public static RestfulResult success(
@Nonnull final String message,
@Nullable final Object data) {
2022-11-07 17:49:27 +08:00
return new RestfulResult(SUCCESS_STATUS, message, data);
}
public static RestfulResult error() {
2023-04-03 18:01:56 +08:00
return new RestfulResult(DEFAULT_ERROR_STATUS, "未知错误");
2022-11-07 17:49:27 +08:00
}
2023-04-15 20:16:21 +08:00
public static RestfulResult error(
@Nonnull final Object status,
@Nonnull final String message) {
2022-11-07 17:49:27 +08:00
return new RestfulResult(status, message);
}
2023-04-15 20:16:21 +08:00
public static RestfulResult error(
@Nonnull final Object status,
@Nonnull final String message,
@Nullable final Object data) {
2022-11-07 17:49:27 +08:00
return new RestfulResult(status, message, data);
}
2023-04-15 20:16:21 +08:00
public static RestfulResult error(@Nonnull final Object status, @Nonnull final Throwable e) {
String msg = e.getMessage();
if (msg == null) {
msg = "";
}
return new RestfulResult(status, msg);
2022-11-07 17:49:27 +08:00
}
2023-04-03 18:01:56 +08:00
public static RestfulResult of(
final boolean isSuccess,
2023-04-15 20:16:21 +08:00
@Nonnull final Supplier<RestfulResult> success,
@Nonnull final Supplier<RestfulResult> error) {
2023-04-03 18:01:56 +08:00
return isSuccess ? success.get() : error.get();
}
public static RestfulResult of(
2023-04-15 20:16:21 +08:00
@Nonnull final BooleanSupplier isSuccess,
@Nonnull final Supplier<RestfulResult> success,
@Nonnull final Supplier<RestfulResult> error) {
2023-04-03 18:01:56 +08:00
return isSuccess.getAsBoolean() ? success.get() : error.get();
}
2023-04-12 11:01:42 +08:00
// Constructors
2023-04-03 18:01:56 +08:00
2023-04-15 20:16:21 +08:00
private RestfulResult(
@Nonnull final Object status,
@Nonnull final String message,
@Nullable final Object data) {
2023-04-12 11:01:42 +08:00
this.status = status;
this.message = message;
this.data = data;
2023-04-03 18:01:56 +08:00
}
2023-04-12 11:01:42 +08:00
// Constructors end
2023-04-03 18:01:56 +08:00
2023-04-12 11:01:42 +08:00
// Getters
2023-04-03 18:01:56 +08:00
2023-04-15 20:16:21 +08:00
@Nonnull
2023-04-12 11:01:42 +08:00
public Object getStatus() {
return status;
2023-04-03 18:01:56 +08:00
}
2023-04-15 20:16:21 +08:00
@Nonnull
2023-04-12 11:01:42 +08:00
public String getMessage() {
return message;
2023-04-03 18:01:56 +08:00
}
2023-04-15 20:16:21 +08:00
@Nullable
2023-04-12 11:01:42 +08:00
public Object getData() {
return data;
}
2023-04-03 18:01:56 +08:00
2023-04-12 11:01:42 +08:00
// Getters end
2023-04-03 18:01:56 +08:00
2023-04-12 11:01:42 +08:00
@Override
public String toString() {
return "RestfulResult [status=" + status + ", message=" + message + ", data=" + data + "]";
2023-04-03 18:01:56 +08:00
}
2022-11-07 17:49:27 +08:00
}