From b446d327b18bb45a4cf3bb29bb64ff803969dead Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E6=98=95?= Date: Mon, 25 Mar 2024 16:07:21 +0800 Subject: [PATCH] =?UTF-8?q?1.=20=E5=8F=82=E6=95=B0=E6=A0=A1=E9=AA=8C?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=9B=BD=E9=99=85=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/cf/cs/base/config/LocaleConfig.java | 11 ++++++++++ .../java/com/cf/cs/base/misc/HelperUtils.java | 20 +++++++++---------- .../i18n/validationMessage_en_US.properties | 14 +++++++++++++ .../i18n/validationMessage_zh_CN.properties | 14 +++++++++++++ .../cs/protocol/pojo/base/BaseProtocol.java | 12 +++++------ .../protocol/pojo/dto/AddDictContentReq.java | 6 +++--- .../cf/cs/protocol/pojo/dto/BasePagedReq.java | 11 +++++----- .../cs/protocol/pojo/dto/DictContentReq.java | 4 ++-- .../cf/cs/protocol/pojo/dto/IdArrayReq.java | 8 ++++---- .../com/cf/cs/protocol/pojo/dto/LoginReq.java | 10 +++++----- .../cs/protocol/pojo/dto/NewUserDictReq.java | 6 +++--- .../pojo/dto/OperationLogDetailsReq.java | 4 ++-- .../pojo/dto/RegisterResourceReq.java | 4 ++-- .../cs/protocol/pojo/dto/RegisterUserReq.java | 14 ++++++------- .../cs/protocol/pojo/dto/RemoveDictReq.java | 2 +- .../cf/cs/protocol/pojo/dto/UserIdReq.java | 4 ++-- .../cf/cs/protocol/pojo/po/DictDetails.java | 4 ++-- .../cf/cs/protocol/pojo/po/ResourceInfo.java | 14 ++++++------- .../valids/impl/ValidHttpMethodImpl.java | 5 ++++- .../impl/ValidProtocolTimestampImpl.java | 9 ++++++--- 20 files changed, 109 insertions(+), 67 deletions(-) create mode 100644 cs-base/src/main/resources/i18n/validationMessage_en_US.properties create mode 100644 cs-base/src/main/resources/i18n/validationMessage_zh_CN.properties diff --git a/cs-base/src/main/java/com/cf/cs/base/config/LocaleConfig.java b/cs-base/src/main/java/com/cf/cs/base/config/LocaleConfig.java index af007d3..813fe54 100644 --- a/cs-base/src/main/java/com/cf/cs/base/config/LocaleConfig.java +++ b/cs-base/src/main/java/com/cf/cs/base/config/LocaleConfig.java @@ -2,7 +2,9 @@ package com.cf.cs.base.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.context.support.ReloadableResourceBundleMessageSource; import org.springframework.context.support.ResourceBundleMessageSource; +import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; import java.util.Locale; @@ -30,4 +32,13 @@ public class LocaleConfig { source.setDefaultEncoding("UTF-8"); return source; } + + @Bean + public LocalValidatorFactoryBean localValidatorFactoryBean() { + LocalValidatorFactoryBean localValidatorFactoryBean = new LocalValidatorFactoryBean(); + ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource(); + messageSource.setBasename("i18n/validationMessage"); + localValidatorFactoryBean.setValidationMessageSource(messageSource); + return localValidatorFactoryBean; + } } diff --git a/cs-base/src/main/java/com/cf/cs/base/misc/HelperUtils.java b/cs-base/src/main/java/com/cf/cs/base/misc/HelperUtils.java index 30635aa..60be3ba 100644 --- a/cs-base/src/main/java/com/cf/cs/base/misc/HelperUtils.java +++ b/cs-base/src/main/java/com/cf/cs/base/misc/HelperUtils.java @@ -7,9 +7,8 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import jakarta.servlet.http.HttpServletRequest; import jakarta.validation.ConstraintViolation; -import jakarta.validation.Validation; -import jakarta.validation.ValidatorFactory; -import org.hibernate.validator.HibernateValidator; +import org.springframework.context.i18n.LocaleContextHolder; +import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; import java.io.IOException; import java.io.InputStream; @@ -26,6 +25,7 @@ import java.util.ArrayList; import java.util.Enumeration; import java.util.HashMap; import java.util.List; +import java.util.Locale; import java.util.Map; import java.util.Set; @@ -35,12 +35,12 @@ import java.util.Set; * @author xajhuang @163.com */ public class HelperUtils { + private static final ObjectMapper OBJ_MAPPER = ObjectMapperProvider.getMapper(); + private HelperUtils() { throw new AssertionError("Instantiating utility class."); } - private static final ObjectMapper OBJ_MAPPER = ObjectMapperProvider.getMapper(); - /** * Gets json. * @@ -155,15 +155,13 @@ public class HelperUtils { */ public static List validate(T t, Class... groups) { Set> set; - ValidatorFactory validatorFactory = Validation.byProvider(HibernateValidator.class) - .configure() - .failFast(false) - .buildValidatorFactory(); + LocaleContextHolder.setLocale(Locale.forLanguageTag(ApiContextUtils.getLanguare().replace('_', '-'))); + LocalValidatorFactoryBean localValidatorFactory = SpringBootBeanUtils.getBean(LocalValidatorFactoryBean.class); if (groups != null && groups.length > 0) { - set = validatorFactory.getValidator().validate(t, groups); + set = localValidatorFactory.getValidator().validate(t, groups); } else { - set = validatorFactory.getValidator().validate(t); + set = localValidatorFactory.getValidator().validate(t); } List validateError = new ArrayList<>(); if (!set.isEmpty()) { diff --git a/cs-base/src/main/resources/i18n/validationMessage_en_US.properties b/cs-base/src/main/resources/i18n/validationMessage_en_US.properties new file mode 100644 index 0000000..e22320a --- /dev/null +++ b/cs-base/src/main/resources/i18n/validationMessage_en_US.properties @@ -0,0 +1,14 @@ +item.not_null=field cannot be null +item.not_empty=field cannot be empty string +item.value_range=Field value range [{min}, {max}] +item.value_min=Minimum value is {min} +array.value_range=Array/List size range [{min}, {max}] +array.not_empty=Arrays/List cannot be NULL and elements cannot be empty +timestamp.timeout=The difference between the field value and the current time cannot be greater than the timeout configuration +page.item_size=The value must be an integer multiple of 5 +uuid.format=UUID string must match format(xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx) +invalid.character=Invalid character strings exist +password.length=The password size must be SHA256 length +http.request_invalid=Not a legitimate HTTP request method +timeout.current.format=Request time[{0}] greater than current server time[{1}] +timeout.server.format=Request time [{0}] Delay exceeds current server time [{1}] Allowed range {2}(S) \ No newline at end of file diff --git a/cs-base/src/main/resources/i18n/validationMessage_zh_CN.properties b/cs-base/src/main/resources/i18n/validationMessage_zh_CN.properties new file mode 100644 index 0000000..f5b50e0 --- /dev/null +++ b/cs-base/src/main/resources/i18n/validationMessage_zh_CN.properties @@ -0,0 +1,14 @@ +item.not_null=\u5B57\u6BB5\u4E0D\u80FD\u4E3A NULL +item.not_empty=\u5B57\u6BB5\u4E0D\u80FD\u4E3A\u7A7A\u5B57\u7B26\u4E32 +item.value_range=\u5B57\u6BB5\u53D6\u503C\u8303\u56F4 [{min}, {max}] +item.value_min=\u6700\u5C0F\u503C\u4E3A {min} +array.value_range=\u6570\u7EC4/\u94FE\u8868\u5143\u7D20\u4E2A\u6570\u53D6\u503C\u8303\u56F4 [{min}, {max}] +array.not_empty=\u6570\u7EC4/\u94FE\u8868\u4E0D\u80FD\u4E3ANULL\u4E14\u5143\u7D20\u4E0D\u80FD\u4E3A\u7A7A +timestamp.timeout=\u5B57\u6BB5\u503C\u4E0E\u5F53\u524D\u65F6\u95F4\u5DEE\u4E0D\u80FD\u5927\u4E8E\u8D85\u65F6\u914D\u7F6E +page.item_size=\u53D6\u503C\u5FC5\u987B\u4E3A 5 \u7684\u6574\u6570\u500D +uuid.format=\u5FC5\u987B\u4E3A\u7B26\u5408UUID\u89C4\u8303(xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)\u7684\u5B57\u7B26\u4E32 +invalid_character=\u5B57\u7B26\u4E32\u5B58\u5728\u975E\u6CD5\u5B57\u7B26 +password.length=\u5BC6\u7801\u957F\u5EA6\u5FC5\u987B\u4E3ASHA256\u7F16\u7801\u540E\u7684\u957F\u5EA6 +http.request_invalid=\u4E0D\u662F\u5408\u6CD5\u7684 HTTP \u8BF7\u6C42\u65B9\u6CD5 +timeout.current.format=\u8BF7\u6C42\u65F6\u95F4[{0}] \u5927\u4E8E\u5F53\u524D\u670D\u52A1\u5668\u65F6\u95F4[{1}] +timeout.server.format=\u8BF7\u6C42\u65F6\u95F4[{0}] \u5EF6\u65F6\u8D85\u8FC7\u5F53\u524D\u670D\u52A1\u5668\u65F6\u95F4[{1}] \u5141\u8BB8\u8303\u56F4 {2}(S) \ No newline at end of file diff --git a/cs-protocol/src/main/java/com/cf/cs/protocol/pojo/base/BaseProtocol.java b/cs-protocol/src/main/java/com/cf/cs/protocol/pojo/base/BaseProtocol.java index e61fcbd..5ae8a68 100644 --- a/cs-protocol/src/main/java/com/cf/cs/protocol/pojo/base/BaseProtocol.java +++ b/cs-protocol/src/main/java/com/cf/cs/protocol/pojo/base/BaseProtocol.java @@ -30,16 +30,16 @@ public class BaseProtocol implements Serializable { @Serial private static final long serialVersionUID = -32326523643641L; - @NotNull(message = "ver 字段不能为空", groups = ValidGroups.ProtocolCommonValid.class) - @Range(min = 1, max = 9999, message = "ver 字段最小值为 1", groups = ValidGroups.ProtocolCommonValid.class) + @NotNull(message = "ver {item.not_null}", groups = ValidGroups.ProtocolCommonValid.class) + @Range(min = 1, max = 9999, message = "ver {item.value_range}", groups = ValidGroups.ProtocolCommonValid.class) private Integer ver; - @NotNull(message = "cryptoType 字段不能为空", groups = ValidGroups.ProtocolCommonValid.class) - @Range(min = 0, max = 4, message = "cryptoType 字段取值为 [0, 4]", groups = ValidGroups.ProtocolCommonValid.class) + @NotNull(message = "cryptoType {item.not_null}", groups = ValidGroups.ProtocolCommonValid.class) + @Range(min = 0, max = 4, message = "cryptoType {item.value_range}", groups = ValidGroups.ProtocolCommonValid.class) private Integer cryptoType; - @NotNull(message = "timeStamp 字段不能为空", groups = ValidGroups.ProtocolCommonValid.class) - @ValidProtocolTimestamp(message = "timeStamp 字段值与当前时间差不能大于超时配置", groups = ValidGroups.ProtocolCommonValid.class) + @NotNull(message = "timeStamp {item.not_null}", groups = ValidGroups.ProtocolCommonValid.class) + @ValidProtocolTimestamp(message = "timeStamp {timestamp.timeout}", groups = ValidGroups.ProtocolCommonValid.class) private Long timeStamp; @Valid diff --git a/cs-protocol/src/main/java/com/cf/cs/protocol/pojo/dto/AddDictContentReq.java b/cs-protocol/src/main/java/com/cf/cs/protocol/pojo/dto/AddDictContentReq.java index f978e7a..a4ac37b 100644 --- a/cs-protocol/src/main/java/com/cf/cs/protocol/pojo/dto/AddDictContentReq.java +++ b/cs-protocol/src/main/java/com/cf/cs/protocol/pojo/dto/AddDictContentReq.java @@ -27,10 +27,10 @@ import java.util.List; @Schema(name = "AddDictContentReq", description = "新增用户字典内容请求参数") @JsonInclude(JsonInclude.Include.NON_NULL) public class AddDictContentReq { - @NotBlank(message = "dictTpe 字典名称不能为空", groups = ValidGroups.DictReqValid.class) + @NotBlank(message = "dictTpe {item.not_empty}", groups = ValidGroups.DictReqValid.class) private String dictName; - @NotNull(message = "dictContent 字典内容值不能为 NULL", groups = ValidGroups.DictReqValid.class) - @Size(min = 1, max = 100, message = "字典内容至少需要指定 1 个,最多只能指定 100 个", groups = ValidGroups.DictReqValid.class) + @NotNull(message = "dictContent {item.not_null}", groups = ValidGroups.DictReqValid.class) + @Size(min = 1, max = 100, message = "DictDetails {array.value_range}", groups = ValidGroups.DictReqValid.class) private List<@Valid DictDetails> dictContent; } diff --git a/cs-protocol/src/main/java/com/cf/cs/protocol/pojo/dto/BasePagedReq.java b/cs-protocol/src/main/java/com/cf/cs/protocol/pojo/dto/BasePagedReq.java index ef87524..b87d4bb 100644 --- a/cs-protocol/src/main/java/com/cf/cs/protocol/pojo/dto/BasePagedReq.java +++ b/cs-protocol/src/main/java/com/cf/cs/protocol/pojo/dto/BasePagedReq.java @@ -4,12 +4,12 @@ import com.cf.cs.protocol.validation.group.ValidGroups; import com.cf.cs.protocol.validation.valids.ValidPageSize; import com.fasterxml.jackson.annotation.JsonInclude; import io.swagger.v3.oas.annotations.media.Schema; -import jakarta.validation.constraints.Max; import jakarta.validation.constraints.Min; import jakarta.validation.constraints.NotNull; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; +import org.hibernate.validator.constraints.Range; /** * The type Base paged req. @@ -23,15 +23,14 @@ import lombok.NoArgsConstructor; @Schema(name = "BasePagedReq", description = "分页请求基础参数") public class BasePagedReq { @Schema(description = "分页号,最小值为1") - @NotNull(message = "pageNumber 必须指定分页配置", groups = ValidGroups.BasePagedReqValid.class) + @NotNull(message = "pageNumber {item.not_null}", groups = ValidGroups.BasePagedReqValid.class) @Min(value = 1, message = "pageNumber 最小值为1", groups = ValidGroups.BasePagedReqValid.class) private Long pageNumber; @Schema(description = "分页大小,最小值为5, 最大值100, 取值必须为5的整数倍") - @NotNull(message = "pageSize 必须指定分页配置", groups = ValidGroups.BasePagedReqValid.class) - @ValidPageSize(message = "pageSize 取值必须为 5 的整数倍", groups = ValidGroups.BasePagedReqValid.class) - @Min(value = 5, message = "pageSize 最小值为5", groups = ValidGroups.BasePagedReqValid.class) - @Max(value = 100, message = "pageSize 最大值为100", groups = ValidGroups.BasePagedReqValid.class) + @NotNull(message = "pageSize {item.not_null}", groups = ValidGroups.BasePagedReqValid.class) + @ValidPageSize(message = "pageSize {page.item_size}", groups = ValidGroups.BasePagedReqValid.class) + @Range(min = 5, max = 100, message = "pageSize {item.value_range}", groups = ValidGroups.BasePagedReqValid.class) private Long pageSize; @Schema(description = "数据总数,未知时可以为空或者小于0, 存在时可以加快后端数据库查询速度") diff --git a/cs-protocol/src/main/java/com/cf/cs/protocol/pojo/dto/DictContentReq.java b/cs-protocol/src/main/java/com/cf/cs/protocol/pojo/dto/DictContentReq.java index a844f8e..cf1c79b 100644 --- a/cs-protocol/src/main/java/com/cf/cs/protocol/pojo/dto/DictContentReq.java +++ b/cs-protocol/src/main/java/com/cf/cs/protocol/pojo/dto/DictContentReq.java @@ -26,7 +26,7 @@ import java.util.List; @JsonInclude(JsonInclude.Include.NON_NULL) public class DictContentReq { @Schema(description = "字典名") - @NotNull(message = "dictTpe 字典名称不能为NULL", groups = ValidGroups.DictReqValid.class) - @Size(min = 1, max = 100, message = "dictTpe 单次查询字典名称条数为 [1, 100]", groups = ValidGroups.DictReqValid.class) + @NotNull(message = "dictTpe {item.not_null}", groups = ValidGroups.DictReqValid.class) + @Size(min = 1, max = 100, message = "dictTpe {array.value_range}", groups = ValidGroups.DictReqValid.class) private List<@NotBlank(message = "dictTpe 字典名称不能为空", groups = ValidGroups.DictReqValid.class) String> dictTpe; } diff --git a/cs-protocol/src/main/java/com/cf/cs/protocol/pojo/dto/IdArrayReq.java b/cs-protocol/src/main/java/com/cf/cs/protocol/pojo/dto/IdArrayReq.java index 8f449b9..e239f9d 100644 --- a/cs-protocol/src/main/java/com/cf/cs/protocol/pojo/dto/IdArrayReq.java +++ b/cs-protocol/src/main/java/com/cf/cs/protocol/pojo/dto/IdArrayReq.java @@ -26,10 +26,10 @@ public class IdArrayReq { /** * The Ids. */ - @NotNull(message = "ids 不能为 NULL", groups = ValidGroups.UserIdReqValid.class) - @Size(min = 1, max = 100, message = "ids 元素个数最小为1, 最大为100", groups = ValidGroups.UserIdReqValid.class) - List<@NotBlank(message = "guid 不能为空", groups = ValidGroups.UserIdReqValid.class) + @NotNull(message = "ids {item.not_null}", groups = ValidGroups.UserIdReqValid.class) + @Size(min = 1, max = 100, message = "ids {array.value_range}", groups = ValidGroups.UserIdReqValid.class) + List<@NotBlank(message = "guid {item.not_empty}", groups = ValidGroups.UserIdReqValid.class) @Pattern(regexp = "^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$", - message = "uid 必须为符号UUID规范(xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)的字符串", groups = ValidGroups.UserIdReqValid.class) + message = "uid {uuid.format}", groups = ValidGroups.UserIdReqValid.class) String> ids; } diff --git a/cs-protocol/src/main/java/com/cf/cs/protocol/pojo/dto/LoginReq.java b/cs-protocol/src/main/java/com/cf/cs/protocol/pojo/dto/LoginReq.java index 5f47ccb..6bb834f 100644 --- a/cs-protocol/src/main/java/com/cf/cs/protocol/pojo/dto/LoginReq.java +++ b/cs-protocol/src/main/java/com/cf/cs/protocol/pojo/dto/LoginReq.java @@ -22,23 +22,23 @@ import org.hibernate.validator.constraints.Length; @AllArgsConstructor @JsonInclude(JsonInclude.Include.NON_NULL) public class LoginReq { - @NotBlank(message = "username 用户名不能为空", groups = ValidGroups.LogoutReqValid.class) + @NotBlank(message = "username {item.not_empty}", groups = ValidGroups.LogoutReqValid.class) @Pattern(regexp = ConstValue.UN_EXPECT_REGEX_CHARS, flags = Pattern.Flag.CASE_INSENSITIVE, - message = "username 用户名存在非法字符串", + message = "username {invalid.character}", groups = ValidGroups.LogoutReqValid.class) private String username; /** * The Password. */ - @NotBlank(message = "password 密码不能为空", groups = ValidGroups.LoginReqValid.class) + @NotBlank(message = "password {item.not_empty}", groups = ValidGroups.LoginReqValid.class) @Length(min = 32, max = 64, - message = "password 密码长度必须为SHA256编码后的长度", + message = "password {password.length}", groups = ValidGroups.LoginReqValid.class) @Pattern(regexp = ConstValue.UN_EXPECT_REGEX_CHARS, flags = Pattern.Flag.CASE_INSENSITIVE, - message = "password 密码存在非法字符串", + message = "password {invalid.character}", groups = ValidGroups.LoginReqValid.class) private String password; } diff --git a/cs-protocol/src/main/java/com/cf/cs/protocol/pojo/dto/NewUserDictReq.java b/cs-protocol/src/main/java/com/cf/cs/protocol/pojo/dto/NewUserDictReq.java index 651f5f8..f197c28 100644 --- a/cs-protocol/src/main/java/com/cf/cs/protocol/pojo/dto/NewUserDictReq.java +++ b/cs-protocol/src/main/java/com/cf/cs/protocol/pojo/dto/NewUserDictReq.java @@ -23,14 +23,14 @@ import lombok.NoArgsConstructor; @Schema(name = "NewUserDictReq", description = "新增用户字典请求参数") public class NewUserDictReq { @Schema(description = "字典名,用于标识各个不同的字典") - @NotEmpty(message = "dictName 字典类别不能为空", groups = ValidGroups.DictReqValid.class) + @NotEmpty(message = "dictName {item.not_empty}", groups = ValidGroups.DictReqValid.class) private String dictName; @Schema(description = "字典别名,用于显示字典别名") - @NotNull(message = "dictAlias 字典类别不能为NULL", groups = ValidGroups.DictReqValid.class) + @NotNull(message = "dictAlias {item.not_null}", groups = ValidGroups.DictReqValid.class) private String dictAlias; @Schema(description = "字典备注信息") - @NotNull(message = "dictRemark 字典类别不能为NULL", groups = ValidGroups.DictReqValid.class) + @NotNull(message = "dictRemark {item.not_null}", groups = ValidGroups.DictReqValid.class) private String dictRemark; } diff --git a/cs-protocol/src/main/java/com/cf/cs/protocol/pojo/dto/OperationLogDetailsReq.java b/cs-protocol/src/main/java/com/cf/cs/protocol/pojo/dto/OperationLogDetailsReq.java index a52d2a3..e4cc924 100644 --- a/cs-protocol/src/main/java/com/cf/cs/protocol/pojo/dto/OperationLogDetailsReq.java +++ b/cs-protocol/src/main/java/com/cf/cs/protocol/pojo/dto/OperationLogDetailsReq.java @@ -25,7 +25,7 @@ import java.util.List; @Schema(name = "OperationLogDetailsReq", description = "操作日志详细请求参数") public class OperationLogDetailsReq { @Schema(description = "操作日志ID") - @NotEmpty(message = "操作日志ID不能为空", groups = ValidGroups.OperationLogReqValid.class) - @Size(message = "操作日志ID至少需要指定 1 个,最多只能指定 100 个", min = 1, max = 100, groups = ValidGroups.OperationLogReqValid.class) + @NotEmpty(message = "{array.not_empty}", groups = ValidGroups.OperationLogReqValid.class) + @Size(message = "{array.value_range}", min = 1, max = 100, groups = ValidGroups.OperationLogReqValid.class) private List operationIds; } diff --git a/cs-protocol/src/main/java/com/cf/cs/protocol/pojo/dto/RegisterResourceReq.java b/cs-protocol/src/main/java/com/cf/cs/protocol/pojo/dto/RegisterResourceReq.java index 78b73b1..85e75d8 100644 --- a/cs-protocol/src/main/java/com/cf/cs/protocol/pojo/dto/RegisterResourceReq.java +++ b/cs-protocol/src/main/java/com/cf/cs/protocol/pojo/dto/RegisterResourceReq.java @@ -30,7 +30,7 @@ public class RegisterResourceReq { * The Resources. */ @Schema(description = "需要添加的资源") - @NotNull(message = "resources 资源不能为NULL", groups = ValidGroups.ResourceReqValid.class) - @Size(min = 1, max = 100, message = "resources 单次添加资源条数为 [1, 100]", groups = ValidGroups.ResourceReqValid.class) + @NotNull(message = "resources {item.not_null}", groups = ValidGroups.ResourceReqValid.class) + @Size(min = 1, max = 100, message = "resources {array.value_range}", groups = ValidGroups.ResourceReqValid.class) List<@Valid ResourceInfo> resources; } diff --git a/cs-protocol/src/main/java/com/cf/cs/protocol/pojo/dto/RegisterUserReq.java b/cs-protocol/src/main/java/com/cf/cs/protocol/pojo/dto/RegisterUserReq.java index 83b25ef..fe7eb4a 100644 --- a/cs-protocol/src/main/java/com/cf/cs/protocol/pojo/dto/RegisterUserReq.java +++ b/cs-protocol/src/main/java/com/cf/cs/protocol/pojo/dto/RegisterUserReq.java @@ -24,26 +24,26 @@ import org.hibernate.validator.constraints.Length; @JsonInclude(JsonInclude.Include.NON_NULL) public class RegisterUserReq { @Schema(description = "用户名") - @NotBlank(message = "username 用户名不能为空", groups = ValidGroups.UserReqValid.class) + @NotBlank(message = "username {item.not_empty}", groups = ValidGroups.UserReqValid.class) @Pattern(regexp = ConstValue.UN_EXPECT_REGEX_CHARS, flags = Pattern.Flag.CASE_INSENSITIVE, - message = "username 用户名存在非法字符串", + message = "username {invalid.character}", groups = ValidGroups.UserReqValid.class) private String username; @Schema(description = "密码") - @NotBlank(message = "password 密码不能为空", groups = ValidGroups.UserReqValid.class) + @NotBlank(message = "password {item.not_empty}", groups = ValidGroups.UserReqValid.class) @Length(min = 32, max = 64, - message = "password 密码长度必须为SHA256编码后的长度", + message = "password {password.length}", groups = ValidGroups.UserReqValid.class) @Pattern(regexp = ConstValue.UN_EXPECT_REGEX_CHARS, flags = Pattern.Flag.CASE_INSENSITIVE, - message = "password 密码存在非法字符串", + message = "password {invalid.character}", groups = ValidGroups.UserReqValid.class) private String password; @Schema(description = "用户组ID") - @NotNull(message = "roleId 用户组ID不能为空", groups = ValidGroups.UserReqValid.class) - @Min(value = 2, message = "roleId 用户组ID最小取值为2", groups = ValidGroups.UserReqValid.class) + @NotNull(message = "roleId {item.not_null}", groups = ValidGroups.UserReqValid.class) + @Min(value = 2, message = "roleId {item.value_min}", groups = ValidGroups.UserReqValid.class) private Long roleId; } diff --git a/cs-protocol/src/main/java/com/cf/cs/protocol/pojo/dto/RemoveDictReq.java b/cs-protocol/src/main/java/com/cf/cs/protocol/pojo/dto/RemoveDictReq.java index 0bfbe0f..a549a7e 100644 --- a/cs-protocol/src/main/java/com/cf/cs/protocol/pojo/dto/RemoveDictReq.java +++ b/cs-protocol/src/main/java/com/cf/cs/protocol/pojo/dto/RemoveDictReq.java @@ -22,6 +22,6 @@ import lombok.NoArgsConstructor; @Schema(name = "RemoveDictReq", description = "删除用户字典请求参数") public class RemoveDictReq { @Schema(description = "字典名,用于标识各个不同的字典") - @NotNull(message = "dictName 字典名不能为NULL", groups = ValidGroups.DictReqValid.class) + @NotNull(message = "dictName {item.not_null}", groups = ValidGroups.DictReqValid.class) private String dictName; } diff --git a/cs-protocol/src/main/java/com/cf/cs/protocol/pojo/dto/UserIdReq.java b/cs-protocol/src/main/java/com/cf/cs/protocol/pojo/dto/UserIdReq.java index fa92177..977e7b7 100644 --- a/cs-protocol/src/main/java/com/cf/cs/protocol/pojo/dto/UserIdReq.java +++ b/cs-protocol/src/main/java/com/cf/cs/protocol/pojo/dto/UserIdReq.java @@ -20,7 +20,7 @@ import lombok.NoArgsConstructor; @AllArgsConstructor @JsonInclude(JsonInclude.Include.NON_NULL) public class UserIdReq { - @NotNull(message = "userId 用户ID不能为空", groups = ValidGroups.UserIdReqValid.class) - @UUID(message = "uid 必须为符号UUID规范(xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)的字符串", groups = ValidGroups.UserIdReqValid.class) + @NotNull(message = "userId {item.not_null}", groups = ValidGroups.UserIdReqValid.class) + @UUID(message = "uid {uuid.format}", groups = ValidGroups.UserIdReqValid.class) private String uid; } diff --git a/cs-protocol/src/main/java/com/cf/cs/protocol/pojo/po/DictDetails.java b/cs-protocol/src/main/java/com/cf/cs/protocol/pojo/po/DictDetails.java index 757493c..002cc70 100644 --- a/cs-protocol/src/main/java/com/cf/cs/protocol/pojo/po/DictDetails.java +++ b/cs-protocol/src/main/java/com/cf/cs/protocol/pojo/po/DictDetails.java @@ -31,9 +31,9 @@ public class DictDetails { private String upgradeBy; private Timestamp upgradeTime; - @NotBlank(message = "title 字典内容不能为空", groups = ValidGroups.DictReqContentValid.class) + @NotBlank(message = "title {item.not_empty}", groups = ValidGroups.DictReqContentValid.class) private String title; - @NotNull(message = "value 字典内容值不能为 NULL", groups = ValidGroups.DictReqContentValid.class) + @NotNull(message = "value {item.not_null}", groups = ValidGroups.DictReqContentValid.class) private Integer value; private Integer sorted; private String cssClass; diff --git a/cs-protocol/src/main/java/com/cf/cs/protocol/pojo/po/ResourceInfo.java b/cs-protocol/src/main/java/com/cf/cs/protocol/pojo/po/ResourceInfo.java index 24b3628..cce40cd 100644 --- a/cs-protocol/src/main/java/com/cf/cs/protocol/pojo/po/ResourceInfo.java +++ b/cs-protocol/src/main/java/com/cf/cs/protocol/pojo/po/ResourceInfo.java @@ -25,22 +25,22 @@ import java.util.List; @AllArgsConstructor public class ResourceInfo { @Schema(description = "资源路径") - @NotNull(message = "path 资源 URL 不能为 NULL", groups = ValidGroups.ResourceInfoValid.class) - @NotBlank(message = "path 资源 URL 不能为空字符串", groups = ValidGroups.ResourceInfoValid.class) + @NotNull(message = "path {item.not_null}", groups = ValidGroups.ResourceInfoValid.class) + @NotBlank(message = "path {item.not_empty}", groups = ValidGroups.ResourceInfoValid.class) private String path; @Schema(description = "资源名称") - @NotNull(message = "name 资源名称不能为 NULL", groups = ValidGroups.ResourceInfoValid.class) - @NotBlank(message = "name 资源名称不能为空字符串", groups = ValidGroups.ResourceInfoValid.class) + @NotNull(message = "name {item.not_null}", groups = ValidGroups.ResourceInfoValid.class) + @NotBlank(message = "name {item.not_empty}", groups = ValidGroups.ResourceInfoValid.class) private String name; @Schema(description = "资源类型名称") - @NotNull(message = "resTypeId 资源名称不能为 NULL", groups = ValidGroups.ResourceInfoValid.class) + @NotNull(message = "resTypeId {item.not_null}", groups = ValidGroups.ResourceInfoValid.class) private Integer resTypeId; @Schema(description = "HTTP 方法") - @NotNull(message = "HttpMethod 方法名称不能为 NULL", groups = ValidGroups.ResourceInfoValid.class) - @Size(min = 1, max = 8, message = "HttpMethod HTTP 方法名称不能为空", groups = ValidGroups.ResourceInfoValid.class) + @NotNull(message = "HttpMethod {item.not_null}", groups = ValidGroups.ResourceInfoValid.class) + @Size(min = 1, max = 8, message = "HttpMethod HTTP {array.value_range}", groups = ValidGroups.ResourceInfoValid.class) @ValidHttpMethod(groups = ValidGroups.ResourceInfoValid.class) private List httpMethod; } diff --git a/cs-protocol/src/main/java/com/cf/cs/protocol/validation/valids/impl/ValidHttpMethodImpl.java b/cs-protocol/src/main/java/com/cf/cs/protocol/validation/valids/impl/ValidHttpMethodImpl.java index 7f7dcba..dec1746 100644 --- a/cs-protocol/src/main/java/com/cf/cs/protocol/validation/valids/impl/ValidHttpMethodImpl.java +++ b/cs-protocol/src/main/java/com/cf/cs/protocol/validation/valids/impl/ValidHttpMethodImpl.java @@ -1,5 +1,7 @@ package com.cf.cs.protocol.validation.valids.impl; +import com.cf.cs.base.misc.ApiContextUtils; +import com.cf.cs.base.misc.MessageUtil; import com.cf.cs.protocol.validation.valids.ValidHttpMethod; import jakarta.validation.ConstraintValidator; import jakarta.validation.ConstraintValidatorContext; @@ -27,7 +29,8 @@ public class ValidHttpMethodImpl implements ConstraintValidator System.currentTimeMillis()) { - String errMsg = String.format("请求时间[%s] 大于当前服务器时间 [%s]", date, current); + String errMsg = MessageUtil.get("timeout.current.format", new String[] {date, current}, ApiContextUtils.getLanguare()); //禁用默认的message的值 constraintValidatorContext.disableDefaultConstraintViolation(); //重新添加错误提示语句 constraintValidatorContext.buildConstraintViolationWithTemplate(errMsg).addConstraintViolation(); return false; } else if (System.currentTimeMillis() - timeStamp > ProtocolConfigure.TIMEOUT_OF_SECONDS * ConstValue.MS_OF_SECONDS) { - String errMsg = String.format("请求时间[%s] 延时超过当前服务器时间[%s] 允许范围 %d(S)", date, current, - ProtocolConfigure.TIMEOUT_OF_SECONDS); + String errMsg = MessageUtil.get("timeout.current.format", + new String[] {date, current, String.valueOf(ProtocolConfigure.TIMEOUT_OF_SECONDS)}, + ApiContextUtils.getLanguare()); //禁用默认的message的值 constraintValidatorContext.disableDefaultConstraintViolation(); //重新添加错误提示语句