如何在Spring Boot应用程序中使用AOP捕获Http动词和api端点 [英] How to capture Http verb and api end point using AOP in Spring Boot application
本文介绍了如何在Spring Boot应用程序中使用AOP捕获Http动词和api端点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我打算在我的Spring Boot应用程序中实现一个方面,以便在成功返回给定的rest API时捕获以下值:
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端点,例如/api/...
- Http动词.即PUT/POST等
- 请求有效负载和请求/查询参数
我正在这样做,如下:
@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
}
}
有人可以帮助捕获Http动词和api端点吗?
Could anyone please help here in capturing Http verb and api end point as well ?
推荐答案
您必须获取请求对象并可以从中获取所需的值
You have to get the request object and can get the values you required from it
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes())
.getRequest();
并使用 HttpServletRequest
request.getParameterMap()
request.getMethod()
request.getRequestURL()
这篇关于如何在Spring Boot应用程序中使用AOP捕获Http动词和api端点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文