The SSM framework implements Requet parameter logging based on Spring-AOP
1. Parameter type
- From form submission, available from the request’s getParameterMap()
- Raw commits (object type commits) from request getReade() or getInputStream()
2. Add maven dependencies
net.sf.json-lib
json-lib
2.4
jdk15
com.google.code.gson
gson
${gson.version}
3. The configuration of aop RequsetParamsJouranAspect cut class
/ * *
- Request parameter logs are recorded
- @ the Author: CatalpaFlat
- @Descrition:
- @Date: Create in 22:24 2017/11/12
- @ Modified BY:
/
@Aspect
@Component
public class RequsetParamsJouranAspect {
/ *- The service layer section
/
private final String POINT_CUT = “execution( com.chen.logic.controller..(..) ) “;
/ * - Log output * / private static final Logger Logger = Logger. GetLogger (RequsetParamsJouranAspect. Class. GetName ());
- The service layer section
@Pointcut(POINT_CUT)
private void pointcut() {
}
@Before(value = "pointcut()")
public void before(JoinPoint joinPoint) {
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
ServletRequestAttributes sra = (ServletRequestAttributes) requestAttributes;
HttpServletRequest request = sra.getRequest();
Map<String, Object> returnMap = new HashMap<>();
Map<String, String[]> parameterMap = request.getParameterMap();
Iterator entries = parameterMap.entrySet().iterator();
Map.Entry entry;
String name;
String value = "";
while (entries.hasNext()) {
entry = (Map.Entry) entries.next();
name = (String) entry.getKey();
Object valueObj = entry.getValue();
if (null == valueObj) {
value = "";
} else if (valueObj instanceof String[]) {
String[] values = (String[]) valueObj;
for (String v : values) {
value = v + ",";
}
value = value.substring(0, value.length() - 1);
} else {
value = valueObj.toString();
}
returnMap.put(name, value);
}
JSONObject paramsJson = JSONObject.fromObject(new Gson().toJson(returnMap));
logger.info("paramsJson:"+paramsJson.toString());
Object[] args = joinPoint.getArgs();
JSONArray bodyArray = new JSONArray();
for (Object obj:args){
if(! (obj instanceof HttpServletRequest)&&! isBasicDataType(obj)) bodyArray.add(JSONObject.fromObject(obj)); } logger.info("bodyArray:"+bodyArray.toString()); } /** * is a basic data type or a custom object * @param obj parameter object * @returnIs it a basic data type or a custom object (true/false) */ private Boolean isBasicDataType(Object obj){if (obj instanceof String)
return true;
if (obj instanceof Boolean)
return true; Class<? > aClass = obj.getClass();returnaClass.isPrimitive(); }}Copy the code
Note:
-
When the @requestBody object is annotated, the parameter is submitted in raw format and received by the object, so the parameter type can be excluded by determining whether it is a basic data type
4. Test
Based on the: blog.csdn.net/dushiwodecu…
4.1. The Controller
@PostMapping(value = “getraw”)
public String getraw(@RequestBody Test test){return "OK";Copy the code
}
@PostMapping(value = “getParams”)
public String getParams(@RequestParam String name){return name;Copy the code
}
4.2. Test calls