关于Spring Boot 中AOP拦截入参记录日志报错的原因分析

在Spring Boot应用中使用AOP拦截记录请求参数时遇到`IllegalStateException`,原因是尝试将包括ServletRequest在内的非可序列化对象转换为JSON字符串。解决办法是避免对joinPoint.getArgs()获取的所有参数进行序列化,而是仅处理实际业务参数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

异常:java.lang.IllegalStateException: It is illegal to call this method if the current request is not in asynchronous mode (i.e. isAsyncStarted() returns false)


package cn.bainuo.gene.aspect;

import cn.bainuo.gene.common.utils.RedisUserUtil;
import cn.bainuo.gene.security.domain.User;
import cn.bainuo.gene.security.domain.vo.UserInfoVo;
import cn.bainuo.gene.security.service.LoginService;
import cn.bainuo.gene.utils.utils.HttpRequestUtil;
import cn.bainuo.gene.utils.utils.StringUtils;
import cn.bainuo.gene.utils.utils.SystemControllerLog;
import cn.bainuo.gene.utils.utils.SystemServiceLog;
import com.alibaba.fastjson.JSON;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;

@Aspect
@Component
@SuppressWarnings("all")
public class SystemLogAspect {
    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private LoginService loginService;


    @Autowired
    private RedisUserUtil redisUserUtil;

    @Pointcut("@annotation(cn.bainuo.gene.utils.utils.SystemServiceLog)")
    public void serviceAspect(){
    }

    @Pointcut("@annotation(cn.bainuo.gene.utils.utils.SystemControllerLog)")
    public void controllerAspect(){
    }

    @Before("
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值