SpringBoot 上传文件记录日志报错:It is illegal to call this method if the current request is not in asynchronou

心已赠人 2022-02-05 09:15 460阅读 0赞

springboot 上传文件记录日志是报错It is illegal to call this method if the current request is not in asynchronous mode (i.e. isAsyncStarted() returns false)

原因,因为Object[] args = joinPoint.getArgs();会得到多余的ServletRequest,MultipartFile等,而这些入参并不能进行序列化。这里我们将args进行处理:

  1. // 请求的参数
  2. Object[] args = joinPoint.getArgs();
  3. // 将参数所在的数组转换成json
  4. Object[] arguments = new Object[args.length];
  5. for (int i = 0; i < args.length; i++) {
  6. if (args[i] instanceof ServletRequest || args[i] instanceof ServletResponse
  7. || args[i] instanceof MultipartFile) {
  8. /* ServletRequest不能序列化,从入参里排除,否则报异常:java.lang.IllegalStateException: It is
  9. illegal to call this method if the current request is not in asynchronous
  10. mode (i.e. isAsyncStarted() returns false)
  11. ServletResponse不能序列化 从入参里排除,否则报异常:java.lang.IllegalStateException:
  12. getOutputStream() has already been called for this response*/
  13. continue;
  14. }
  15. arguments[i] = args[i];
  16. }
  17. String paramter = "";
  18. if (arguments != null) {
  19. try {
  20. paramter = JSONArray.fromObject(arguments).toString();
  21. } catch (Exception e) {
  22. paramter = arguments.toString();
  23. }
  24. }

将不能进行序列化的入参过滤掉,只要留下我们需要记录的入参参数记录到日志中即可。

发表评论

表情:
评论列表 (有 0 条评论,460人围观)

还没有评论,来说两句吧...

相关阅读