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); } |
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