Spring AOP 后置通知修改响应httpstatus方式
作者:CatalpaFlat 时间:2022-06-05 06:54:26
Spring AOP后置通知修改响应httpstatus
1.定义Aspect
/**
* 响应体切面
* 后置通知修改httpstatus
*
* @author : CatalpaFlat
*/
@Component
@Aspect
public class ApiResponseAspect {
private Logger logger = LoggerFactory.getLogger(this.getClass());
/**
* 切面
*/
private final String POINT_CUT = "execution(* com.xxx.web.controller..*(..))";
@Pointcut(POINT_CUT)
private void pointcut() {
}
@AfterReturning(value = POINT_CUT, returning = "apiResponse", argNames = "apiResponse")
public void doAfterReturningAdvice2(ApiResponse apiResponse) {
logger.info("apiResponse:" + apiResponse);
Integer state = apiResponse.getState();
if (state != null) {
ServletRequestAttributes res = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
res.getResponse().setStatus(state);
}
}
}
2.使用
2.1.请求体
return ApiUtil.error(HttpStatus.UNSUPPORTED_MEDIA_TYPE.value(),"the request body is empty");
2.2.参数缺失
return ApiUtil.error(HttpStatus.BAD_REQUEST.value(),"Parameter id is empty");
2.3.权限认证
return ApiUtil.error(HttpStatus.UNAUTHORIZED.value(),"Current requests need user validation");
2.4.与资源存在冲突
return ApiUtil.error(HttpStatus.CONFLICT.value(),"Conflict with resources");
2.5.携带error信息
return ApiUtil.error(HttpStatus.BAD_REQUEST.value(),"There are some mistakes",obj);
3.ApiResponse响应体
public class ApiResponse {
private Integer state;
private String message;
private Object result;
private Object error;
}
4.ApiUtil
public class ApiUtil {
/**
* http回调错误
*/
public static ApiResponse error(Integer code, String msg) {
ApiResponse result = new ApiResponse();
result.setState(code);
result.setMessage(msg);
return result;
}
/**
* http回调错误
*/
public static ApiResponse error(Integer code, String msg,Object error) {
ApiResponse result = new ApiResponse();
result.setState(code);
result.setMessage(msg);
result.setError(error);
return result;
}
}
Spring AOP前后置通知最简单案例
仅仅针对于spring
案例分析:
该案例执行Demo类中的三个方法,分别输出Demo1,Demo2,Demo3
我们以Demo2为切点,分别执行前置通知和后置通知
1.首先导jar包
2.写applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 将Demo放入bean容器中 -->
<bean id="demo" class="com.hym.bean.Demo"></bean>
<!-- 将前置通知和后置通知也放入到bean容器中 id 自己任意取,后续引用就取id ,class是全类名 -->
<bean id ="myBefore" class="com.hym.advice.MyBeforeAdvice"></bean>
<bean id ="myAfter" class="com.hym.advice.MyAfterAdvice"></bean>
<aop:config>
<!-- 围绕的哪一个切点进行前后置通知 execution(* 全类名+方法名 ) 这是固定写法 id 自己取名,后续引用就取id-->
<aop:pointcut expression="execution(* com.hym.bean.Demo.Demo2())" id="mypoint"/>
<!-- 通知 根据advice-ref中的值 来区分是前置通知还是后置通知 。 值就是前后置通知的id pointcut-ref 是切点的id-->
<aop:advisor advice-ref="myBefore" pointcut-ref="mypoint"/>
<aop:advisor advice-ref="myAfter" pointcut-ref="mypoint"/>
</aop:config>
<!-- r如果存在两个参数,name和id 那么用以下的写法 -->
<!-- <aop:config>
<aop:pointcut expression="execution(* com.hym.bean.Demo.Demo2(String,int) and args(name,id)) " id=""/>
</aop:config> -->
</beans>
3.项目架构
4.Demo类
package com.hym.bean;
public class Demo {
public void Demo1() {
System.out.println("Demo1");
}
public void Demo2() {
System.out.println("Demo2");
}
public void Demo3() {
System.out.println("Demo3");
}
}
5.前后置通知
前置通知:
类中方法需要实现MethodBeforeAdvice
package com.hym.advice;
import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice;
public class MyAfterAdvice implements AfterReturningAdvice{
@Override
public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable {
System.out.println("执行后置通知");
}
}
后置通知:
类中方法需要实现AfterReturningAdvice
该接口命名规范与前置通知有差异,需注意
package com.hym.advice;
import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice;
public class MyAfterAdvice implements AfterReturningAdvice{
@Override
public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable {
System.out.println("执行后置通知");
}
}
最后测试类:
package com.hym.test;
import org.apache.catalina.core.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.hym.bean.Demo;
public class Test {
public static void main(String[] args) {
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
Demo demo = ac.getBean("demo",Demo.class);
demo.Demo1();
demo.Demo2();
demo.Demo3();
}
}
最终执行结果:
AOP:面向切面编程
在执行Demo时,是纵向执行的,先Demo1,Demo2,Demo3.
但是我们以Demo2为切点,添加了前后置通知,这三个形成了一个横向的切面过程。
来源:https://blog.csdn.net/DuShiWoDeCuo/article/details/78818413
标签:Spring,AOP,后置通知,httpstatus
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java实现人员信息管理系统
2023-11-02 05:21:31
![](https://img.aspxhome.com/file/2023/5/58425_0s.jpg)
Spring Security登录添加验证码的实现过程
2021-07-25 23:47:17
![](https://img.aspxhome.com/file/2023/2/119192_0s.png)
Java利用Dijkstra算法求解拓扑关系最短路径
2021-08-24 10:30:54
![](https://img.aspxhome.com/file/2023/0/68510_0s.jpg)
springboot+mybatis报错找不到实体类的问题
2023-05-20 02:04:10
![](https://img.aspxhome.com/file/2023/4/132154_0s.png)
Java经典面试题汇总--多线程
2023-07-13 01:17:48
![](https://img.aspxhome.com/file/2023/1/57521_0s.jpg)
SpringBoot实现MapperScan添加动态配置(占位符)
2023-11-26 05:08:06
Android string.xml中的替换方法
2021-11-10 11:47:56
Javaweb动态开发最重要的Servlet详解
2023-04-09 20:11:17
![](https://img.aspxhome.com/file/2023/4/84274_0s.png)
Android自定义View弹性滑动Scroller详解
2023-08-07 09:56:39
浅谈一下Java中的悲观锁和乐观锁
2023-08-12 05:54:27
关于springboot配置druid数据源不生效问题(踩坑记)
2021-08-04 13:46:08
![](https://img.aspxhome.com/file/2023/1/106081_0s.png)
Unity实现倒计时组件
2021-06-18 01:06:44
![](https://img.aspxhome.com/file/2023/7/100147_0s.gif)
Java的动态绑定与双分派_动力节点Java学院整理
2021-07-14 11:18:50
Android适配底部虚拟按键的方法详解
2023-11-09 18:20:55
C#中CheckedListBox控件的用法实例
2021-08-05 06:03:29
![](https://img.aspxhome.com/file/2023/5/94575_0s.png)
spring schedule实现动态配置执行时间
2022-09-06 18:47:30
Java中的多态用法实例分析
2021-07-11 18:36:35
![](https://img.aspxhome.com/file/2023/8/77098_0s.png)
Java三大特性之多态详解
2022-11-07 14:24:40
C# 设置Chart的X轴为时间轴详情
2022-03-14 12:48:30
![](https://img.aspxhome.com/file/2023/2/119512_0s.png)
Java实现简单的斗地主游戏
2023-03-13 10:42:57
![](https://img.aspxhome.com/file/2023/1/100661_0s.jpg)