mirror of https://gitee.com/stylefeng/guns
更新7.0.1校验模块引用
parent
06f97ce41a
commit
7d1fc93029
7
pom.xml
7
pom.xml
|
@ -80,13 +80,6 @@
|
|||
<version>${roses.kernel.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!--参数校验模块-->
|
||||
<dependency>
|
||||
<groupId>cn.stylefeng.roses</groupId>
|
||||
<artifactId>validator-spring-boot-starter</artifactId>
|
||||
<version>${roses.kernel.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!--字典业务-->
|
||||
<dependency>
|
||||
<groupId>cn.stylefeng.roses</groupId>
|
||||
|
|
|
@ -0,0 +1,38 @@
|
|||
package cn.stylefeng.guns.config.validator;
|
||||
|
||||
import cn.stylefeng.roses.kernel.validator.api.context.RequestGroupContext;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.validation.Errors;
|
||||
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
|
||||
|
||||
/**
|
||||
* 用于真正校验参数之前缓存一下group的class类型
|
||||
* <p>
|
||||
* 因为ConstraintValidator的自定义校验中获取不到当前进行的group
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @date 2020/8/12 20:07
|
||||
*/
|
||||
@Slf4j
|
||||
public class GunsValidator extends LocalValidatorFactoryBean {
|
||||
|
||||
@Override
|
||||
public void validate(Object target, Errors errors, Object... validationHints) {
|
||||
|
||||
try {
|
||||
if (validationHints.length > 0) {
|
||||
|
||||
// 如果是class类型,利用ThreadLocal缓存一下class类型
|
||||
if (validationHints[0] instanceof Class) {
|
||||
|
||||
// 临时保存group的class值
|
||||
RequestGroupContext.set((Class<?>) validationHints[0]);
|
||||
}
|
||||
}
|
||||
super.validate(target, errors, validationHints);
|
||||
} finally {
|
||||
RequestGroupContext.clear();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,57 @@
|
|||
package cn.stylefeng.guns.config.validator;
|
||||
|
||||
import cn.stylefeng.roses.kernel.validator.api.context.RequestParamContext;
|
||||
import org.springframework.core.Conventions;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.http.converter.HttpMessageConverter;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||
import org.springframework.web.bind.WebDataBinder;
|
||||
import org.springframework.web.bind.support.WebDataBinderFactory;
|
||||
import org.springframework.web.context.request.NativeWebRequest;
|
||||
import org.springframework.web.method.support.ModelAndViewContainer;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 拓展原有RequestResponseBodyMethodProcessor,只为缓存临时参数
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @date 2020/8/21 20:51
|
||||
*/
|
||||
public class GunsValidatorRequestResponseBodyMethodProcessor extends RequestResponseBodyMethodProcessor {
|
||||
|
||||
public GunsValidatorRequestResponseBodyMethodProcessor(List<HttpMessageConverter<?>> converters) {
|
||||
super(converters);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object resolveArgument(MethodParameter parameter, @Nullable ModelAndViewContainer mavContainer, NativeWebRequest webRequest, @Nullable WebDataBinderFactory binderFactory) throws Exception {
|
||||
|
||||
parameter = parameter.nestedIfOptional();
|
||||
Object arg = readWithMessageConverters(webRequest, parameter, parameter.getNestedGenericParameterType());
|
||||
|
||||
// 临时缓存一下@RequestBody注解上的参数
|
||||
RequestParamContext.setObject(arg);
|
||||
|
||||
String name = Conventions.getVariableNameForParameter(parameter);
|
||||
|
||||
if (binderFactory != null) {
|
||||
WebDataBinder binder = binderFactory.createBinder(webRequest, arg, name);
|
||||
if (arg != null) {
|
||||
validateIfApplicable(binder, parameter);
|
||||
if (binder.getBindingResult().hasErrors() && isBindExceptionRequired(binder, parameter)) {
|
||||
throw new MethodArgumentNotValidException(parameter, binder.getBindingResult());
|
||||
}
|
||||
}
|
||||
if (mavContainer != null) {
|
||||
mavContainer.addAttribute(BindingResult.MODEL_KEY_PREFIX + name, binder.getBindingResult());
|
||||
}
|
||||
}
|
||||
|
||||
return adaptArgumentIfNecessary(arg, parameter);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,54 @@
|
|||
package cn.stylefeng.guns.config.validator;
|
||||
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
|
||||
import org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.Resource;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* 自定义的GunsRequestResponseBodyMethodProcessor,放在所有resolvers之前
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @date 2020/8/21 21:09
|
||||
*/
|
||||
@Configuration
|
||||
@AutoConfigureBefore(ValidationAutoConfiguration.class)
|
||||
public class MethodArgumentResolverAutoConfiguration {
|
||||
|
||||
@Resource
|
||||
private RequestMappingHandlerAdapter adapter;
|
||||
|
||||
/**
|
||||
* 自定义的spring参数校验器,重写主要为了保存一些在自定义validator中读不到的属性
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @date 2020/8/12 20:18
|
||||
*/
|
||||
@Bean
|
||||
public GunsValidator gunsValidator() {
|
||||
return new GunsValidator();
|
||||
}
|
||||
|
||||
/**
|
||||
* 自定义的GunsRequestResponseBodyMethodProcessor,放在所有resolvers之前
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @date 2020/12/16 18:34
|
||||
*/
|
||||
@PostConstruct
|
||||
public void injectSelfMethodArgumentResolver() {
|
||||
List<HandlerMethodArgumentResolver> argumentResolvers = new ArrayList<>();
|
||||
argumentResolvers.add(new GunsValidatorRequestResponseBodyMethodProcessor(adapter.getMessageConverters()));
|
||||
argumentResolvers.addAll(Objects.requireNonNull(adapter.getArgumentResolvers()));
|
||||
adapter.setArgumentResolvers(argumentResolvers);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue