mirror of
https://github.com/continew-org/continew-starter.git
synced 2025-09-09 20:57:23 +08:00
feat(core): 新增 JSON 格式字符串校验器
This commit is contained in:
@@ -28,7 +28,7 @@ import java.util.Arrays;
|
|||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 枚举校验注解校验器
|
* 枚举校验器
|
||||||
*
|
*
|
||||||
* @author Charles7c
|
* @author Charles7c
|
||||||
* @author Jasmine
|
* @author Jasmine
|
||||||
|
@@ -0,0 +1,66 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
|
||||||
|
* <p>
|
||||||
|
* 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
|
||||||
|
* <p>
|
||||||
|
* http://www.gnu.org/licenses/lgpl.html
|
||||||
|
* <p>
|
||||||
|
* 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 格式字符串校验注解
|
||||||
|
*
|
||||||
|
* <p>
|
||||||
|
* 校验字符串是否为 JSON 格式字符串
|
||||||
|
* {@code @JsonString(message = "必须为有效的 JSON 格式")} <br />
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @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<? extends Payload>[] payload() default {};
|
||||||
|
}
|
@@ -0,0 +1,38 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
|
||||||
|
* <p>
|
||||||
|
* 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
|
||||||
|
* <p>
|
||||||
|
* http://www.gnu.org/licenses/lgpl.html
|
||||||
|
* <p>
|
||||||
|
* 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<JsonString, String> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isValid(String value, ConstraintValidatorContext context) {
|
||||||
|
if (value == null) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return JSONUtil.isTypeJSON(value);
|
||||||
|
}
|
||||||
|
}
|
@@ -16,13 +16,12 @@
|
|||||||
|
|
||||||
package top.continew.starter.core.validation.constraints;
|
package top.continew.starter.core.validation.constraints;
|
||||||
|
|
||||||
import cn.hutool.core.text.CharSequenceUtil;
|
|
||||||
import cn.hutool.core.util.PhoneUtil;
|
import cn.hutool.core.util.PhoneUtil;
|
||||||
import jakarta.validation.ConstraintValidator;
|
import jakarta.validation.ConstraintValidator;
|
||||||
import jakarta.validation.ConstraintValidatorContext;
|
import jakarta.validation.ConstraintValidatorContext;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 手机号校验注解校验器
|
* 手机号校验器
|
||||||
*
|
*
|
||||||
* @author Charles7c
|
* @author Charles7c
|
||||||
* @since 2.10.0
|
* @since 2.10.0
|
||||||
@@ -31,7 +30,7 @@ public class MobileValidator implements ConstraintValidator<Mobile, String> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isValid(String value, ConstraintValidatorContext context) {
|
public boolean isValid(String value, ConstraintValidatorContext context) {
|
||||||
if (CharSequenceUtil.isBlank(value)) {
|
if (value == null) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return PhoneUtil.isMobile(value);
|
return PhoneUtil.isMobile(value);
|
||||||
|
@@ -63,7 +63,8 @@ public class IdempotentAspect {
|
|||||||
public Object around(ProceedingJoinPoint joinPoint, Idempotent idempotent) throws Throwable {
|
public Object around(ProceedingJoinPoint joinPoint, Idempotent idempotent) throws Throwable {
|
||||||
String cacheKey = this.getCacheKey(joinPoint, idempotent);
|
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());
|
throw new IdempotentException(idempotent.message());
|
||||||
}
|
}
|
||||||
// 执行目标方法
|
// 执行目标方法
|
||||||
|
Reference in New Issue
Block a user