1. 程式人生 > >SpringBoot 攔截 response 記錄日誌

SpringBoot 攔截 response 記錄日誌

() com org instance response tin AS obj control

廢話不多說直接上代碼

package com.gs.techpub.filter;

import com.gridsum.techpub.utils.JsonUtil;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class ResponseFilter  {

    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @AfterReturning(returning = "ret", pointcut = "execution( * com.gs.techpub.controller.*.*(..))")
    public void doAfterReturning(Object ret) {
        logger.info("返回值 : " + JsonUtil.getInstance().toJson(ret));
    }
}

  記得加上依賴

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

  

SpringBoot 攔截 response 記錄日誌