diff --git a/continew-starter-core/src/main/java/top/continew/starter/core/autoconfigure/ValidatorAutoConfiguration.java b/continew-starter-core/src/main/java/top/continew/starter/core/autoconfigure/ValidatorAutoConfiguration.java new file mode 100644 index 00000000..5d231dd4 --- /dev/null +++ b/continew-starter-core/src/main/java/top/continew/starter/core/autoconfigure/ValidatorAutoConfiguration.java @@ -0,0 +1,67 @@ +/* + * 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.autoconfigure; + +import jakarta.annotation.PostConstruct; +import jakarta.validation.Validator; +import org.hibernate.validator.HibernateValidator; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.boot.autoconfigure.AutoConfiguration; +import org.springframework.context.MessageSource; +import org.springframework.context.annotation.Bean; +import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; + +import java.util.Properties; + +/** + * JSR 303 校验器自动配置 + * + * @author Charles7c + * @since 2.3.0 + */ +@AutoConfiguration +public class ValidatorAutoConfiguration { + + private static final Logger log = LoggerFactory.getLogger(ValidatorAutoConfiguration.class); + + /** + * Validator 失败立即返回模式配置 + * + *

+ * 默认情况下会校验完所有字段,然后才抛出异常。 + *

+ */ + @Bean + public Validator validator(MessageSource messageSource) { + try (LocalValidatorFactoryBean factoryBean = new LocalValidatorFactoryBean()) { + // 国际化 + factoryBean.setValidationMessageSource(messageSource); + factoryBean.setProviderClass(HibernateValidator.class); + Properties properties = new Properties(); + properties.setProperty("hibernate.validator.fail_fast", "true"); + factoryBean.setValidationProperties(properties); + factoryBean.afterPropertiesSet(); + return factoryBean.getValidator(); + } + } + + @PostConstruct + public void postConstruct() { + log.debug("[ContiNew Starter] - Auto Configuration 'Validator' completed initialization."); + } +} diff --git a/continew-starter-core/src/main/java/top/continew/starter/core/util/MessageSourceUtils.java b/continew-starter-core/src/main/java/top/continew/starter/core/util/MessageSourceUtils.java new file mode 100644 index 00000000..bec7d5b6 --- /dev/null +++ b/continew-starter-core/src/main/java/top/continew/starter/core/util/MessageSourceUtils.java @@ -0,0 +1,84 @@ +/* + * 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.util; + +import cn.hutool.extra.spring.SpringUtil; +import org.springframework.context.MessageSource; +import org.springframework.context.i18n.LocaleContextHolder; + +/** + * 国际化工具类 + * + * @author Jasmine + * @since 2.2.0 + */ +public class MessageSourceUtils { + + private static final MessageSource MESSAGE_SOURCE = SpringUtil.getBean(MessageSource.class); + private static final Object[] EMPTY_ARGS = {}; + + private MessageSourceUtils() { + } + + /** + * 根据消息编码获取 + * + * @param code 消息编码 + * @return 国际化后的消息 + */ + public static String getMessage(String code) { + return getMessage(code, EMPTY_ARGS); + } + + /** + * 根据消息编码获取 + * + * @param code 消息编码 + * @param args 参数 + * @return 国际化后的消息 + */ + public static String getMessage(String code, Object... args) { + return getMessage(code, code, args); + } + + /** + * 根据消息编码获取 + * + * @param code 消息编码 + * @param defaultMessage 默认消息 + * @return 国际化后的消息 + */ + public static String getMessage(String code, String defaultMessage) { + return getMessage(code, defaultMessage, EMPTY_ARGS); + } + + /** + * 根据消息编码获取 + * + * @param code 消息编码 + * @param defaultMessage 默认消息 + * @param args 参数 + * @return 国际化后的消息 + */ + public static String getMessage(String code, String defaultMessage, Object... args) { + try { + return MESSAGE_SOURCE.getMessage(code, args, LocaleContextHolder.getLocale()); + } catch (Exception e) { + return defaultMessage; + } + } +} diff --git a/continew-starter-core/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/continew-starter-core/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports index 73f7d823..f2d8c252 100644 --- a/continew-starter-core/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports +++ b/continew-starter-core/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -1,3 +1,4 @@ top.continew.starter.core.autoconfigure.project.ProjectAutoConfiguration +top.continew.starter.core.autoconfigure.ValidatorAutoConfiguration top.continew.starter.core.autoconfigure.threadpool.ThreadPoolAutoConfiguration top.continew.starter.core.autoconfigure.threadpool.AsyncAutoConfiguration \ No newline at end of file diff --git a/continew-starter-web/src/main/java/top/continew/starter/web/autoconfigure/exception/GlobalExceptionHandler.java b/continew-starter-web/src/main/java/top/continew/starter/web/autoconfigure/exception/GlobalExceptionHandler.java index 1fb3eb2c..ef1b0596 100644 --- a/continew-starter-web/src/main/java/top/continew/starter/web/autoconfigure/exception/GlobalExceptionHandler.java +++ b/continew-starter-web/src/main/java/top/continew/starter/web/autoconfigure/exception/GlobalExceptionHandler.java @@ -42,7 +42,7 @@ import top.continew.starter.core.exception.GlobalException; import top.continew.starter.core.exception.ResultInfoInterface; import top.continew.starter.web.autoconfigure.i18n.I18nProperties; import top.continew.starter.web.model.R; -import top.continew.starter.web.util.MessageSourceUtils; +import top.continew.starter.core.util.MessageSourceUtils; /** * 全局异常处理器 diff --git a/continew-starter-web/src/main/java/top/continew/starter/web/autoconfigure/exception/GlobalExceptionHandlerAutoConfiguration.java b/continew-starter-web/src/main/java/top/continew/starter/web/autoconfigure/exception/GlobalExceptionHandlerAutoConfiguration.java index 7b598cb3..8b08a109 100644 --- a/continew-starter-web/src/main/java/top/continew/starter/web/autoconfigure/exception/GlobalExceptionHandlerAutoConfiguration.java +++ b/continew-starter-web/src/main/java/top/continew/starter/web/autoconfigure/exception/GlobalExceptionHandlerAutoConfiguration.java @@ -17,20 +17,13 @@ package top.continew.starter.web.autoconfigure.exception; import jakarta.annotation.PostConstruct; -import jakarta.validation.Validation; -import jakarta.validation.Validator; -import jakarta.validation.ValidatorFactory; -import org.hibernate.validator.HibernateValidator; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.config.AutowireCapableBeanFactory; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController; import org.springframework.boot.context.properties.EnableConfigurationProperties; -import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; -import org.springframework.validation.beanvalidation.SpringConstraintValidatorFactory; import top.continew.starter.web.autoconfigure.i18n.I18nProperties; /** @@ -47,25 +40,6 @@ public class GlobalExceptionHandlerAutoConfiguration { private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandlerAutoConfiguration.class); - /** - * Validator 失败立即返回模式配置 - * - *

- * 默认情况下会校验完所有字段,然后才抛出异常。 - *

- */ - @Bean - public Validator validator(AutowireCapableBeanFactory beanFactory) { - ValidatorFactory validatorFactory = Validation.byProvider(HibernateValidator.class) - .configure() - .failFast(true) - .constraintValidatorFactory(new SpringConstraintValidatorFactory(beanFactory)) - .buildValidatorFactory(); - try (validatorFactory) { - return validatorFactory.getValidator(); - } - } - @PostConstruct public void postConstruct() { log.debug("[ContiNew Starter] - Auto Configuration 'Web-Global Exception Handler' completed initialization."); diff --git a/continew-starter-web/src/main/java/top/continew/starter/web/util/MessageSourceUtils.java b/continew-starter-web/src/main/java/top/continew/starter/web/util/MessageSourceUtils.java deleted file mode 100644 index 66ddb4a7..00000000 --- a/continew-starter-web/src/main/java/top/continew/starter/web/util/MessageSourceUtils.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * 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.web.util; - -import cn.hutool.extra.spring.SpringUtil; -import org.springframework.context.MessageSource; -import org.springframework.context.i18n.LocaleContextHolder; - -/** - * @author Jasmine - * @since 2.2.0 - */ -public class MessageSourceUtils { - - private static final MessageSource messageSource = SpringUtil.getBean(MessageSource.class); - - private static final Object[] emptyArray = new Object[] {}; - - public static String getMessage(String key) { - return getMessage(key, emptyArray); - } - - public static String getMessage(String key, String defaultMessage) { - return getMessage(key, defaultMessage, emptyArray); - } - - public static String getMessage(String msgKey, Object... args) { - return getMessage(msgKey, msgKey, args); - } - - public static String getMessage(String msgKey, String defaultMessage, Object... args) { - try { - return messageSource.getMessage(msgKey, args, LocaleContextHolder.getLocale()); - } catch (Exception e) { - return defaultMessage; - } - } - -}