How to capture Http verb and api end point using AOP in Spring Boot application

风流意气都作罢 提交于 2021-01-29 10:09:16

问题


I am planning to implement an aspect in order to capture the following values for a given rest API on successful return, in my spring boot application:

  1. api endpoint i.e. like /api/ ...
  2. Http verb. i.e. PUT/POST etc
  3. Request payload and request/query param

I am doing this as follows:

@Aspect
public class MyAspect {

  private final Logger log = LoggerFactory.getLogger(this.getClass());


  @Pointcut("within(com.web.rest.*)")
  public void applicationResourcePointcut() {
  }

  @AfterReturning(value = ("applicationResourcePointcut()"),
      returning = "returnValue")
  public void endpointAfterReturning(JoinPoint p, Object returnValue)  throws Throwable {
   
    ObjectMapper mapper = new ObjectMapper();
    mapper.enable(SerializationFeature.INDENT_OUTPUT);
    System.out.println("REQUEST PAYLOAD = " + mapper.writeValueAsString(p.getArgs()));
    System.out.println("METHOD NAME = " + p.getSignature().getName());
    System.out.println("RESPONSE OBJECT = " + mapper.writeValueAsString(returnValue));   
   
     //CAN NOT UNDERSTAND HOW TO CAPTURE HTTP VERB AND ENDPOINT HERE 
  }

}

Could anyone please help here in capturing Http verb and api end point as well ?


回答1:


You have to get the request object and can get the values you required from it

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

and use methods available in HttpServletRequest

request.getParameterMap()
request.getMethod()
request.getRequestURL()


来源:https://stackoverflow.com/questions/64242791/how-to-capture-http-verb-and-api-end-point-using-aop-in-spring-boot-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!