我在使用 aop 做日志切面,记录 controller 的入参和返回时,将参数使用 fastjson 格式化。代码如下:
Object[] args = joinPoint.getArgs();
// 报错出现在下面这行
String params = JSONObject.toJSONStringWithDateFormat(args, dateFormat, SerializerFeature.WriteMapNullValue);
结果出现如下报错:
@GetMapping("/read/{id}")
public String read(@PathVariable("id") Integer id,
HttpServletResponse response,
Model model){
return null;
}
Object[] args = joinPoint.getArgs();
if (Objects.nonNull(args)){
List<Object> argsList = Arrays.asList(args);
// 将 HttpServletResponse 和 HttpServletRequest 参数移除,不然会报异常
List<Object> collect = argsList.stream().filter(o -> !(o instanceof HttpServletResponse || o instanceof HttpServletRequest)).collect(Collectors.toList());
collect.toArray(args);
}
String params = JSONObject.toJSONStringWithDateFormat(args, dateFormat, SerializerFeature.WriteMapNullValue);