Spring AOP 自定义注解获取http接口及WebService接口入参和出参

注解方法实现过程中可以采用如下获取方式:—以下为例

 HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

 

 

 

 

 

 

1.定义两个方法注解,分别标记要处理的http接口及Webservice接口:

http接口注解

1
2
3
4
5
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.METHOD })
public @interface AnnotationForIntfMark {
    String value();
}

WebService接口注解

1
2
3
4
5
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.METHOD })
public @interface AnnotationForWsMark {
    String value();
}

2.定义Spring AOP切入点,两种接口注解切入点,注意 中间用||,网上也有说明使用or,试过之后发现or后面的切入点无效

1
2
3
@Pointcut("@annotation(ms.platform.base.interfaces.AnnotationForIntfMark) || @annotation(ms.platform.base.interfaces.AnnotationForWsMark)")
    public void pointcut() {
    }

3.环绕式加入切入点

1
2
3
4
5
6
7
8
9
10
@Around("pointcut()")
    public void handle(ProceedingJoinPoint joinPoint) throws Throwable {
        StringBuffer sb = new StringBuffer();
        String reqParam = preHandle(joinPoint);
        sb.append("Input Param:【").append(reqParam).append("】").append("\n");
        Object retVal = joinPoint.proceed();
        String respParam = postHandle(retVal);
        sb.append("Output Param:【").append(respParam).append("】").append("\n");
        MSLog.error(sb.toString());
    }

 

4.preHandle(joinPoint)获取接口入参,postHandle(retVal)获取接口出参

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
private String preHandle(ProceedingJoinPoint joinPoint) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
                .getRequest();
        Signature signature = joinPoint.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        Method targetMethod = methodSignature.getMethod();
        Annotation[] annotations = targetMethod.getAnnotations();
        boolean isIntf = false;
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < annotations.length; i++) {
            if (annotations[i].annotationType().equals(AnnotationForIntfMark.class)) {
                sb.append(request.getAttribute("jsonContent"));
                isIntf = true;
                break;
            }
        }
        if (!isIntf) {
            Object[] args = joinPoint.getArgs();
            for (int j = 0; j < args.length; j++) {
                sb.append(JsonUtil.bean2json(args[j]));
            }
        }
        return sb.toString();
    }

 

 

 

1
2
3
private String postHandle(Object retVal) {
        return JsonUtil.bean2json(retVal);
    }

 

1
 

5.切面类定义,注意需要添加@Component,否则将扫描不到切面类

1
2
3
4
5
@Aspect
@Component
public class WebRequestAroundAdvice {
 
}

 

posted on   范兵  阅读(4758)  评论(0编辑  收藏  举报

编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix

导航

< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5
点击右上角即可分享
微信分享提示