package com.wlb.response;
import com.wlb.enums.StatusCodeResultEnum;
import lombok.Getter;
import lombok.Setter;
@Setter
@Getter
public class ApiResponse<T> {
private int code;
private String message;
private T data;
public ApiResponse() {
}
public ApiResponse(int code, String message, T data) {
this.code = code;
this.message = message;
this.data = data;
}
public static <T> ApiResponse<T> success(T data) {
return new ApiResponse<>(StatusCodeResultEnum.SUCCESS.getCode(), StatusCodeResultEnum.SUCCESS.getMessage(), data);
}
public static <T> ApiResponse<T> success(String message, T data) {
return new ApiResponse<>(StatusCodeResultEnum.SUCCESS.getCode(), message, data);
}
public static <T> ApiResponse<T> error(int code, String message) {
return new ApiResponse<>(code, message, null);
}
public static <T> ApiResponse<T> error(StatusCodeResultEnum statusCode) {
return new ApiResponse<>(statusCode.getCode(), statusCode.getMessage(), null);
}
}
package com.wlb.enums;
import lombok.Getter;
@Getter
public enum StatusCodeResultEnum {
SUCCESS(200, "成功"),
BAD_REQUEST(400, "错误的请求"),
UNAUTHORIZED(401, "未授权"),
FORBIDDEN(403, "禁止"),
NOT_FOUND(404, "未找到"),
INTERNAL_SERVER_ERROR(500, "服务器内部错误");
private final int code;
private final String message;
StatusCodeResultEnum(int code, String message) {
this.code = code;
this.message = message;
}
}