Spring AOP 后置通知修改响应httpstatus方式
作者:CatalpaFlat 发布时间:2022-06-05 06:54:26
标签:Spring,AOP,后置通知,httpstatus
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
0
投稿
猜你喜欢
- 在之前的博客使用SpringMVC创建Web工程并使用SpringSecurity进行权限控制的详细配置方法 中,我们描述了如何配置一个基于
- SpringBoot @NotBlank错误java 验证出现如下错误:javax.validation.UnexpectedTypeExc
- 介绍Objects Comparer是用于对象比较的工具,c#常见的数据结构都是可以用这个三方库进行对比,比较复杂的对象也是可以比较的。简而
- Spring Security登录表单配置1.引入pom依赖创建一个Spring Boot工程,引入Web和Spring Security依
- Jackson 日期格式化技巧使用 Spring Boot 时,需要使用 Jackson 处理一些 Java Time API 类型的 JS
- 1. 问题描述springboot的面世,成为Java开发者的一大福音,大大提升了开发的效率,其实springboot只是在maven的基础
- @schedule注解动态配置时间间隔动态配置时间间隔是通过自己实现的任务注册到任务调度实现的,并在每次调度的时候更改下次调度时间间隔,如果
- 一.Unsafe类的源码分析JDK的rt.jar包中的Unsafe类提供了硬件级别的原子操作,Unsafe里面的方法都是native方法,通
- C#是一门支持多线程的语言,因此线程的使用也是比较常见的。由于线程的知识在Win32编程的时候已经说得过多,所以在.Net中很少介绍这部分(
- java web返回中文乱码ajax返回中文乱码问题 在浏览器按F12查看数据包可以看到charset为 iso-8859-1,这是spri
- 字节流和字符流对于文件必然有读和写的操作,读和写就对应了输入和输出流,流又分成字节和字符流。1.从对文件的操作来讲,有读和写的操作——也就是
- 此处网上最多的做法是需要修改tomcat的参数配置大致如下:<Connector port="8080" prot
- 1.基本介绍代码块又称为初始化块,属于类中的成员(类的一部分),类似于方法,讲逻辑语句封装在方法体中,用{}抱起来;但和方法不同,没有方法名
- package other;import java.security.MessageDigest;import java.security.
- 前言JavaEE(Java Enterprise Edition),Java企业版,是一个用于企业级web开发平台。最早由Sun公司定制并发
- class ftp{ private string host = null; &n
- 如下所示://定义二维数组写法1 class numthree{public static void main(String[] args)
- 有时候在配置中心有些参数是需要修改的,这时候如何不重启而达到实时生效的效果呢?添加依赖<dependencies>
- 引言Netty作为高性能的网络通信框架,它是IO模型演变过程中的产物。Netty以Java NIO为基础,是一种基于异步事件驱动的网络通信应
- 项目开发中,经常会遇到定时任务的场景,Spring提供了@Scheduled注解,方便进行定时任务的开发概述要使用@Scheduled注解,