springboot打印请求路径
时间: 2023-10-24 07:37:51 浏览: 169
springboot获取URL请求参数的多种方式
可以使用@ControllerAdvice和@ExceptionHandler注释来实现在Spring Boot中打印请求路径。具体实现方法可以参考以下代码:
@ControllerAdvice
public class RequestLoggingAdvice {
private static final Logger LOGGER = LoggerFactory.getLogger(RequestLoggingAdvice.class);
@ExceptionHandler(Exception.class)
public void logException(Exception e, HttpServletRequest request) {
LOGGER.error("Exception on request {}", request.getRequestURI(), e);
}
@RequestMapping
public void before(HttpServletRequest request) {
LOGGER.info("Received request {}", request.getRequestURI());
}
@RequestMapping
public void after(HttpServletRequest request) {
LOGGER.info("Finished request {}", request.getRequestURI());
}
}
当你发送一个请求时,控制台将打印出类似于以下内容的日志:
2022-01-01 00:00:00.000 INFO RequestLoggingAdvice:27 - Received request /api/user/123
2022-01-01 00:00:00.100 ERROR RequestLoggingAdvice:21 - Exception on request /api/user/123
java.lang.NullPointerException: ...
2022-01-01 00:00:01.000 INFO RequestLoggingAdvice:31 - Finished request /api/user/123
阅读全文