feat(core): 新增 JSR 303 校验器自动配置(从 web 模块迁移)

1.从 web 模块移动 JSR 303 校验器自动配置到 core 模块
2.从 web 模块移动 MessageSourceUtils 到 core 模块
This commit is contained in:
2024-07-03 23:22:07 +08:00
parent d31d8d209a
commit 6809600858
6 changed files with 153 additions and 80 deletions

View File

@@ -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;
/**
* 全局异常处理器

View File

@@ -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.");

View File

@@ -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;
}
}
}