mirror of
				https://github.com/continew-org/continew-starter.git
				synced 2025-10-31 10:57:15 +08:00 
			
		
		
		
	feat(core): 新增 JSR 303 校验器自动配置(从 web 模块迁移)
1.从 web 模块移动 JSR 303 校验器自动配置到 core 模块 2.从 web 模块移动 MessageSourceUtils 到 core 模块
This commit is contained in:
		| @@ -0,0 +1,67 @@ | ||||
| /* | ||||
|  * 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.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 失败立即返回模式配置 | ||||
|      * | ||||
|      * <p> | ||||
|      * 默认情况下会校验完所有字段,然后才抛出异常。 | ||||
|      * </p> | ||||
|      */ | ||||
|     @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."); | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,84 @@ | ||||
| /* | ||||
|  * 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.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; | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -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 | ||||
| @@ -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; | ||||
|  | ||||
| /** | ||||
|  * 全局异常处理器 | ||||
|   | ||||
| @@ -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 失败立即返回模式配置 | ||||
|      * | ||||
|      * <p> | ||||
|      * 默认情况下会校验完所有字段,然后才抛出异常。 | ||||
|      * </p> | ||||
|      */ | ||||
|     @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."); | ||||
|   | ||||
| @@ -1,53 +0,0 @@ | ||||
| /* | ||||
|  * 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.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; | ||||
|         } | ||||
|     } | ||||
|  | ||||
| } | ||||
		Reference in New Issue
	
	Block a user