springboot之aop切面获取请求
springboot之aop切面获取请求
项目场景:
在学习springboot的博客开发中,通过aop切面,对博客中的操作进行记录
问题描述:
问题:
在切面方法中,无法获取请求的参数和类名,方法,ip等
解决方法:
@Before("log()")//调用log方法public void doBefore(JoinPoint joinPoint){System.out.println("------程序执行之前-----------");ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();HttpServletRequest request = requestAttributes.getRequest();String url = request.getRequestURL().toString();String ip = request.getRemoteAddr();Object[] args = joinPoint.getArgs();
// 类名-->方法名String methodName = joinPoint.getSignature().getDeclaringTypeName()+"-->"+joinPoint.getSignature().getName();getRequestLog getRequestLog = new getRequestLog(url,ip,methodName,args);System.out.println(getRequestLog);}
过程:
RequestContextHolder是一个包含了request请求的容器,所以要获取请求中的信息,自然要从容器中获取。
然而RequestContextHolder只能获取RequestAttributes对象,要取得request,必须从ServletRequestAttributes获取
通过查看底层的源码,可以发现ServletRequestAttributes是继承了AbstractRequestAttributes,然后AbstractRequestAttributes继承了RequestAttributes
也就是说ServletRequestAttributes是RequestAttributes的子类,所以直接强转就可以了
通过强转后的ServletRequestAttributes直接getRequest,即可得到request对象。
使用了内部类封装所需要打印的值
// 内部类,封装需要得到的值private class getRequestLog{private String url;private String ip;private String methodName;private Object[] requestParms;public getRequestLog() {}public getRequestLog(String url, String ip, String methodName, Object[] requestParms) {this.url = url;this.ip = ip;this.methodName = methodName;this.requestParms = requestParms;}@Overridepublic String toString() {return "getRequestLog{" +"url='" + url + '\'' +", ip='" + ip + '\'' +", methodName='" + methodName + '\'' +", requestParms=" + Arrays.toString(requestParms) +'}';}
joinPoint的使用方法,参考博客:
joinPoint的使用
springboot之aop切面获取请求相关推荐
- @Aspect aop切面获取请求接口类名、方法名、及参数
1.自定义一个注解,通过将自定义的注解添加到对应的方法上,可实现对该方法进行增强操作 package com.basis.aspect;import java.lang.annotation.Elem ...
- aop springboot 传入参数_java相关:springboot配置aop切面日志打印过程解析
java相关:springboot配置aop切面日志打印过程解析 发布于 2020-3-31| 复制链接 摘记: 这篇文章主要介绍了springboot配置aop切面日志打印过程解析,文中通过示例代码 ...
- java 切面 不执行,解决springboot的aop切面不起作用问题(失效的排查)
检查下springboot的启动类是否开启扫描 @springbootapplication @componentscan(basepackages = {"com.zhangpu.spri ...
- SpringBoot之AOP切面的使用
相关文章: Spring AOP切面使用详细解析 SpringBoot之Listener注册到Spring容器中的多种方法 SpringBoot之Interceptor拦截器注入使用 SpringBo ...
- springboot实现AOP切面编程
概述 AOP(Aspect Oriented Programming) 即面向切面编程.面向切面是面向对象中的一种方式而已.在代码执行过程中,动态嵌入其他代码,叫做面向切面编程(将交叉业务逻辑封装成成 ...
- springboot配置aop切面详解
1 引入依赖 <dependency><groupId>org.springframework.boot</groupId><artifactId>sp ...
- aop切面获取方法参数,注解,及判断是否成功执行
项目中第一次用到自定义注解,这里简单记一下,以方便以后忘了来回顾一下 自定义注解 /*** 修改方法日志注解*/ @Target(ElementType.METHOD) @Documented @Re ...
- AOP切面获取参数的一个小技巧
一般来说,我们的参数,都是通过json传递的,那么这个问题就转化成了,从json中获取指定字符串的问题. OK,这个问题就简单了.如下: public static void main(String[ ...
- springboot中面向切面编程(AOP)
文章目录 1.什么是AOP 2.springboot使用aop 1.什么是AOP AOP就是通过动态为项目中某些类在运行过程中动态创建代理对象,在对象中完成附加功能,保证在处理业务时更加专注于自己的业 ...
最新文章
- mysql 安装包_ubuntu下安装mysql全记录
- thymeleaf 获取yml中的值_SpringBoot引入Thymeleaf
- HTTPS和HTTPS证书
- log4j/slf4j
- 作者:武永卫(1974-),男,清华大学计算机科学与技术系教授
- 用python批量下载网络图片_python批量下载图片的三种方法
- python创建person类用printinfo方法_Python学习期刊Day11类和对象(2),日记,与,下
- MyBatis中字符串拼接实现模糊查询的sql
- 百行代码解读阿里 AloT 芯片平台无剑 100!
- java给xyz大小排序_Java Collection - 002 排序
- axure rp 8.1.0.3379 密钥license,Axure8.1中文破解版 8.1.0.3379 含授权码
- tplink 智能dhcp服务器,tplink路由器设置:桥接提示“获取IP地址失败,请检查主路由器DHCP服务器是否开启”...
- 影响职场升迁的小动作
- 活血化瘀药题库【1】
- 直播预告 | 华南理工实验室专场二
- uni-app项目开发-----初级入门教程(从0到1制作打包自己的app)
- html转图片/html2canvas的使用/星座测试/类似于损友圈的活动
- 树莓派4B连不上wifi的一个意想不到的原因
- The 10th Zhejiang Provincial Collegiate Programming Contest 蒻菜的水题题解。
- Android处子Demo—简易的UC首页动画