洞察如何通过低成本家政服务app实现高效管理与数字化转型
807
2022-11-18
Spring AOP 后置通知修改响应httpstatus方式
目录Spring AOP后置通知修改响应httpstatus1.定义Aspect2.使用3.ApiResponse响应体4.ApiUtilSpring AOP前后置通知最简单案例1.首先导jar包2.写applicationContext.xml3.项目架构4.Demo类5.前后置通知
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();
resuhttp://lt.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
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:aop="http://springframework.org/schema/aop" xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop.xsd">
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xmlns:aop="http://springframework.org/schema/aop"
xsi:schemaLocation="http://springframework.org/schema/beans
http://springframework.org/schema/beans/spring-beans.xsd
http://springframework.org/schema/aop
http://springframework.org/schema/aop/spring-aop.xsd">
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为切点,添加了前后置通知,这三个形成了一个横向的切面过程。
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~