解码对象属性

This commit is contained in:
bwcx_jzy 2021-10-25 18:38:03 +08:00
parent 8b3b7da520
commit 147f95776d

View File

@ -1,12 +1,16 @@
package io.jpom.common;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.URLUtil;
import cn.jiangzeyin.common.interceptor.DefaultHandlerMethodArgumentResolver;
import io.jpom.model.BaseJsonModel;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.ModelAndViewContainer;
import java.lang.reflect.Field;
/**
* 解析 参数 url 编码
*
@ -21,6 +25,17 @@ public class UrlDecodeHandlerMethodArgumentResolver extends DefaultHandlerMethod
if (argument instanceof String) {
// 解码
return URLUtil.decode(argument.toString());
} else if (argument instanceof BaseJsonModel) {
// 解码对象属性
Field[] fields = ReflectUtil.getFields(argument.getClass());
for (Field field : fields) {
Class<?> type = field.getType();
if (type == String.class) {
String fieldValue = (String) ReflectUtil.getFieldValue(argument, field);
fieldValue = URLUtil.decode(fieldValue);
ReflectUtil.setFieldValue(argument, field, fieldValue);
}
}
}
return argument;
}