diff --git a/continew-starter-core/src/main/java/top/continew/starter/core/validation/constraints/EnumValueValidator.java b/continew-starter-core/src/main/java/top/continew/starter/core/validation/constraints/EnumValueValidator.java index 0100f68f..bde17fea 100644 --- a/continew-starter-core/src/main/java/top/continew/starter/core/validation/constraints/EnumValueValidator.java +++ b/continew-starter-core/src/main/java/top/continew/starter/core/validation/constraints/EnumValueValidator.java @@ -28,7 +28,7 @@ import java.util.Arrays; import java.util.function.Function; /** - * 枚举校验注解校验器 + * 枚举校验器 * * @author Charles7c * @author Jasmine diff --git a/continew-starter-core/src/main/java/top/continew/starter/core/validation/constraints/JsonString.java b/continew-starter-core/src/main/java/top/continew/starter/core/validation/constraints/JsonString.java new file mode 100644 index 00000000..3c07d359 --- /dev/null +++ b/continew-starter-core/src/main/java/top/continew/starter/core/validation/constraints/JsonString.java @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. + *

+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * 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 top.continew.starter.core.validation.constraints; + +import jakarta.validation.Constraint; +import jakarta.validation.Payload; +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import static java.lang.annotation.ElementType.*; +import static java.lang.annotation.ElementType.TYPE_USE; + +/** + * JSON 格式字符串校验注解 + * + *

+ * 校验字符串是否为 JSON 格式字符串 + * {@code @JsonString(message = "必须为有效的 JSON 格式")}
+ *

+ * + * @author Charles7c + * @since 2.12.0 + */ +@Documented +@Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE}) +@Retention(RetentionPolicy.RUNTIME) +@Constraint(validatedBy = JsonStringValidator.class) +public @interface JsonString { + + /** + * 提示消息 + * + * @return 提示消息 + */ + String message() default "必须为有效的 JSON 格式"; + + /** + * 分组 + * + * @return 分组 + */ + Class[] groups() default {}; + + /** + * 负载 + * + * @return 负载 + */ + Class[] payload() default {}; +} \ No newline at end of file diff --git a/continew-starter-core/src/main/java/top/continew/starter/core/validation/constraints/JsonStringValidator.java b/continew-starter-core/src/main/java/top/continew/starter/core/validation/constraints/JsonStringValidator.java new file mode 100644 index 00000000..751b40c7 --- /dev/null +++ b/continew-starter-core/src/main/java/top/continew/starter/core/validation/constraints/JsonStringValidator.java @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. + *

+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * 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 top.continew.starter.core.validation.constraints; + +import cn.hutool.json.JSONUtil; +import jakarta.validation.ConstraintValidator; +import jakarta.validation.ConstraintValidatorContext; + +/** + * JSON 格式字符串校验器 + * + * @author Charles7c + * @since 2.12.0 + */ +public class JsonStringValidator implements ConstraintValidator { + + @Override + public boolean isValid(String value, ConstraintValidatorContext context) { + if (value == null) { + return true; + } + return JSONUtil.isTypeJSON(value); + } +} \ No newline at end of file diff --git a/continew-starter-core/src/main/java/top/continew/starter/core/validation/constraints/MobileValidator.java b/continew-starter-core/src/main/java/top/continew/starter/core/validation/constraints/MobileValidator.java index 1ec59422..e195e545 100644 --- a/continew-starter-core/src/main/java/top/continew/starter/core/validation/constraints/MobileValidator.java +++ b/continew-starter-core/src/main/java/top/continew/starter/core/validation/constraints/MobileValidator.java @@ -16,13 +16,12 @@ package top.continew.starter.core.validation.constraints; -import cn.hutool.core.text.CharSequenceUtil; import cn.hutool.core.util.PhoneUtil; import jakarta.validation.ConstraintValidator; import jakarta.validation.ConstraintValidatorContext; /** - * 手机号校验注解校验器 + * 手机号校验器 * * @author Charles7c * @since 2.10.0 @@ -31,7 +30,7 @@ public class MobileValidator implements ConstraintValidator { @Override public boolean isValid(String value, ConstraintValidatorContext context) { - if (CharSequenceUtil.isBlank(value)) { + if (value == null) { return true; } return PhoneUtil.isMobile(value); diff --git a/continew-starter-idempotent/src/main/java/top/continew/starter/idempotent/aop/IdempotentAspect.java b/continew-starter-idempotent/src/main/java/top/continew/starter/idempotent/aop/IdempotentAspect.java index 1c9ab32b..ab32d7f1 100644 --- a/continew-starter-idempotent/src/main/java/top/continew/starter/idempotent/aop/IdempotentAspect.java +++ b/continew-starter-idempotent/src/main/java/top/continew/starter/idempotent/aop/IdempotentAspect.java @@ -63,7 +63,8 @@ public class IdempotentAspect { public Object around(ProceedingJoinPoint joinPoint, Idempotent idempotent) throws Throwable { String cacheKey = this.getCacheKey(joinPoint, idempotent); // 如果键已存在,则抛出异常 - if (!RedisUtils.setIfAbsent(cacheKey,cacheKey, Duration.ofMillis(idempotent.unit().toMillis(idempotent.timeout())))) { + if (!RedisUtils.setIfAbsent(cacheKey, cacheKey, Duration.ofMillis(idempotent.unit() + .toMillis(idempotent.timeout())))) { throw new IdempotentException(idempotent.message()); } // 执行目标方法