问题
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:
- api endpoint i.e. like /api/ ...
- Http verb. i.e. PUT/POST etc
- 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