1、创建注解
package com.huangniu.common.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.PARAMETER) // 参数
@Retention(RetentionPolicy.RUNTIME)
public @interface BaseEntityParam {
}
2、添加解析参数解析器
package com.huangniu.common.core.resolver;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeanInstantiationException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.TypeMismatchException;
import org.springframework.core.MethodParameter;
import org.springframework.http.HttpMethod;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.*;
import org.springframework.validation.annotation.ValidationAnnotationUtils;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.bind.support.WebRequestDataBinder;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.annotation.ModelFactory;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.method.support.ModelAndViewContainer;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;
import org.springframework.web.multipart.support.StandardServletPartUtils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Part;
import java.lang.annotation.Annotation;
import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.util.*;
public class BaseEntityModelAttributeMethodProcessor implements HandlerMethodArgumentResolver, HandlerMethodReturnValueHandler {
protected final Log logger = LogFactory.getLog(this.getClass());
private final boolean annotationNotRequired;
public BaseEntityModelAttributeMethodProcessor(boolean annotationNotRequired) {
this.annotationNotRequired = annotationNotRequired;
}
public boolean supportsParameter(MethodParameter parameter) {
return parameter.hasParameterAnnotation(ModelAttribute.class) || this.annotationNotRequired && !BeanUtils.isSimpleProperty(parameter.getParameterType());
}
@Nullable
public Object resolveArgument(MethodParameter parameter, @Nullable ModelAndViewContainer mavContainer, NativeWebRequest webRequest, @Nullable WebDataBinderFactory binderFactory) throws Exception {
Assert.state(mavContainer != null, "ModelAttributeMethodProcessor requires ModelAndViewContainer");
Assert.state(binderFactory != null, "ModelAttributeMethodProcessor requires WebDataBinderFactory");
String name = ModelFactory.getNameForParameter(parameter);
ModelAttribute ann = (ModelAttribute) parameter.getParameterAnnotation(ModelAttribute.class);
if (ann != null) {
mavContainer.setBinding(name, ann.binding());
}
Object attribute = null;
BindingResult bindingResult = null;
if (mavContainer.containsAttribute(name)) {
attribute = mavContainer.getModel().get(name);
} else {
try {
attribute = this.createAttribute(name, parameter, binderFactory, webRequest);
} catch (BindException var10) {
if (this.isBindExceptionRequired(parameter)) {
throw var10;
}
if (parameter.getParameterType() == Optional.class) {
attribute = Optional.empty();
} else {
attribute = var10.getTarget();
}
bindingResult = var10.getBindingResult();
}
}
if (bindingResult == null) {
WebDataBinder binder = binderFactory.createBinder(webRequest, attribute, name);
if (binder.getTarget() != null) {
if (!mavContainer.isBindingDisabled(name)) {
this.bindRequestParameters(binder, webRequest);
}
this.validateIfApplicable(binder, parameter);
if (binder.getBindingResult().hasErrors() && this.isBindExceptionRequired(binder, parameter)) {
throw new BindException(binder.getBindingResult());
}
}
if (!parameter.getParameterType().isInstance(attribute)) {
attribute = binder.convertIfNecessary(binder.getTarget(), parameter.getParameterType(), parameter);
}
bindingResult = binder.getBindingResult();
}
Map bindingResultModel = bindingResult.getModel();
mavContainer.removeAttributes(bindingResultModel);
mavContainer.addAllAttributes(bindingResultModel);
return attribute;
}
protected Object createAttribute(String attributeName, MethodParameter parameter, WebDataBinderFactory binderFactory, NativeWebRequest webRequest) throws Exception {
MethodParameter nestedParameter = parameter.nestedIfOptional();
Class> clazz = nestedParameter.getNestedParameterType();
Constructor> ctor = BeanUtils.getResolvableConstructor(clazz);
Object attribute = this.constructAttribute(ctor, attributeName, parameter, binderFactory, webRequest);
if (parameter != nestedParameter) {
attribute = Optional.of(attribute);
}
return attribute;
}
protected Object constructAttribute(Constructor> ctor, String attributeName, MethodParameter parameter, WebDataBinderFactory binderFactory, NativeWebRequest webRequest) throws Exception {
if (ctor.getParameterCount() == 0) {
return BeanUtils.instantiateClass(ctor, new Object[0]);
} else {
String[] paramNames = BeanUtils.getParameterNames(ctor);
Class>[] paramTypes = ctor.getParameterTypes();
Object[] args = new Object[paramTypes.length];
WebDataBinder binder = binderFactory.createBinder(webRequest, (Object) null, attributeName);
String fieldDefaultPrefix = binder.getFieldDefaultPrefix();
String fieldMarkerPrefix = binder.getFieldMarkerPrefix();
boolean bindingFailure = false;
Set failedParams = new HashSet(4);
Object value;
for (int i = 0; i < paramNames.length; ++i) {
String paramName = paramNames[i];
Class> paramType = paramTypes[i];
value = webRequest.getParameterValues(paramName);
if (ObjectUtils.isArray(value) && Array.getLength(value) == 1) {
value = Array.get(value, 0);
}
if (value == null) {
if (fieldDefaultPrefix != null) {
value = webRequest.getParameter(fieldDefaultPrefix + paramName);
}
if (value == null) {
if (fieldMarkerPrefix != null && webRequest.getParameter(fieldMarkerPrefix + paramName) != null) {
value = binder.getEmptyValue(paramType);
} else {
value = this.resolveConstructorArgument(paramName, paramType, webRequest);
}
}
}
try {
MethodParameter methodParam = new FieldAwareConstructorParameter(ctor, i, paramName);
if (value == null && methodParam.isOptional()) {
args[i] = methodParam.getParameterType() == Optional.class ? Optional.empty() : null;
} else {
args[i] = binder.convertIfNecessary(value, paramType, methodParam);
}
} catch (TypeMismatchException var20) {
var20.initPropertyName(paramName);
args[i] = null;
failedParams.add(paramName);
binder.getBindingResult().recordFieldValue(paramName, paramType, value);
binder.getBindingErrorProcessor().processPropertyAccessException(var20, binder.getBindingResult());
bindingFailure = true;
}
}
if (!bindingFailure) {
return BeanUtils.instantiateClass(ctor, args);
} else {
BindingResult result = binder.getBindingResult();
for (int i = 0; i < paramNames.length; ++i) {
String paramName = paramNames[i];
if (!failedParams.contains(paramName)) {
value = args[i];
result.recordFieldValue(paramName, paramTypes[i], value);
this.validateValueIfApplicable(binder, parameter, ctor.getDeclaringClass(), paramName, value);
}
}
if (!parameter.isOptional()) {
try {
final Object target = BeanUtils.instantiateClass(ctor, args);
throw new BindException(result) {
public Object getTarget() {
return target;
}
};
} catch (BeanInstantiationException var19) {
}
}
throw new BindException(result);
}
}
}
protected void bindRequestParameters(WebDataBinder binder, NativeWebRequest request) {
((WebRequestDataBinder) binder).bind(request);
}
@Nullable
public Object resolveConstructorArgument(String paramName, Class> paramType, NativeWebRequest request) throws Exception {
MultipartRequest multipartRequest = (MultipartRequest) request.getNativeRequest(MultipartRequest.class);
if (multipartRequest != null) {
List files = multipartRequest.getFiles(paramName);
if (!files.isEmpty()) {
return files.size() == 1 ? files.get(0) : files;
}
} else if (StringUtils.startsWithIgnoreCase(request.getHeader("Content-Type"), "multipart/form-data")) {
HttpServletRequest servletRequest = (HttpServletRequest) request.getNativeRequest(HttpServletRequest.class);
if (servletRequest != null && HttpMethod.POST.matches(servletRequest.getMethod())) {
List parts = StandardServletPartUtils.getParts(servletRequest, paramName);
if (!parts.isEmpty()) {
return parts.size() == 1 ? parts.get(0) : parts;
}
}
}
return null;
}
protected void validateIfApplicable(WebDataBinder binder, MethodParameter parameter) {
Annotation[] var3 = parameter.getParameterAnnotations();
int var4 = var3.length;
for (int var5 = 0; var5 < var4; ++var5) {
Annotation ann = var3[var5];
Object[] validationHints = ValidationAnnotationUtils.determineValidationHints(ann);
if (validationHints != null) {
binder.validate(validationHints);
break;
}
}
}
protected void validateValueIfApplicable(WebDataBinder binder, MethodParameter parameter, Class> targetType, String fieldName, @Nullable Object value) {
Annotation[] var6 = parameter.getParameterAnnotations();
int var7 = var6.length;
for (int var8 = 0; var8 < var7; ++var8) {
Annotation ann = var6[var8];
Object[] validationHints = ValidationAnnotationUtils.determineValidationHints(ann);
if (validationHints != null) {
Iterator var11 = binder.getValidators().iterator();
while (var11.hasNext()) {
Validator validator = (Validator) var11.next();
if (validator instanceof SmartValidator) {
try {
((SmartValidator) validator).validateValue(targetType, fieldName, value, binder.getBindingResult(), validationHints);
} catch (IllegalArgumentException var14) {
}
}
}
return;
}
}
}
protected boolean isBindExceptionRequired(WebDataBinder binder, MethodParameter parameter) {
return this.isBindExceptionRequired(parameter);
}
protected boolean isBindExceptionRequired(MethodParameter parameter) {
int i = parameter.getParameterIndex();
Class>[] paramTypes = parameter.getExecutable().getParameterTypes();
boolean hasBindingResult = paramTypes.length > i + 1 && Errors.class.isAssignableFrom(paramTypes[i + 1]);
return !hasBindingResult;
}
public boolean supportsReturnType(MethodParameter returnType) {
return returnType.hasMethodAnnotation(ModelAttribute.class) || this.annotationNotRequired && !BeanUtils.isSimpleProperty(returnType.getParameterType());
}
public void handleReturnValue(@Nullable Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {
if (returnValue != null) {
String name = ModelFactory.getNameForReturnValue(returnValue, returnType);
mavContainer.addAttribute(name, returnValue);
}
}
private static class FieldAwareConstructorParameter extends MethodParameter {
private final String parameterName;
@Nullable
private volatile Annotation[] combinedAnnotations;
public FieldAwareConstructorParameter(Constructor> constructor, int parameterIndex, String parameterName) {
super(constructor, parameterIndex);
this.parameterName = parameterName;
}
public Annotation[] getParameterAnnotations() {
Annotation[] anns = this.combinedAnnotations;
if (anns == null) {
anns = super.getParameterAnnotations();
try {
Field field = this.getDeclaringClass().getDeclaredField(this.parameterName);
Annotation[] fieldAnns = field.getAnnotations();
if (fieldAnns.length > 0) {
List merged = new ArrayList(anns.length + fieldAnns.length);
merged.addAll(Arrays.asList(anns));
Annotation[] var5 = fieldAnns;
int var6 = fieldAnns.length;
int var7 = 0;
while (true) {
if (var7 >= var6) {
anns = (Annotation[]) merged.toArray(new Annotation[0]);
break;
}
Annotation fieldAnn = var5[var7];
boolean existingType = false;
Annotation[] var10 = anns;
int var11 = anns.length;
for (int var12 = 0; var12 < var11; ++var12) {
Annotation ann = var10[var12];
if (ann.annotationType() == fieldAnn.annotationType()) {
existingType = true;
break;
}
}
if (!existingType) {
merged.add(fieldAnn);
}
++var7;
}
}
} catch (SecurityException | NoSuchFieldException var14) {
}
this.combinedAnnotations = anns;
}
return anns;
}
public String getParameterName() {
return this.parameterName;
}
}
}
package com.huangniu.common.core.resolver;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.huangniu.common.annotation.BaseEntityParam;
import com.huangniu.common.core.domain.BaseEntity;
import org.springframework.core.MethodParameter;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.validation.DataBinder;
import org.springframework.web.bind.ServletRequestDataBinder;
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.HandlerMapping;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.Map;
public class BaseEntityServletModelAttributeMethodProcessor extends BaseEntityModelAttributeMethodProcessor {
final String paramsName = "params";
final String paramsStrName = "paramsStr";
public BaseEntityServletModelAttributeMethodProcessor(boolean annotationNotRequired) {
super(annotationNotRequired);
}
@Override
public boolean supportsParameter(MethodParameter parameter) {
return parameter.hasParameterAnnotation(BaseEntityParam.class);
}
protected final Object createAttribute(String attributeName, MethodParameter parameter, WebDataBinderFactory binderFactory, NativeWebRequest request) throws Exception {
String value = this.getRequestValueForAttribute(attributeName, request);
if (value != null) {
Object attribute = this.createAttributeFromRequestValue(value, attributeName, parameter, binderFactory, request);
if (attribute != null) {
return attribute;
}
}
return super.createAttribute(attributeName, parameter, binderFactory, request);
}
@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
Object object = super.resolveArgument(parameter, mavContainer, webRequest, binderFactory);
String params = ((HttpServletRequest) webRequest.getNativeRequest(HttpServletRequest.class)).getParameter(paramsStrName);
if (StrUtil.isNotBlank(params) && object != null) {
JSONObject jsonObject = JSONObject.parseObject(params);
((BaseEntity) object).setParams(jsonObject);
}
return object;
}
@Nullable
protected String getRequestValueForAttribute(String attributeName, NativeWebRequest request) {
Map variables = this.getUriTemplateVariables(request);
String variableValue = (String) variables.get(attributeName);
if (StringUtils.hasText(variableValue)) {
return variableValue;
} else {
String parameterValue = request.getParameter(attributeName);
return StringUtils.hasText(parameterValue) ? parameterValue : null;
}
}
protected final Map getUriTemplateVariables(NativeWebRequest request) {
Map variables = (Map) request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, 0);
return variables != null ? variables : Collections.emptyMap();
}
@Nullable
protected Object createAttributeFromRequestValue(String sourceValue, String attributeName, MethodParameter parameter, WebDataBinderFactory binderFactory, NativeWebRequest request) throws Exception {
DataBinder binder = binderFactory.createBinder(request, (Object) null, attributeName);
ConversionService conversionService = binder.getConversionService();
if (conversionService != null) {
TypeDescriptor source = TypeDescriptor.valueOf(String.class);
TypeDescriptor target = new TypeDescriptor(parameter);
if (conversionService.canConvert(source, target)) {
return binder.convertIfNecessary(sourceValue, parameter.getParameterType(), parameter);
}
}
return null;
}
protected void bindRequestParameters(WebDataBinder binder, NativeWebRequest request) {
ServletRequest servletRequest = (ServletRequest) request.getNativeRequest(ServletRequest.class);
Assert.state(servletRequest != null, "No ServletRequest");
ServletRequestDataBinder servletBinder = (ServletRequestDataBinder) binder;
servletBinder.bind(servletRequest);
}
@Nullable
public Object resolveConstructorArgument(String paramName, Class> paramType, NativeWebRequest request) throws Exception {
Object value = super.resolveConstructorArgument(paramName, paramType, request);
if (value != null) {
return value;
} else {
ServletRequest servletRequest = (ServletRequest) request.getNativeRequest(ServletRequest.class);
if (servletRequest != null) {
String attr = HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE;
Map uriVars = (Map) servletRequest.getAttribute(attr);
return uriVars.get(paramName);
} else {
return null;
}
}
}
}
3、在ResourcesConfig中添加参数解析器
/**
* 添加自定义参数绑定的
*
* @param resolvers
*/
@Override
public void addArgumentResolvers(List resolvers) {
resolvers.add(new BaseEntityServletModelAttributeMethodProcessor(true));
}
4、在Controller中添加注解
@GetMapping("/list")
public TableDataInfo list(@BaseEntityParam SysVideoDevice sysVideoDevice) {
}
5、就可以对参数或者params
6、前端src-utils-request中get参数处理部分对参数进行修改
// get请求映射params参数
if (config.method === 'get' && config.params) {
//新增加-开始
if (config.params.params != null && config.params.params != undefined) {
config.params.paramsStr = JSON.stringify(config.params.params)
}
//新增加-结束
let url = config.url + '?' + tansParams(config.params)
url = url.slice(0, -1)
config.params = {}
config.url = url
}
7、同样在下载请求的时候添加对应参数处理
// 通用下载方法
export function download(url, params, filename) {
downloadLoadingInstance = Loading.service({
text: '正在下载数据,请稍候',
spinner: 'el-icon-loading',
background: 'rgba(0, 0, 0, 0.7)'
})
//新添加-开始
if (params.params != null && params.params != undefined) {
params.paramsStr = JSON.stringify(params.params)
}
//新添加-结束