mirror of
https://github.com/continew-org/continew-starter.git
synced 2025-09-11 16:57:14 +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
|
Reference in New Issue
Block a user