Spring AOP custom annotations Get HTTP interface and WebService interface entry and exit parameters

Source: Internet
Author: User

The following methods can be obtained during the implementation of the annotation method:--for example

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

1. Define two method annotations to mark the HTTP interface and WebService interface to be processed, respectively:

HTTP interface Annotations

12345 @Retention(RetentionPolicy.RUNTIME)@Target({ ElementType.TYPE, ElementType.METHOD })public@interfaceAnnotationForIntfMark {    String value();}

WebService interface Annotations

12345 @Retention(RetentionPolicy.RUNTIME)@Target({ ElementType.TYPE, ElementType.METHOD })public@interfaceAnnotationForWsMark {    String value();}

2. Define spring AOP pointcuts, two interface annotation pointcuts, note the Intermediate | |, the Internet also indicates the use or, after trying to find that the pointcut after or is invalid

123 @Pointcut("@annotation(ms.platform.base.interfaces.AnnotationForIntfMark) || @annotation(ms.platform.base.interfaces.AnnotationForWsMark)")    publicvoidpointcut() {    }

3. Wrap-Around Join entry point

12345678910 @Around("pointcut()")    publicvoidhandle(ProceedingJoinPoint joinPoint) throwsThrowable {        StringBuffer sb = newStringBuffer();        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) Get interface entry parameter, Posthandle (retVal) get interface out parameter

123456789101112131415161718192021222324 privateString 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();        booleanisIntf = false;        StringBuffer sb = newStringBuffer();        for(inti = 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(intj = 0; j < args.length; j++) {                sb.append(JsonUtil.bean2json(args[j]));            }        }        returnsb.toString();    }

123 privateString postHandle(Object retVal) {        returnJsonUtil.bean2json(retVal);    }

1

5. Slice class definition, note the need to add @Component, otherwise you will not scan the slice class

12345 @Aspect@Componentpublicclass WebRequestAroundAdvice {}

Spring AOP custom annotations Get HTTP interface and WebService interface entry and exit parameters

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.