diff --git a/bubble-core/pom.xml b/bubble-core/pom.xml index f06f26f9..b7f5f024 100644 --- a/bubble-core/pom.xml +++ b/bubble-core/pom.xml @@ -23,11 +23,6 @@ jakarta.servlet jakarta.servlet-api - - - com.github.xiaoymin - knife4j-core - io.swagger.core.v3 @@ -38,10 +33,10 @@ cn.hutool hutool-all - + - com.google.guava - guava + com.google.auto.service + auto-service @@ -67,6 +62,12 @@ org.projectlombok lombok + + + org.junit.jupiter + junit-jupiter + test + \ No newline at end of file diff --git a/bubble-core/src/main/java/cn/fxbin/bubble/core/constant/YesOrNo.java b/bubble-core/src/main/java/cn/fxbin/bubble/core/constant/YesOrNo.java deleted file mode 100644 index 56af7df9..00000000 --- a/bubble-core/src/main/java/cn/fxbin/bubble/core/constant/YesOrNo.java +++ /dev/null @@ -1,32 +0,0 @@ -package cn.fxbin.bubble.core.constant; - -import lombok.AllArgsConstructor; -import lombok.Getter; - -/** - * YesOrNo - * - * @author fxbin - * @version v1.0 - * @since 2020/3/30 15:54 - */ -@Getter -@AllArgsConstructor -public enum YesOrNo { - - /** - * - */ - YES(1, "Y"), - - /** - * - */ - NO(0, "N"); - - - final int value; - - final String desc; - -} diff --git a/bubble-core/src/main/java/cn/fxbin/bubble/core/dataobject/BizErrorCode.java b/bubble-core/src/main/java/cn/fxbin/bubble/core/dataobject/BizErrorCode.java new file mode 100644 index 00000000..6a31da17 --- /dev/null +++ b/bubble-core/src/main/java/cn/fxbin/bubble/core/dataobject/BizErrorCode.java @@ -0,0 +1,24 @@ +package cn.fxbin.bubble.core.dataobject; + +/** + * BizErrorCode + * + *

+ * 业务状态码定义,业务方可做相应实现 + *

+ * + * @author fxbin + * @version v1.0 + * @since 2023/8/29 00:15 + */ +public non-sealed interface BizErrorCode extends ErrorCode { + + /** + * 错误码解析 + * + * @param errorCode 错误码 + * @return {@link BizErrorCode} + */ + BizErrorCode resolve(int errorCode); + +} diff --git a/bubble-core/src/main/java/cn/fxbin/bubble/core/dataobject/ErrorCode.java b/bubble-core/src/main/java/cn/fxbin/bubble/core/dataobject/ErrorCode.java new file mode 100644 index 00000000..58a8732e --- /dev/null +++ b/bubble-core/src/main/java/cn/fxbin/bubble/core/dataobject/ErrorCode.java @@ -0,0 +1,100 @@ +package cn.fxbin.bubble.core.dataobject; + + +import java.io.Serializable; + +/** + * 错误代码 + * ErrorCode + * + *

x + * 分为两类: + * 全局错误码(参照 {@link org.springframework.http.HttpStatus} 实现),实现为: {@code GlobalErrorCode}
+ * 业务错误码 {@code BizErrorCode}

+ *

+ * 参照:HTTP 响应状态码 + *

+ * + * @author fxbin + * @version v1.0 + * @since 2023/08/29 00:44 + */ + +public sealed interface ErrorCode extends Serializable permits GlobalErrorCode, BizErrorCode { + + /** + * Return the integer value of this status code. + */ + int value(); + + /** + * Return the String value of error reason phrase. + */ + String reasonPhrase(); + + /** + * Whether this status code is in the Informational class ({@code 1xx}). + * @see RFC 2616 + */ + default boolean is1xxInformational() { + return false; + }; + + /** + * Whether this status code is in the Successful class ({@code 2xx}). + * @see RFC 2616 + */ + default boolean is2xxSuccessful() { + return false; + } + + /** + * Whether this status code is in the Redirection class ({@code 3xx}). + * @see RFC 2616 + */ + default boolean is3xxRedirection() { + return false; + } + + /** + * Whether this status code is in the Client Error class ({@code 4xx}). + * @see RFC 2616 + */ + default boolean is4xxClientError() { + return false; + } + + /** + * Whether this status code is in the Server Error class ({@code 5xx}). + * @see RFC 2616 + */ + default boolean is5xxServerError() { + return false; + } + + /** + * Whether this status code is in the Client or Server Error class + * @see RFC 2616 + * @see RFC 2616 + * ({@code 4xx} or {@code 5xx}). + * @see #is4xxClientError() + * @see #is5xxServerError() + */ + boolean isError(); + + default boolean isSameCodeAs(ErrorCode other) { + return value() == other.value(); + } + + static ErrorCode valueOf(int code) { + GlobalErrorCode errorCode = GlobalErrorCode.resolve(code); + if (errorCode != null) { + return errorCode; + } + + // TODO 实现对业务错误码的判断 + + return null; + } + +} diff --git a/bubble-core/src/main/java/cn/fxbin/bubble/core/dataobject/GlobalErrorCode.java b/bubble-core/src/main/java/cn/fxbin/bubble/core/dataobject/GlobalErrorCode.java new file mode 100644 index 00000000..52c48f80 --- /dev/null +++ b/bubble-core/src/main/java/cn/fxbin/bubble/core/dataobject/GlobalErrorCode.java @@ -0,0 +1,507 @@ +package cn.fxbin.bubble.core.dataobject; + +import lombok.AllArgsConstructor; +import org.springframework.lang.Nullable; + +/** + * GlobalErrorCode + * + * @author fxbin + * @version v1.0 + * @since 2023/8/29 00:12 + */ +@AllArgsConstructor +public enum GlobalErrorCode implements ErrorCode { + + // 1xx Informational + + /** + * {@code 100 Continue}. + * @see HTTP/1.1: Semantics and Content, section 6.2.1 + */ + CONTINUE(100, HttpStatusSeries.INFORMATIONAL, "Continue"), + /** + * {@code 101 Switching Protocols}. + * @see HTTP/1.1: Semantics and Content, section 6.2.2 + */ + SWITCHING_PROTOCOLS(101, HttpStatusSeries.INFORMATIONAL, "Switching Protocols"), + /** + * {@code 102 Processing}. + * @see WebDAV + */ + PROCESSING(102, HttpStatusSeries.INFORMATIONAL, "Processing"), + /** + * {@code 103 Early Hints}. + * @see An HTTP Status Code for Indicating Hints + * @since 6.0.5 + */ + EARLY_HINTS(103, HttpStatusSeries.INFORMATIONAL, "Early Hints"), + /** + * {@code 103 Checkpoint}. + * @see A proposal for supporting + * resumable POST/PUT HTTP requests in HTTP/1.0 + * @deprecated in favor of {@link #EARLY_HINTS} which will be returned from {@code HttpStatus.valueOf(103)} + */ + @Deprecated(since = "6.0.5") + CHECKPOINT(103, HttpStatusSeries.INFORMATIONAL, "Checkpoint"), + + // 2xx Success + + /** + * {@code 200 OK}. + * @see HTTP/1.1: Semantics and Content, section 6.3.1 + */ + OK(200, HttpStatusSeries.SUCCESSFUL, "OK"), + /** + * {@code 201 Created}. + * @see HTTP/1.1: Semantics and Content, section 6.3.2 + */ + CREATED(201, HttpStatusSeries.SUCCESSFUL, "Created"), + /** + * {@code 202 Accepted}. + * @see HTTP/1.1: Semantics and Content, section 6.3.3 + */ + ACCEPTED(202, HttpStatusSeries.SUCCESSFUL, "Accepted"), + /** + * {@code 203 Non-Authoritative Information}. + * @see HTTP/1.1: Semantics and Content, section 6.3.4 + */ + NON_AUTHORITATIVE_INFORMATION(203, HttpStatusSeries.SUCCESSFUL, "Non-Authoritative Information"), + /** + * {@code 204 No Content}. + * @see HTTP/1.1: Semantics and Content, section 6.3.5 + */ + NO_CONTENT(204, HttpStatusSeries.SUCCESSFUL, "No Content"), + /** + * {@code 205 Reset Content}. + * @see HTTP/1.1: Semantics and Content, section 6.3.6 + */ + RESET_CONTENT(205, HttpStatusSeries.SUCCESSFUL, "Reset Content"), + /** + * {@code 206 Partial Content}. + * @see HTTP/1.1: Range Requests, section 4.1 + */ + PARTIAL_CONTENT(206, HttpStatusSeries.SUCCESSFUL, "Partial Content"), + /** + * {@code 207 Multi-Status}. + * @see WebDAV + */ + MULTI_STATUS(207, HttpStatusSeries.SUCCESSFUL, "Multi-Status"), + /** + * {@code 208 Already Reported}. + * @see WebDAV Binding Extensions + */ + ALREADY_REPORTED(208, HttpStatusSeries.SUCCESSFUL, "Already Reported"), + /** + * {@code 226 IM Used}. + * @see Delta encoding in HTTP + */ + IM_USED(226, HttpStatusSeries.SUCCESSFUL, "IM Used"), + + // 3xx Redirection + + /** + * {@code 300 Multiple Choices}. + * @see HTTP/1.1: Semantics and Content, section 6.4.1 + */ + MULTIPLE_CHOICES(300, HttpStatusSeries.REDIRECTION, "Multiple Choices"), + /** + * {@code 301 Moved Permanently}. + * @see HTTP/1.1: Semantics and Content, section 6.4.2 + */ + MOVED_PERMANENTLY(301, HttpStatusSeries.REDIRECTION, "Moved Permanently"), + /** + * {@code 302 Found}. + * @see HTTP/1.1: Semantics and Content, section 6.4.3 + */ + FOUND(302, HttpStatusSeries.REDIRECTION, "Found"), + /** + * {@code 302 Moved Temporarily}. + * @see HTTP/1.0, section 9.3 + * @deprecated in favor of {@link #FOUND} which will be returned from {@code HttpStatus.valueOf(302)} + */ + @Deprecated + MOVED_TEMPORARILY(302, HttpStatusSeries.REDIRECTION, "Moved Temporarily"), + /** + * {@code 303 See Other}. + * @see HTTP/1.1: Semantics and Content, section 6.4.4 + */ + SEE_OTHER(303, HttpStatusSeries.REDIRECTION, "See Other"), + /** + * {@code 304 Not Modified}. + * @see HTTP/1.1: Conditional Requests, section 4.1 + */ + NOT_MODIFIED(304, HttpStatusSeries.REDIRECTION, "Not Modified"), + /** + * {@code 305 Use Proxy}. + * @see HTTP/1.1: Semantics and Content, section 6.4.5 + * @deprecated due to security concerns regarding in-band configuration of a proxy + */ + @Deprecated + USE_PROXY(305, HttpStatusSeries.REDIRECTION, "Use Proxy"), + /** + * {@code 307 Temporary Redirect}. + * @see HTTP/1.1: Semantics and Content, section 6.4.7 + */ + TEMPORARY_REDIRECT(307, HttpStatusSeries.REDIRECTION, "Temporary Redirect"), + /** + * {@code 308 Permanent Redirect}. + * @see RFC 7238 + */ + PERMANENT_REDIRECT(308, HttpStatusSeries.REDIRECTION, "Permanent Redirect"), + + // --- 4xx Client Error --- + + /** + * {@code 400 Bad Request}. + * @see HTTP/1.1: Semantics and Content, section 6.5.1 + */ + BAD_REQUEST(400, HttpStatusSeries.CLIENT_ERROR, "Bad Request"), + /** + * {@code 401 Unauthorized}. + * @see HTTP/1.1: Authentication, section 3.1 + */ + UNAUTHORIZED(401, HttpStatusSeries.CLIENT_ERROR, "Unauthorized"), + /** + * {@code 402 Payment Required}. + * @see HTTP/1.1: Semantics and Content, section 6.5.2 + */ + PAYMENT_REQUIRED(402, HttpStatusSeries.CLIENT_ERROR, "Payment Required"), + /** + * {@code 403 Forbidden}. + * @see HTTP/1.1: Semantics and Content, section 6.5.3 + */ + FORBIDDEN(403, HttpStatusSeries.CLIENT_ERROR, "Forbidden"), + /** + * {@code 404 Not Found}. + * @see HTTP/1.1: Semantics and Content, section 6.5.4 + */ + NOT_FOUND(404, HttpStatusSeries.CLIENT_ERROR, "Not Found"), + /** + * {@code 405 Method Not Allowed}. + * @see HTTP/1.1: Semantics and Content, section 6.5.5 + */ + METHOD_NOT_ALLOWED(405, HttpStatusSeries.CLIENT_ERROR, "Method Not Allowed"), + /** + * {@code 406 Not Acceptable}. + * @see HTTP/1.1: Semantics and Content, section 6.5.6 + */ + NOT_ACCEPTABLE(406, HttpStatusSeries.CLIENT_ERROR, "Not Acceptable"), + /** + * {@code 407 Proxy Authentication Required}. + * @see HTTP/1.1: Authentication, section 3.2 + */ + PROXY_AUTHENTICATION_REQUIRED(407, HttpStatusSeries.CLIENT_ERROR, "Proxy Authentication Required"), + /** + * {@code 408 Request Timeout}. + * @see HTTP/1.1: Semantics and Content, section 6.5.7 + */ + REQUEST_TIMEOUT(408, HttpStatusSeries.CLIENT_ERROR, "Request Timeout"), + /** + * {@code 409 Conflict}. + * @see HTTP/1.1: Semantics and Content, section 6.5.8 + */ + CONFLICT(409, HttpStatusSeries.CLIENT_ERROR, "Conflict"), + /** + * {@code 410 Gone}. + * @see + * HTTP/1.1: Semantics and Content, section 6.5.9 + */ + GONE(410, HttpStatusSeries.CLIENT_ERROR, "Gone"), + /** + * {@code 411 Length Required}. + * @see + * HTTP/1.1: Semantics and Content, section 6.5.10 + */ + LENGTH_REQUIRED(411, HttpStatusSeries.CLIENT_ERROR, "Length Required"), + /** + * {@code 412 Precondition failed}. + * @see + * HTTP/1.1: Conditional Requests, section 4.2 + */ + PRECONDITION_FAILED(412, HttpStatusSeries.CLIENT_ERROR, "Precondition Failed"), + /** + * {@code 413 Payload Too Large}. + * @since 4.1 + * @see + * HTTP/1.1: Semantics and Content, section 6.5.11 + */ + PAYLOAD_TOO_LARGE(413, HttpStatusSeries.CLIENT_ERROR, "Payload Too Large"), + /** + * {@code 413 Request Entity Too Large}. + * @see HTTP/1.1, section 10.4.14 + * @deprecated in favor of {@link #PAYLOAD_TOO_LARGE} which will be + * returned from {@code HttpStatus.valueOf(413)} + */ + @Deprecated + REQUEST_ENTITY_TOO_LARGE(413, HttpStatusSeries.CLIENT_ERROR, "Request Entity Too Large"), + /** + * {@code 414 URI Too Long}. + * @since 4.1 + * @see + * HTTP/1.1: Semantics and Content, section 6.5.12 + */ + URI_TOO_LONG(414, HttpStatusSeries.CLIENT_ERROR, "URI Too Long"), + /** + * {@code 414 Request-URI Too Long}. + * @see HTTP/1.1, section 10.4.15 + * @deprecated in favor of {@link #URI_TOO_LONG} which will be returned from {@code HttpStatus.valueOf(414)} + */ + @Deprecated + REQUEST_URI_TOO_LONG(414, HttpStatusSeries.CLIENT_ERROR, "Request-URI Too Long"), + /** + * {@code 415 Unsupported Media Type}. + * @see + * HTTP/1.1: Semantics and Content, section 6.5.13 + */ + UNSUPPORTED_MEDIA_TYPE(415, HttpStatusSeries.CLIENT_ERROR, "Unsupported Media Type"), + /** + * {@code 416 Requested Range Not Satisfiable}. + * @see HTTP/1.1: Range Requests, section 4.4 + */ + REQUESTED_RANGE_NOT_SATISFIABLE(416, HttpStatusSeries.CLIENT_ERROR, "Requested range not satisfiable"), + /** + * {@code 417 Expectation Failed}. + * @see + * HTTP/1.1: Semantics and Content, section 6.5.14 + */ + EXPECTATION_FAILED(417, HttpStatusSeries.CLIENT_ERROR, "Expectation Failed"), + /** + * {@code 418 I'm a teapot}. + * @see HTCPCP/1.0 + */ + I_AM_A_TEAPOT(418, HttpStatusSeries.CLIENT_ERROR, "I'm a teapot"), + /** + * @deprecated See + * + * WebDAV Draft Changes + */ + @Deprecated + INSUFFICIENT_SPACE_ON_RESOURCE(419, HttpStatusSeries.CLIENT_ERROR, "Insufficient Space On Resource"), + /** + * @deprecated See + * + * WebDAV Draft Changes + */ + @Deprecated + METHOD_FAILURE(420, HttpStatusSeries.CLIENT_ERROR, "Method Failure"), + /** + * @deprecated + * See + * WebDAV Draft Changes + */ + @Deprecated + DESTINATION_LOCKED(421, HttpStatusSeries.CLIENT_ERROR, "Destination Locked"), + /** + * {@code 422 Unprocessable Entity}. + * @see WebDAV + */ + UNPROCESSABLE_ENTITY(422, HttpStatusSeries.CLIENT_ERROR, "Unprocessable Entity"), + /** + * {@code 423 Locked}. + * @see WebDAV + */ + LOCKED(423, HttpStatusSeries.CLIENT_ERROR, "Locked"), + /** + * {@code 424 Failed Dependency}. + * @see WebDAV + */ + FAILED_DEPENDENCY(424, HttpStatusSeries.CLIENT_ERROR, "Failed Dependency"), + /** + * {@code 425 Too Early}. + * @since 5.2 + * @see RFC 8470 + */ + TOO_EARLY(425, HttpStatusSeries.CLIENT_ERROR, "Too Early"), + /** + * {@code 426 Upgrade Required}. + * @see Upgrading to TLS Within HTTP/1.1 + */ + UPGRADE_REQUIRED(426, HttpStatusSeries.CLIENT_ERROR, "Upgrade Required"), + /** + * {@code 428 Precondition Required}. + * @see Additional HTTP Status Codes + */ + PRECONDITION_REQUIRED(428, HttpStatusSeries.CLIENT_ERROR, "Precondition Required"), + /** + * {@code 429 Too Many Requests}. + * @see Additional HTTP Status Codes + */ + TOO_MANY_REQUESTS(429, HttpStatusSeries.CLIENT_ERROR, "Too Many Requests"), + /** + * {@code 431 Request Header Fields Too Large}. + * @see Additional HTTP Status Codes + */ + REQUEST_HEADER_FIELDS_TOO_LARGE(431, HttpStatusSeries.CLIENT_ERROR, "Request Header Fields Too Large"), + /** + * {@code 451 Unavailable For Legal Reasons}. + * @see + * An HTTP Status Code to Report Legal Obstacles + * @since 4.3 + */ + UNAVAILABLE_FOR_LEGAL_REASONS(451, HttpStatusSeries.CLIENT_ERROR, "Unavailable For Legal Reasons"), + + // --- 5xx Server Error --- + + /** + * {@code 500 Internal Server Error}. + * @see HTTP/1.1: Semantics and Content, section 6.6.1 + */ + INTERNAL_SERVER_ERROR(500, HttpStatusSeries.SERVER_ERROR, "Internal Server Error"), + /** + * {@code 501 Not Implemented}. + * @see HTTP/1.1: Semantics and Content, section 6.6.2 + */ + NOT_IMPLEMENTED(501, HttpStatusSeries.SERVER_ERROR, "Not Implemented"), + /** + * {@code 502 Bad Gateway}. + * @see HTTP/1.1: Semantics and Content, section 6.6.3 + */ + BAD_GATEWAY(502, HttpStatusSeries.SERVER_ERROR, "Bad Gateway"), + /** + * {@code 503 Service Unavailable}. + * @see HTTP/1.1: Semantics and Content, section 6.6.4 + */ + SERVICE_UNAVAILABLE(503, HttpStatusSeries.SERVER_ERROR, "Service Unavailable"), + /** + * {@code 504 Gateway Timeout}. + * @see HTTP/1.1: Semantics and Content, section 6.6.5 + */ + GATEWAY_TIMEOUT(504, HttpStatusSeries.SERVER_ERROR, "Gateway Timeout"), + /** + * {@code 505 HTTP Version Not Supported}. + * @see HTTP/1.1: Semantics and Content, section 6.6.6 + */ + HTTP_VERSION_NOT_SUPPORTED(505, HttpStatusSeries.SERVER_ERROR, "HTTP Version not supported"), + /** + * {@code 506 Variant Also Negotiates} + * @see Transparent Content Negotiation + */ + VARIANT_ALSO_NEGOTIATES(506, HttpStatusSeries.SERVER_ERROR, "Variant Also Negotiates"), + /** + * {@code 507 Insufficient Storage} + * @see WebDAV + */ + INSUFFICIENT_STORAGE(507, HttpStatusSeries.SERVER_ERROR, "Insufficient Storage"), + /** + * {@code 508 Loop Detected} + * @see WebDAV Binding Extensions + */ + LOOP_DETECTED(508, HttpStatusSeries.SERVER_ERROR, "Loop Detected"), + /** + * {@code 509 Bandwidth Limit Exceeded} + */ + BANDWIDTH_LIMIT_EXCEEDED(509, HttpStatusSeries.SERVER_ERROR, "Bandwidth Limit Exceeded"), + /** + * {@code 510 Not Extended} + * @see HTTP Extension Framework + */ + NOT_EXTENDED(510, HttpStatusSeries.SERVER_ERROR, "Not Extended"), + /** + * {@code 511 Network Authentication Required}. + * @see Additional HTTP Status Codes + */ + NETWORK_AUTHENTICATION_REQUIRED(511, HttpStatusSeries.SERVER_ERROR, "Network Authentication Required"); + + + private static final GlobalErrorCode[] VALUES; + + static { + VALUES = values(); + } + + + private final int value; + + private final HttpStatusSeries series; + + private final String reasonPhrase; + + @Override + public int value() { + return this.value; + } + + @Override + public String reasonPhrase() { + return this.reasonPhrase; + } + + /** + * Return the HTTP status series of this status code. + * @see HttpStatusSeries + */ + public HttpStatusSeries series() { + return this.series; + } + + @Override + public boolean is1xxInformational() { + return (series() == HttpStatusSeries.INFORMATIONAL); + } + + @Override + public boolean is2xxSuccessful() { + return (series() == HttpStatusSeries.SUCCESSFUL); + } + + @Override + public boolean is3xxRedirection() { + return (series() == HttpStatusSeries.REDIRECTION); + } + + @Override + public boolean is4xxClientError() { + return (series() == HttpStatusSeries.CLIENT_ERROR); + } + + @Override + public boolean is5xxServerError() { + return (series() == HttpStatusSeries.SERVER_ERROR); + } + + @Override + public boolean isError() { + return (is4xxClientError() || is5xxServerError()); + } + + /** + * Return a string representation of this status code. + */ + @Override + public String toString() { + return this.value + " " + name(); + } + + + /** + * Return the {@code GlobalErrorCode} enum constant with the specified numeric value. + * @param errorCode the numeric value of the enum to be returned + * @return the enum constant with the specified numeric value + * @throws IllegalArgumentException if this enum has no constant for the specified numeric value + */ + public static GlobalErrorCode valueOf(int errorCode) { + GlobalErrorCode code = resolve(errorCode); + if (code == null) { + throw new IllegalArgumentException("No matching constant for [" + errorCode + "]"); + } + return code; + } + + /** + * Resolve the given status code to an {@code GlobalErrorCode}, if possible. + * @param errorCode the HTTP status code (potentially non-standard) + * @return the corresponding {@code GlobalErrorCode}, or {@code null} if not found + */ + @Nullable + public static GlobalErrorCode resolve(int errorCode) { + // Use cached VALUES instead of values() to prevent array allocation. + for (GlobalErrorCode code : VALUES) { + if (code.value == errorCode) { + return code; + } + } + return null; + } + +} diff --git a/bubble-core/src/main/java/cn/fxbin/bubble/core/dataobject/HttpStatusSeries.java b/bubble-core/src/main/java/cn/fxbin/bubble/core/dataobject/HttpStatusSeries.java new file mode 100644 index 00000000..613fbaf0 --- /dev/null +++ b/bubble-core/src/main/java/cn/fxbin/bubble/core/dataobject/HttpStatusSeries.java @@ -0,0 +1,69 @@ +package cn.fxbin.bubble.core.dataobject; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import org.springframework.lang.Nullable; + +/** + * HttpStatusSeries + * + *

+ * Enumeration of HTTP status series. + * HTTP 响应状态码归类 + *

+ * + * @author fxbin + * @version v1.0 + * @since 2023/8/29 00:31 + * @see org.springframework.http.HttpStatus.Series + */ +@Getter +@AllArgsConstructor +public enum HttpStatusSeries { + + INFORMATIONAL(1), + SUCCESSFUL(2), + REDIRECTION(3), + CLIENT_ERROR(4), + SERVER_ERROR(5); + + private final int value; + + /** + * Return the integer value of this status series. Ranges from 1 to 5. + */ + public int value() { + return this.value; + } + + /** + * Return the {@code Series} enum constant for the supplied status code. + * @param statusCode the HTTP status code (potentially non-standard) + * @return the {@code Series} enum constant for the supplied status code + * @throws IllegalArgumentException if this enum has no corresponding constant + */ + public static HttpStatusSeries valueOf(int statusCode) { + HttpStatusSeries series = resolve(statusCode); + if (series == null) { + throw new IllegalArgumentException("No matching constant for [" + statusCode + "]"); + } + return series; + } + + /** + * Resolve the given status code to an {@code StatusCodeSeries}, if possible. + * @param statusCode the HTTP status code (potentially non-standard) + * @return the corresponding {@code Series}, or {@code null} if not found + */ + @Nullable + public static HttpStatusSeries resolve(int statusCode) { + int seriesCode = statusCode / 100; + for (HttpStatusSeries series : values()) { + if (series.value == seriesCode) { + return series; + } + } + return null; + } + +} diff --git a/bubble-core/src/main/java/cn/fxbin/bubble/core/model/PageRequest.java b/bubble-core/src/main/java/cn/fxbin/bubble/core/dataobject/PageRequest.java similarity index 97% rename from bubble-core/src/main/java/cn/fxbin/bubble/core/model/PageRequest.java rename to bubble-core/src/main/java/cn/fxbin/bubble/core/dataobject/PageRequest.java index a47fae11..41ccabc7 100644 --- a/bubble-core/src/main/java/cn/fxbin/bubble/core/model/PageRequest.java +++ b/bubble-core/src/main/java/cn/fxbin/bubble/core/dataobject/PageRequest.java @@ -1,4 +1,4 @@ -package cn.fxbin.bubble.core.model; +package cn.fxbin.bubble.core.dataobject; import io.swagger.v3.oas.annotations.media.Schema; import jakarta.validation.constraints.Min; diff --git a/bubble-core/src/main/java/cn/fxbin/bubble/core/model/PageResult.java b/bubble-core/src/main/java/cn/fxbin/bubble/core/dataobject/PageResult.java similarity index 98% rename from bubble-core/src/main/java/cn/fxbin/bubble/core/model/PageResult.java rename to bubble-core/src/main/java/cn/fxbin/bubble/core/dataobject/PageResult.java index d13098c2..b1344b24 100644 --- a/bubble-core/src/main/java/cn/fxbin/bubble/core/model/PageResult.java +++ b/bubble-core/src/main/java/cn/fxbin/bubble/core/dataobject/PageResult.java @@ -1,4 +1,4 @@ -package cn.fxbin.bubble.core.model; +package cn.fxbin.bubble.core.dataobject; import io.swagger.v3.oas.annotations.media.Schema; import lombok.Getter; diff --git a/bubble-core/src/main/java/cn/fxbin/bubble/core/model/Result.java b/bubble-core/src/main/java/cn/fxbin/bubble/core/dataobject/Result.java similarity index 79% rename from bubble-core/src/main/java/cn/fxbin/bubble/core/model/Result.java rename to bubble-core/src/main/java/cn/fxbin/bubble/core/dataobject/Result.java index 20059b3e..d59767b9 100644 --- a/bubble-core/src/main/java/cn/fxbin/bubble/core/model/Result.java +++ b/bubble-core/src/main/java/cn/fxbin/bubble/core/dataobject/Result.java @@ -1,4 +1,4 @@ -package cn.fxbin.bubble.core.model; +package cn.fxbin.bubble.core.dataobject; import cn.fxbin.bubble.core.exception.ServiceException; import com.fasterxml.jackson.annotation.JsonInclude; @@ -39,12 +39,12 @@ public class Result implements Serializable { public Result() { } - private Result(ResultCode resultCode) { - this(resultCode.code, resultCode.msg); + private Result(ErrorCode errorCode) { + this(errorCode.value(), errorCode.reasonPhrase()); } - private Result(ResultCode resultCode, T data) { - this(resultCode.code, resultCode.msg, data); + private Result(ErrorCode errorCode, T data) { + this(errorCode.value(), errorCode.reasonPhrase(), data); } private Result(int errcode, String errmsg) { @@ -68,7 +68,7 @@ public Result(int errcode, String errmsg, T data) { public static boolean isSuccess(@Nullable Result result) { return Optional.ofNullable(result) .map(r -> r.errcode) - .map(code -> ResultCode.SUCCESS.code == code) + .map(code -> GlobalErrorCode.OK.value() == code) .orElse(Boolean.FALSE); } @@ -93,7 +93,7 @@ public static boolean isNotSuccess(@Nullable Result result) { * @return java.lang.Integer */ public static Integer getErrCode(@Nullable Result result) { - return Optional.ofNullable(result).isPresent() ? Optional.of(result.errcode).get() : ResultCode.FAILURE.code; + return Optional.ofNullable(result).isPresent() ? Optional.of(result.errcode).get() : GlobalErrorCode.INTERNAL_SERVER_ERROR.value(); } @@ -105,7 +105,7 @@ public static Integer getErrCode(@Nullable Result result) { * @return java.lang.String */ public static String getErrMsg(@Nullable Result result) { - return Optional.ofNullable(result).isPresent() ? Optional.of(result.errmsg).get() : ResultCode.FAILURE.msg; + return Optional.ofNullable(result).isPresent() ? Optional.of(result.errmsg).get() : GlobalErrorCode.INTERNAL_SERVER_ERROR.reasonPhrase(); } @@ -120,7 +120,7 @@ public static String getErrMsg(@Nullable Result result) { @Nullable public static T getData(@Nullable Result result) { return Optional.ofNullable(result) - .filter(r -> r.errcode == ResultCode.SUCCESS.code) + .filter(r -> r.errcode == GlobalErrorCode.OK.value()) .map(r -> r.data) .orElse(null); } @@ -134,7 +134,7 @@ public static T getData(@Nullable Result result) { * @return cn.fxbin.bubble.core.model.Result */ public static Result success() { - return new Result<>(ResultCode.SUCCESS); + return new Result<>(GlobalErrorCode.OK); } @@ -147,7 +147,7 @@ public static Result success() { * @return cn.fxbin.bubble.core.model.Result */ public static Result success(@Nullable T data) { - return new Result<>(ResultCode.SUCCESS, data); + return new Result<>(GlobalErrorCode.OK, data); } @@ -170,12 +170,12 @@ public static Result status(boolean status, String errmsg) { * * @since 2020/3/25 22:51 * @param status 错误状态 - * @param resultCode cn.fxbin.bubble.core.model.ResultesultCode + * @param errorCode cn.fxbin.bubble.core.model.ResultesultCode * @param 泛型标记 * @return cn.fxbin.bubble.core.model.Result */ - public static Result status(boolean status, ResultCode resultCode) { - return status ? Result.success() : Result.failure(resultCode); + public static Result status(boolean status, ErrorCode errorCode) { + return status ? Result.success() : Result.failure(errorCode); } @@ -187,7 +187,7 @@ public static Result status(boolean status, ResultCode resultCode) { * @return cn.fxbin.bubble.core.model.Result */ public static Result failure(String errmsg) { - return new Result<>(ResultCode.FAILURE.code, errmsg); + return new Result<>(GlobalErrorCode.INTERNAL_SERVER_ERROR.value(), errmsg); } @@ -205,15 +205,16 @@ public static Result failure(int errcode, String errmsg) { /** + * 失败 * failure * - * @since 2020/3/25 22:52 - * @param resultCode cn.fxbin.bubble.core.model.ResultesultCode - * @param errmsg 错误信息 + * @param errmsg 错误信息 + * @param errorCode 错误代码 * @return cn.fxbin.bubble.core.model.Result + * @since 2020/3/25 22:52 */ - public static Result failure(ResultCode resultCode, String errmsg) { - return new Result<>(resultCode.code, errmsg); + public static Result failure(ErrorCode errorCode, String errmsg) { + return new Result<>(errorCode.value(), errmsg); } @@ -221,11 +222,11 @@ public static Result failure(ResultCode resultCode, String errmsg) { * failure * * @since 2020/3/25 22:53 - * @param resultCode cn.fxbin.bubble.core.model.ResultesultCode + * @param errorCode cn.fxbin.bubble.core.model.ResultesultCode * @return cn.fxbin.bubble.core.model.Result */ - public static Result failure(ResultCode resultCode) { - return new Result<>(resultCode); + public static Result failure(ErrorCode errorCode) { + return new Result<>(errorCode); } diff --git a/bubble-core/src/main/java/cn/fxbin/bubble/core/enumeration/IEnum.java b/bubble-core/src/main/java/cn/fxbin/bubble/core/enumeration/IEnum.java index 1f1a2f6e..fa1d5f6f 100644 --- a/bubble-core/src/main/java/cn/fxbin/bubble/core/enumeration/IEnum.java +++ b/bubble-core/src/main/java/cn/fxbin/bubble/core/enumeration/IEnum.java @@ -14,7 +14,7 @@ public interface IEnum { * * @return int */ - int getValue(); + int value(); /** @@ -22,6 +22,6 @@ public interface IEnum { * * @return {@link String} */ - String getName(); + String name(); } diff --git a/bubble-core/src/main/java/cn/fxbin/bubble/core/enumeration/IEnumHelper.java b/bubble-core/src/main/java/cn/fxbin/bubble/core/enumeration/IEnumHelper.java index 89569a76..6d7d8278 100644 --- a/bubble-core/src/main/java/cn/fxbin/bubble/core/enumeration/IEnumHelper.java +++ b/bubble-core/src/main/java/cn/fxbin/bubble/core/enumeration/IEnumHelper.java @@ -1,6 +1,7 @@ package cn.fxbin.bubble.core.enumeration; import cn.fxbin.bubble.core.exception.InvalidEnumValueException; +import lombok.experimental.UtilityClass; import java.util.Arrays; import java.util.List; @@ -13,6 +14,7 @@ * @version v1.0 * @since 2022/10/26 11:49 */ +@UtilityClass public class IEnumHelper { /** @@ -23,9 +25,9 @@ public class IEnumHelper { * @param 泛型 * @return true 存在,false 不存在 */ - public static boolean existByValue(int value, T[] enums) { + public boolean existByValue(int value, T[] enums) { return Arrays.stream(enums) - .anyMatch(e -> e.getValue() == value); + .anyMatch(e -> e.value() == value); } /** @@ -36,9 +38,9 @@ public static boolean existByValue(int value, T[] enums) { * @param 泛型 * @return 枚举对象 */ - public static T getByValue(int value, T[] enums) { + public T valueOf(int value, T[] enums) { return Arrays.stream(enums) - .filter(e -> e.getValue() == value) + .filter(e -> e.value() == value) .findAny() .orElseThrow(InvalidEnumValueException::new); } @@ -50,9 +52,9 @@ public static T getByValue(int value, T[] enums) { * @param 泛型 * @return 枚举对象列表 */ - public static List getList(T[] enums) { + public List allOf(T[] enums) { return Arrays.stream(enums) - .filter(e -> e.getValue() != -1) + .filter(e -> e.value() != -1) .collect(Collectors.toList()); } diff --git a/bubble-core/src/main/java/cn/fxbin/bubble/core/enumeration/YesOrNo.java b/bubble-core/src/main/java/cn/fxbin/bubble/core/enumeration/YesOrNo.java new file mode 100644 index 00000000..abcd8ce1 --- /dev/null +++ b/bubble-core/src/main/java/cn/fxbin/bubble/core/enumeration/YesOrNo.java @@ -0,0 +1,37 @@ +package cn.fxbin.bubble.core.enumeration; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +/** + * YesOrNo + * + * @author fxbin + * @version v1.0 + * @since 2020/3/30 15:54 + */ +@AllArgsConstructor +public enum YesOrNo implements IEnum { + + /** + * Yes + */ + YES(1, "Y"), + + /** + * No + */ + NO(0, "N"); + + + private final int value; + + @Getter + private final String name; + + @Override + public int value() { + return this.value; + } + +} diff --git a/bubble-core/src/main/java/cn/fxbin/bubble/core/exception/ServiceException.java b/bubble-core/src/main/java/cn/fxbin/bubble/core/exception/ServiceException.java index 30e5d7d9..54fcb892 100644 --- a/bubble-core/src/main/java/cn/fxbin/bubble/core/exception/ServiceException.java +++ b/bubble-core/src/main/java/cn/fxbin/bubble/core/exception/ServiceException.java @@ -1,8 +1,8 @@ package cn.fxbin.bubble.core.exception; import cn.fxbin.bubble.core.logging.LoggerMessageFormat; -import cn.fxbin.bubble.core.model.Result; -import cn.fxbin.bubble.core.model.ResultCode; +import cn.fxbin.bubble.core.dataobject.Result; +import cn.fxbin.bubble.core.dataobject.ErrorCode; import lombok.Getter; /** @@ -37,15 +37,15 @@ public ServiceException(Result result) { this.errmsg = result.getErrmsg(); } - public ServiceException(ResultCode resultCode) { - super(resultCode.getMsg()); - this.errcode = resultCode.getCode(); - this.errmsg = resultCode.getMsg(); + public ServiceException(ErrorCode errorCode) { + super(errorCode.reasonPhrase()); + this.errcode = errorCode.value(); + this.errmsg = errorCode.reasonPhrase(); } - public ServiceException(ResultCode resultCode, String errmsg, Object... args) { + public ServiceException(ErrorCode errorCode, String errmsg, Object... args) { super(LoggerMessageFormat.format(errmsg, args)); - this.errcode = resultCode.getCode(); + this.errcode = errorCode.value(); this.errmsg = LoggerMessageFormat.format(errmsg, args); } diff --git a/bubble-core/src/main/java/cn/fxbin/bubble/core/exception/UtilException.java b/bubble-core/src/main/java/cn/fxbin/bubble/core/exception/UtilException.java index b38f3761..1fe7a3ac 100644 --- a/bubble-core/src/main/java/cn/fxbin/bubble/core/exception/UtilException.java +++ b/bubble-core/src/main/java/cn/fxbin/bubble/core/exception/UtilException.java @@ -1,7 +1,7 @@ package cn.fxbin.bubble.core.exception; import cn.fxbin.bubble.core.logging.LoggerMessageFormat; -import cn.fxbin.bubble.core.model.ResultCode; +import cn.fxbin.bubble.core.dataobject.ErrorCode; import lombok.Getter; /** @@ -24,10 +24,10 @@ public UtilException(String errmsg) { this.errmsg = errmsg; } - public UtilException(ResultCode resultCode) { - super(resultCode.getMsg()); - this.errcode = resultCode.getCode(); - this.errmsg = resultCode.getMsg(); + public UtilException(ErrorCode errorCode) { + super(errorCode.reasonPhrase()); + this.errcode = errorCode.value(); + this.errmsg = errorCode.reasonPhrase(); } public UtilException(Integer errcode, String errmsg) { diff --git a/bubble-core/src/main/java/cn/fxbin/bubble/core/model/ResultCode.java b/bubble-core/src/main/java/cn/fxbin/bubble/core/model/ResultCode.java deleted file mode 100644 index 504f2e81..00000000 --- a/bubble-core/src/main/java/cn/fxbin/bubble/core/model/ResultCode.java +++ /dev/null @@ -1,111 +0,0 @@ -package cn.fxbin.bubble.core.model; - -import lombok.AllArgsConstructor; -import lombok.Getter; - -/** - * ResultCode - * - * 0 标识成功, -1 为默认失败状态码 - * - * 一般情况下,建议使用 HTTP 响应状态码 - * - * - * @author fxbin - * @version v1.0 - * @since 2020/3/20 17:19 - */ -@Getter -@AllArgsConstructor -public enum ResultCode { - - /** - * 接口调用成功 - */ - SUCCESS(0, "Request Successful"), - - /** - * 服务器暂不可用,建议稍候重试。建议重试次数不超过3次。 - */ - FAILURE(-1, "System Busy"), - - /** - * 请求参数有误 - */ - BAD_REQUEST(400, "Bad Request"), - - /** - * Unauthorized - */ - UNAUTHORIZED(401, "Unauthorized"), - - /** - * Forbidden - */ - FORBIDDEN(403, "Forbidden"), - - /** - * 找不到地址 - */ - NOT_FOUND(404, "Not Found"), - - /** - * 不支持当前请求方法 - */ - METHOD_NOT_ALLOWED(405, "Method Not Allowed"), - - /** - * 不接受的媒体类型 - */ - UNSUPPORTED_MEDIA_TYPE(415, "Unsupported Media Type"), - - /** - * 服务异常 - */ - INTERNAL_SERVER_ERROR(500, "Internal Server Error"), - - - // === 自定义 - // 400|20--40 参数异常[20-30] token认证问题异常[31-35] - - /** - * 消息不能读取 - */ - MESSAGE_NOT_READABLE(40405,"Message Not Readable"), - - /** - * 重定向至登录页面 - */ - REDIRECT_LOGIN_CODE(30302, "Please login again"), - - - /** - * 请求参数缺失 - */ - REQUEST_PARAM_MISSING_ERROR(40021, "Request parameter missing error"), - - /** - * 请求参数格式有误 - */ - REQUEST_PARAM_FORMAT_ERROR(40023, "Request parameter format is incorrect"), - - /** - * 请求参数校验错误 - */ - REQUEST_PARAM_VALIDATION_ERROR(40025, "Request parameter validation error"), - - /** - * access toekn 过期 - */ - AUTHORIZATION_ACCESS_TOKEN_EXPIRED(40031, "access token 过期"), - - /** - * refresh token 过期 - */ - AUTHORIZATION_REFRESH_TOKEN_EXPIRED(40032, "refresh token 过期"); - - - final int code; - - final String msg; -} diff --git a/bubble-spring-boot-starters/bubble-spring-boot-starter-data-mybatis-plus/src/main/java/cn/fxbin/bubble/data/mybatisplus/mapper/BaseMapperX.java b/bubble-spring-boot-starters/bubble-spring-boot-starter-data-mybatis-plus/src/main/java/cn/fxbin/bubble/data/mybatisplus/mapper/BaseMapperX.java index e030ef90..031fb4b4 100644 --- a/bubble-spring-boot-starters/bubble-spring-boot-starter-data-mybatis-plus/src/main/java/cn/fxbin/bubble/data/mybatisplus/mapper/BaseMapperX.java +++ b/bubble-spring-boot-starters/bubble-spring-boot-starter-data-mybatis-plus/src/main/java/cn/fxbin/bubble/data/mybatisplus/mapper/BaseMapperX.java @@ -1,25 +1,19 @@ package cn.fxbin.bubble.data.mybatisplus.mapper; -import cn.fxbin.bubble.core.model.PageRequest; -import cn.fxbin.bubble.core.model.PageResult; +import cn.fxbin.bubble.core.dataobject.PageRequest; +import cn.fxbin.bubble.core.dataobject.PageResult; import cn.fxbin.bubble.data.mybatisplus.util.PageUtils; import com.baomidou.mybatisplus.core.conditions.Wrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.core.metadata.IPage; -import com.baomidou.mybatisplus.core.metadata.TableInfo; -import com.baomidou.mybatisplus.core.metadata.TableInfoHelper; -import com.baomidou.mybatisplus.core.toolkit.Assert; -import com.baomidou.mybatisplus.core.toolkit.StringUtils; import com.baomidou.mybatisplus.core.toolkit.support.SFunction; import com.baomidou.mybatisplus.extension.toolkit.Db; import org.apache.ibatis.annotations.Param; -import java.io.Serializable; import java.util.Collection; import java.util.List; -import java.util.Objects; /** * BaseMapperX diff --git a/bubble-spring-boot-starters/bubble-spring-boot-starter-data-mybatis-plus/src/main/java/cn/fxbin/bubble/data/mybatisplus/util/PageUtils.java b/bubble-spring-boot-starters/bubble-spring-boot-starter-data-mybatis-plus/src/main/java/cn/fxbin/bubble/data/mybatisplus/util/PageUtils.java index 999574ab..3c3933c8 100644 --- a/bubble-spring-boot-starters/bubble-spring-boot-starter-data-mybatis-plus/src/main/java/cn/fxbin/bubble/data/mybatisplus/util/PageUtils.java +++ b/bubble-spring-boot-starters/bubble-spring-boot-starter-data-mybatis-plus/src/main/java/cn/fxbin/bubble/data/mybatisplus/util/PageUtils.java @@ -1,7 +1,7 @@ package cn.fxbin.bubble.data.mybatisplus.util; -import cn.fxbin.bubble.core.model.PageRequest; -import cn.fxbin.bubble.core.model.PageResult; +import cn.fxbin.bubble.core.dataobject.PageRequest; +import cn.fxbin.bubble.core.dataobject.PageResult; import cn.fxbin.bubble.core.util.CollectionUtils; import cn.hutool.core.util.PageUtil; import com.baomidou.mybatisplus.core.metadata.IPage; diff --git a/bubble-spring-boot-starters/bubble-spring-boot-starter-openfeign/src/main/java/cn/fxbin/bubble/openfeign/codec/CustomizeFeignErrorDecoder.java b/bubble-spring-boot-starters/bubble-spring-boot-starter-openfeign/src/main/java/cn/fxbin/bubble/openfeign/codec/CustomizeFeignErrorDecoder.java index 2b111b21..028781b5 100644 --- a/bubble-spring-boot-starters/bubble-spring-boot-starter-openfeign/src/main/java/cn/fxbin/bubble/openfeign/codec/CustomizeFeignErrorDecoder.java +++ b/bubble-spring-boot-starters/bubble-spring-boot-starter-openfeign/src/main/java/cn/fxbin/bubble/openfeign/codec/CustomizeFeignErrorDecoder.java @@ -1,7 +1,7 @@ package cn.fxbin.bubble.openfeign.codec; import cn.fxbin.bubble.core.exception.ServiceException; -import cn.fxbin.bubble.core.model.Result; +import cn.fxbin.bubble.core.dataobject.Result; import cn.fxbin.bubble.core.util.JsonUtils; import cn.fxbin.bubble.core.util.StringUtils; import feign.FeignException; diff --git a/bubble-spring-boot-starters/bubble-spring-boot-starter-openfeign/src/main/java/cn/fxbin/bubble/openfeign/handler/CustomizeUrlBlockHandler.java b/bubble-spring-boot-starters/bubble-spring-boot-starter-openfeign/src/main/java/cn/fxbin/bubble/openfeign/handler/CustomizeUrlBlockHandler.java index 4557c596..8c10f9e2 100644 --- a/bubble-spring-boot-starters/bubble-spring-boot-starter-openfeign/src/main/java/cn/fxbin/bubble/openfeign/handler/CustomizeUrlBlockHandler.java +++ b/bubble-spring-boot-starters/bubble-spring-boot-starter-openfeign/src/main/java/cn/fxbin/bubble/openfeign/handler/CustomizeUrlBlockHandler.java @@ -1,6 +1,6 @@ package cn.fxbin.bubble.openfeign.handler; -import cn.fxbin.bubble.core.model.Result; +import cn.fxbin.bubble.core.dataobject.Result; import cn.fxbin.bubble.core.util.JsonUtils; import cn.fxbin.bubble.core.util.MimeTypeUtils; import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler; diff --git a/bubble-spring-boot-starters/bubble-spring-boot-starter-web/src/main/java/cn/fxbin/bubble/web/handler/DefaultGlobalExceptionHandler.java b/bubble-spring-boot-starters/bubble-spring-boot-starter-web/src/main/java/cn/fxbin/bubble/web/handler/DefaultGlobalExceptionHandler.java index 3ab65289..33294e27 100644 --- a/bubble-spring-boot-starters/bubble-spring-boot-starter-web/src/main/java/cn/fxbin/bubble/web/handler/DefaultGlobalExceptionHandler.java +++ b/bubble-spring-boot-starters/bubble-spring-boot-starter-web/src/main/java/cn/fxbin/bubble/web/handler/DefaultGlobalExceptionHandler.java @@ -1,8 +1,9 @@ package cn.fxbin.bubble.web.handler; +import cn.fxbin.bubble.core.dataobject.GlobalErrorCode; import cn.fxbin.bubble.core.exception.ServiceException; -import cn.fxbin.bubble.core.model.Result; -import cn.fxbin.bubble.core.model.ResultCode; +import cn.fxbin.bubble.core.dataobject.Result; +import cn.fxbin.bubble.core.dataobject.ErrorCode; import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.databind.JsonMappingException; import lombok.extern.slf4j.Slf4j; @@ -42,7 +43,7 @@ public Result exceptionHandler(Exception exception) { @ExceptionHandler(value = ServiceException.class) public Result exceptionHandler(ServiceException ex) { log.warn("[ServiceException]", ex); - return Result.failure((ex.getErrcode() == 0 ? ResultCode.FAILURE.getCode() : ex.getErrcode()), ex.getMessage()); + return Result.failure((ErrorCode.valueOf(ex.getErrcode()).isError() ? GlobalErrorCode.INTERNAL_SERVER_ERROR.value() : ex.getErrcode()), ex.getMessage()); } /** @@ -54,19 +55,18 @@ public Result exceptionHandler(ServiceException ex) { @ExceptionHandler(value = MissingServletRequestParameterException.class) public Result missingServletRequestParameterExceptionHandler(MissingServletRequestParameterException exception) { log.warn("MissingServletRequestParameterExceptionHandler", exception); - return Result.failure(ResultCode.BAD_REQUEST, String.format("请求参数缺失:%s", exception.getParameterName())); + return Result.failure(GlobalErrorCode.BAD_REQUEST, String.format("请求参数缺失:%s", exception.getParameterName())); } /** * 处理 SpringMVC 请求参数类型错误 - * * 例如说,接口上设置了 @RequestParam("xx") 参数为 Integer,结果传递 xx 参数类型为 String */ @ResponseStatus(HttpStatus.BAD_REQUEST) @ExceptionHandler(MethodArgumentTypeMismatchException.class) public Result methodArgumentTypeMismatchExceptionHandler(MethodArgumentTypeMismatchException exception) { log.warn("MethodArgumentTypeMismatchExceptionHandler", exception); - return Result.failure(ResultCode.BAD_REQUEST, String.format("请求参数类型错误:%s", exception.getMessage())); + return Result.failure(GlobalErrorCode.BAD_REQUEST, String.format("请求参数类型错误:%s", exception.getMessage())); } @ResponseStatus(HttpStatus.BAD_REQUEST) @@ -74,7 +74,7 @@ public Result methodArgumentTypeMismatchExceptionHandler(MethodArgumentTypeMi public Result constraintViolationExceptionHandler(ConstraintViolationException exception) { log.warn("ConstraintViolationExceptionHandler", exception); ConstraintViolation constraintViolation = exception.getConstraintViolations().iterator().next(); - return Result.failure(ResultCode.BAD_REQUEST, String.format("请求参数不正确:%s", constraintViolation.getMessage())); + return Result.failure(GlobalErrorCode.BAD_REQUEST, String.format("请求参数不正确:%s", constraintViolation.getMessage())); } @ResponseStatus(HttpStatus.BAD_REQUEST) @@ -82,9 +82,9 @@ public Result constraintViolationExceptionHandler(ConstraintViolationExceptio public Result bodyValidExceptionHandler(MethodArgumentNotValidException exception) { List fieldErrors = exception.getBindingResult().getFieldErrors(); // 取出所有的校验不通过的描述 - List fieldErrorMessages = fieldErrors.stream().map(FieldError::getDefaultMessage).collect(Collectors.toList()); + List fieldErrorMessages = fieldErrors.stream().map(FieldError::getDefaultMessage).toList(); log.warn("MethodArgumentNotValidException: {}", exception.getMessage()); - return Result.failure(ResultCode.BAD_REQUEST, fieldErrorMessages.toString()); + return Result.failure(GlobalErrorCode.BAD_REQUEST, fieldErrorMessages.toString()); } @ResponseStatus(HttpStatus.BAD_REQUEST) @@ -94,8 +94,8 @@ public Result bindExceptionHandler(BindException exception) { FieldError fieldError = exception.getFieldError(); // 断言,避免告警 assert fieldError != null; - return Result.failure(ResultCode.BAD_REQUEST, - ObjectUtils.isEmpty(fieldError.getDefaultMessage()) ? ResultCode.BAD_REQUEST.getMsg(): fieldError.getDefaultMessage()); + return Result.failure(GlobalErrorCode.BAD_REQUEST, + ObjectUtils.isEmpty(fieldError.getDefaultMessage()) ? GlobalErrorCode.BAD_REQUEST.reasonPhrase(): fieldError.getDefaultMessage()); } @ResponseStatus(HttpStatus.BAD_REQUEST) diff --git a/bubble-spring-boot-starters/bubble-spring-boot-starter-web/src/main/java/cn/fxbin/bubble/web/servlet/BaseController.java b/bubble-spring-boot-starters/bubble-spring-boot-starter-web/src/main/java/cn/fxbin/bubble/web/servlet/BaseController.java index f9b6511f..786b41d7 100644 --- a/bubble-spring-boot-starters/bubble-spring-boot-starter-web/src/main/java/cn/fxbin/bubble/web/servlet/BaseController.java +++ b/bubble-spring-boot-starters/bubble-spring-boot-starter-web/src/main/java/cn/fxbin/bubble/web/servlet/BaseController.java @@ -1,7 +1,8 @@ package cn.fxbin.bubble.web.servlet; -import cn.fxbin.bubble.core.model.Result; -import cn.fxbin.bubble.core.model.ResultCode; +import cn.fxbin.bubble.core.dataobject.GlobalErrorCode; +import cn.fxbin.bubble.core.dataobject.Result; +import cn.fxbin.bubble.core.dataobject.ErrorCode; import lombok.extern.slf4j.Slf4j; import org.springframework.lang.Nullable; @@ -54,7 +55,7 @@ public Result success(@Nullable T data) { * @return cn.fxbin.bubble.core.model.Result */ public Result failure(String errmsg) { - return Result.failure(ResultCode.FAILURE, errmsg); + return Result.failure(GlobalErrorCode.INTERNAL_SERVER_ERROR, errmsg); } } diff --git a/bubble-spring-boot-starters/bubble-spring-boot-starter-web/src/main/java/cn/fxbin/bubble/web/servlet/DefaultErrorController.java b/bubble-spring-boot-starters/bubble-spring-boot-starter-web/src/main/java/cn/fxbin/bubble/web/servlet/DefaultErrorController.java index 6be957b4..5c5d25d8 100644 --- a/bubble-spring-boot-starters/bubble-spring-boot-starter-web/src/main/java/cn/fxbin/bubble/web/servlet/DefaultErrorController.java +++ b/bubble-spring-boot-starters/bubble-spring-boot-starter-web/src/main/java/cn/fxbin/bubble/web/servlet/DefaultErrorController.java @@ -1,6 +1,6 @@ package cn.fxbin.bubble.web.servlet; -import cn.fxbin.bubble.core.model.Result; +import cn.fxbin.bubble.core.dataobject.Result; import cn.fxbin.bubble.core.util.BeanUtils; import org.springframework.boot.autoconfigure.web.ErrorProperties; import org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController;