我正在用Jakarta EE创建Quarkus Web应用程序.我有一些@GET
和@POST
方法,我想记录它们的使用情况.为此,我使用这样的拦截器:
@Loggable
@Interceptor
@Priority(1)
public class Logger {
@Inject
private LogService logService;
@AroundInvoke
public Object logMethodEntry(InvocationContext context) throws Exception {
Date currentDateAndTime = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
String dateAndTime = dateFormat.format(currentDateAndTime);
String methodName = context.getTarget().getClass().getName() + "." + context.getMethod().getName();
String ipAddress = "";
Log log = new Log(dateAndTime, ipAddress, methodName);
logService.addLog(log);
return context.proceed();
}
}
此拦截器正在使用@Loggable
注册到目标类.我的问题是,如何检索客户端IP地址?我try 使用HttpServletRequest
,但出现依赖性编译错误.
注意:这仅用于@GET
和@POST
方法,因此总是有一个HTTP请求.