正在回答
1回答
同学你好,同学是否是想通过aop获取请求参数,如果是,建议同学参考下方步骤试一下。
1、首先在你的Maven的pom文件里加入aop的依赖:
1 2 3 4 | < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-aop</ artifactId > </ dependency > |
2、在springboot中设置切面,如下所示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | import javax.servlet.http.HttpServletRequest; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.springframework.context.annotation.Configuration; import org.springframework.web.context.request.RequestAttributes; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import com.google.gson.Gson; import org.slf4j.Logger; import org.slf4j.LoggerFactory;; @Aspect //定义一个切面 @Configuration public class LogRecordAspect { private static final Logger logger = LoggerFactory.getLogger(UserInterceptor. class ); // 定义切点Pointcut @Pointcut ( "execution(* com.jiaobuchong.web.*Controller.*(..))" ) public void excudeService() { } @Around ( "excudeService()" ) public Object doAround(ProceedingJoinPoint pjp) throws Throwable { RequestAttributes ra = RequestContextHolder.getRequestAttributes(); ServletRequestAttributes sra = (ServletRequestAttributes) ra; HttpServletRequest request = sra.getRequest(); String url = request.getRequestURL().toString(); String method = request.getMethod(); String uri = request.getRequestURI(); String queryString = request.getQueryString(); logger.info( "请求开始, 各个参数, url: {}, method: {}, uri: {}, params: {}" , url, method, uri, queryString); // result的值就是被拦截方法的返回值 Object result = pjp.proceed(); Gson gson = new Gson(); logger.info( "请求结束,controller的返回值是 " + gson.toJson(result)); return result; } } |
祝学习愉快!
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧