@AfterThrowing(value = "execution(* com.dianda.change.service..*.*(..))", throwing = "ex") public void afterThrowingAdvice(JoinPoint joinPoint, Exception ex) { Object targetObject = joinPoint.getTarget(); Signature signature = joinPoint.getSignature(); saveErrorLog(targetObject, signature, ex); }这段代码里边怎么获取请求信息
时间: 2024-03-26 10:42:16 浏览: 179
在 `afterThrowingAdvice` 方法中,您可以通过获取 `HttpServletRequest` 对象来获取请求信息。可以通过以下方式来获取 `HttpServletRequest` 对象:
```java
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
```
然后您就可以通过 `HttpServletRequest` 对象获取请求信息了,比如请求 URL、请求参数等信息。例如:
```java
String url = request.getRequestURL().toString();
String method = request.getMethod();
String queryString = request.getQueryString();
Map<String, String[]> parameterMap = request.getParameterMap();
// ...
```
需要注意的是,在获取 `HttpServletRequest` 对象之前,需要确保 `RequestContextHolder.getRequestAttributes()` 不为空。如果为空,说明当前线程不是一个处理 HTTP 请求的线程,无法获取 `HttpServletRequest` 对象。
相关问题
解释以下代码 @AfterThrowing(throwing = "e", pointcut = "execution(* com.usian.user.controller.*.*(..))")
这段代码是一个 Spring AOP 的切面,用于捕获 com.usian.user.controller 包中所有方法的异常。具体来说,当这些方法抛出异常时,@AfterThrowing 注解会触发一个通知,将异常对象 e 作为参数传递给通知方法。同时,pointcut 属性指定了切入点,即哪些方法会被拦截。这里使用了通配符 *,表示匹配所有方法。
优化这段代码: @AfterThrowing(value = "execution(* com.dianda.change.service..*.*(..))", throwing = "ex") public void afterThrowingAdvice(JoinPoint joinPoint, Exception ex) { try { logger.info("进入异常通知方法..."); Object targetObject = joinPoint.getTarget(); Signature signature = joinPoint.getSignature(); // System.out.println("ExceptionClassName={} ===" + ex.getClass().getName()); // // System.out.println("ExceptionClassName={} ===" + ex.getClass().getName()); Map<String, Object> map = new HashMap<>(); map.put("id", UUID.randomUUID().toString()); map.put("JKMC", targetObject.getClass().getName() + " signatureName=" + signature.getName()); map.put("CWMX", ex.getMessage()); map.put("ADDTIME", Format.getDateTime()); InsertParams insertParams = InsertParams.createInsertParams("T_RUN_ERROR_LOG"); insertParams.addParamsForMap(map); baseDaoComponent.insertDataByParams(insertParams); } catch (Exception e) { e.printStackTrace(); } // System.out.println("exception occurred at class "+targetObject.getClass().getName()+ // "\n signatureName="+signature.getName()); //// System.out.println("ExceptionClassName={}"+ ex.getClass().getName()); // System.out.println("message:{}"+ ex.getMessage()); }
这段代码可以进行如下优化:
1. 将 try-catch 语句块中的代码提取到一个方法中,避免重复代码:
```java
private void saveErrorLog(Object targetObject, Signature signature, Exception ex) {
try {
logger.info("进入异常通知方法...");
Map<String, Object> map = new HashMap<>();
map.put("id", UUID.randomUUID().toString());
map.put("JKMC", targetObject.getClass().getName() + " signatureName=" + signature.getName());
map.put("CWMX", ex.getMessage());
map.put("ADDTIME", Format.getDateTime());
InsertParams insertParams = InsertParams.createInsertParams("T_RUN_ERROR_LOG");
insertParams.addParamsForMap(map);
baseDaoComponent.insertDataByParams(insertParams);
} catch (Exception e) {
e.printStackTrace();
}
}
```
2. 使用 @Slf4j 注解代替手动创建 logger 对象,简化代码:
```java
@AfterThrowing(value = "execution(* com.dianda.change.service..*.*(..))", throwing = "ex")
public void afterThrowingAdvice(JoinPoint joinPoint, Exception ex) {
Object targetObject = joinPoint.getTarget();
Signature signature = joinPoint.getSignature();
saveErrorLog(targetObject, signature, ex);
}
```
通过上述优化,代码变得更加简洁易读,也更加易于维护。
阅读全文