toJSON抛出异常It is illegal to call this method if the current request is not in asynchronous mode

toJSON抛出异常It is illegal to call this method if the current request is not in asynchronous mode
橘生淮南问题描述
在使用AOP实现日志记录功能时,用到了JSON.toJSON()将joinPoint.getArgs()数据转换为Json串,运行时抛出异常Caused by: java.lang.IllegalStateException: It is illegal to call this method if the current request is not in asynchronous mode (i.e. isAsyncStarted() returns false)
原因分析及解决方法
原因分析
抛出该异常的主要原因是JSON.toJSON()转换数据时要求入参必须是能够进行序列化的数据,如果入参不能序列化,则会抛出上述异常。
解决方法
将不能进行序列化的入参对象过滤掉,只要留下我们所需数据即可。对于这里的joinPoint,我们只需要去除request和response对象即可
判断request和response的代码:
1 | //object为遍历出来的参数,逐个判断是否为request或response |
完整代码:
1 | //对入参进行遍历 |










