mirror of https://gitee.com/stylefeng/roses
【7.0.2】换一种方法实现参数缓存
parent
625d8a8177
commit
1520cc1278
|
@ -0,0 +1,49 @@
|
||||||
|
package cn.stylefeng.roses.kernel.validator.starter;
|
||||||
|
|
||||||
|
import cn.stylefeng.roses.kernel.validator.api.context.RequestParamContext;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.core.MethodParameter;
|
||||||
|
import org.springframework.http.HttpInputMessage;
|
||||||
|
import org.springframework.http.converter.HttpMessageConverter;
|
||||||
|
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||||
|
import org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdvice;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.lang.reflect.Type;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 请求参数增强,缓存@RequestBody的请求参数
|
||||||
|
*
|
||||||
|
* @author fengshuonan
|
||||||
|
* @date 2021/3/18 18:42
|
||||||
|
*/
|
||||||
|
@ControllerAdvice
|
||||||
|
@Slf4j
|
||||||
|
@SuppressWarnings("all")
|
||||||
|
public class CacheParamRequestBodyAdvice implements RequestBodyAdvice {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean supports(MethodParameter methodParameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) throws IOException {
|
||||||
|
return inputMessage;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object afterBodyRead(Object body, HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
|
||||||
|
|
||||||
|
// 临时缓存一下@RequestBody注解上的参数
|
||||||
|
RequestParamContext.setObject(body);
|
||||||
|
|
||||||
|
return body;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object handleEmptyBody(Object body, HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
|
||||||
|
return body;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
package cn.stylefeng.roses.kernel.validator.starter.mvc;
|
package cn.stylefeng.roses.kernel.validator.starter;
|
||||||
|
|
||||||
import cn.stylefeng.roses.kernel.validator.api.context.RequestGroupContext;
|
import cn.stylefeng.roses.kernel.validator.api.context.RequestGroupContext;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
|
@ -0,0 +1,29 @@
|
||||||
|
package cn.stylefeng.roses.kernel.validator.starter;
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Guns的校验器的自动配置
|
||||||
|
*
|
||||||
|
* @author fengshuonan
|
||||||
|
* @date 2021/3/18 16:03
|
||||||
|
*/
|
||||||
|
@Configuration
|
||||||
|
@AutoConfigureBefore(ValidationAutoConfiguration.class)
|
||||||
|
public class GunsValidatorAutoConfiguration {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 自定义的spring参数校验器,重写主要为了保存一些在自定义validator中读不到的属性
|
||||||
|
*
|
||||||
|
* @author fengshuonan
|
||||||
|
* @date 2020/8/12 20:18
|
||||||
|
*/
|
||||||
|
@Bean
|
||||||
|
public GunsValidator gunsValidator() {
|
||||||
|
return new GunsValidator();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,56 +0,0 @@
|
||||||
package cn.stylefeng.roses.kernel.validator.starter;
|
|
||||||
|
|
||||||
import cn.stylefeng.roses.kernel.validator.starter.mvc.GunsValidator;
|
|
||||||
import cn.stylefeng.roses.kernel.validator.starter.mvc.GunsValidatorRequestResponseBodyMethodProcessor;
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,57 +0,0 @@
|
||||||
package cn.stylefeng.roses.kernel.validator.starter.mvc;
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,4 +0,0 @@
|
||||||
/**
|
|
||||||
* 这个包内容是对spring mvc原有的一些机制的拓展,为了方便校验器对 RequestGroupContext 之类的上下文添值
|
|
||||||
*/
|
|
||||||
package cn.stylefeng.roses.kernel.validator.starter.mvc;
|
|
|
@ -1,2 +1,2 @@
|
||||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||||
cn.stylefeng.roses.kernel.validator.starter.MethodArgumentResolverAutoConfiguration
|
cn.stylefeng.roses.kernel.validator.starter.GunsValidatorAutoConfiguration
|
||||||
|
|
Loading…
Reference in New Issue