如何在 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 启动应用程序中实现一个方面,以便在成功返回时为给定的 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屋!
查看全文