1. 程式人生 > >使用攔截器對前端傳入的字符串進行trim操作

使用攔截器對前端傳入的字符串進行trim操作

request != equal method substr serial style dsi type

 1   @Before("apiItf()")
 2     public void before(JoinPoint joinPoint) throws Exception {
 3         Object[] args = joinPoint.getArgs();
 4         if (args != null) {
 5             Method proxyMethod = ((MethodSignature) joinPoint.getSignature()).getMethod();
 6             ApiOperation apiOperation = proxyMethod.getAnnotation(ApiOperation.class
); 7 log.info("[" + apiOperation.value() + "] Request {}", JacksonUtils.jsonObjectSerializer(joinPoint.getArgs())); 8 9 Object[] object = joinPoint.getArgs(); 10 Object request = object[0]; 11 Field[] fields = request.getClass().getDeclaredFields();
12 for (Field field : fields) { 13 String name = field.getName(); 14 name = name.substring(0, 1).toUpperCase() + name.substring(1); 15 if (field.getGenericType().getTypeName().equals("java.lang.String")) { 16 Method getField = request.getClass().getMethod("get" + name);
17 Method setFiled = request.getClass().getMethod("set" + name, new Class[]{String.class}); 18 String value = (String) getField.invoke(request); 19 if (!Strings.isNullOrEmpty(value)) { 20 setFiled.invoke(request, new Object[]{value.trim()}); 21 } 22 } 23 } 24 } 25 }

使用攔截器對前端傳入的字符串進行trim操作