import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* <p>对请求参数所有的字符串参数剔除空格</p>
* <p>将传递的空字符串参数改成null</p>
* <p>注意:请求参数模型需要提供构造方法</p>
* <p>方便fastJOSN读写</p>
* <p>不需要修改的参数将参数名放到excludes数组中</p>
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface Trim {
/**
* 不需要剔除空格的参数名集合
*/
String[] excludes() default {};
}
package com.paic.phucp.console.aop;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.paic.phucp.console.annotations.Trim;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
/**
* 切面:对使用@Trim自定义注解的接口剔除其中的参数值的前后空格
*/
@Slf4j
@Aspect
@Component
public class TrimAop {
@Pointcut("@annotation(com.paic.phucp.console.annotations.Trim)")
public void trim() {
// this is aop pointcut
}
@Around("trim()")
public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws
Throwable {
MethodSignature signature = (MethodSignature)
proceedingJoinPoint.getSignature();
Trim trim = signature.getMethod().getAnnotation(Trim.class);
if (null == trim) {
return proceedingJoinPoint.proceed();
}
Object[] args = proceedingJoinPoint.getArgs();
if (null == args || args.length == 0) {
return proceedingJoinPoint.proceed(args);
}
List<String> excludeParams = Arrays.asList(trim.excludes());
for (int i = 0; i < args.length; i++) {
if (args[i] instanceof ServletRequest || args[i] instanceof
ServletResponse) {
continue;
}
if (canBeParsed(JSON.toJSONString(args[i]))) {
Class<?> type = args[i].getClass();
Object result =
jsonLoop(JSON.parseObject(JSON.toJSONString(args[i])), excludeParams);
args[i] = JSON.parseObject(JSON.toJSONString(result), type);
}
}
return proceedingJoinPoint.proceed(args);
}
/**
* JSON 循环解析
*/
private Object jsonLoop(Object object, List<String> excludeParams) {
if (null == object) {
return null;
}
if (object instanceof JSONObject) {
JSONObject jsonObject = (JSONObject) object;
for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
parse(excludeParams, jsonObject, entry.getKey(),
entry.getValue());
}
}
if (object instanceof JSONArray) {
JSONArray jsonArray = (JSONArray) object;
for (Object element : jsonArray) {
jsonLoop(element, excludeParams);
}
}
return object;
}
/**
* json解析
*/
private void parse (List<String> excludeParams, JSONObject jsonObject,
String key, Object value) {
if (excludeParams.contains(key) || null == value){
return;
}
if (value instanceof String) {
if (StringUtils.isBlank(value.toString())){
jsonObject.put(key, null);
} else {
jsonObject.put(key, value.toString().trim());
}
} else if (value instanceof JSONArray || value instanceof JSONObject){
jsonLoop(value, excludeParams);
} else {
log.debug("nothing to do");
}
}
/**
* 是否能够被fastJSON 解析
* @param str 被解析的字符串
*/
private boolean canBeParsed(String str) {
try {
JSON.parseObject(str);
} catch (Exception e) {
return false;
}
return true;
}
}