【问题标题】:How do I log the request path for invalid 404 requests in Spring Boot?如何在 Spring Boot 中记录无效 404 请求的请求路径?
【发布时间】:2019-04-11 11:17:15
【问题描述】:

我尝试了以下方法来记录请求路径,但它们没有给出正确的值。请求也没有被重定向。

@Controller
@ApiIgnore
public class AppController implements ErrorController{

  @RequestMapping("/error")
  public void invalidRequest(HttpServletRequest request, HttpServletResponse response){

    CustomLogger.error(TAG, "invalidRequest()", "invalid request: " + request.getHeader("path"));
    CustomLogger.error(TAG, "invalidRequest()", "invalid request: " + request.getPathInfo());
    CustomLogger.error(TAG, "invalidRequest()", "invalid request: " + request.getPathTranslated());
    CustomLogger.error(TAG, "invalidRequest()", "invalid request: " + request.getContextPath());
    CustomLogger.error(TAG, "invalidRequest()", "invalid request: " + request.getServletPath());
    ...
  }

  @Override
  public String getErrorPath() {
    return "/error";
  }

当我提出请求时,我得到

$ curl -i localhost:8080/static/img/notthere.png
HTTP/1.1 200
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
X-Frame-Options: DENY
Content-Length: 27
Date: Wed, 07 Nov 2018 23:13:30 GMT

This is not a valid request

这就是日志中打印的内容:

2018-11-07 18:13:30.967 ERROR 14432 --- [nio-8080-exec-1] c.s.s.apis.AppController  : invalid request: null
2018-11-07 18:13:30.968 ERROR 14432 --- [nio-8080-exec-1] c.s.s.apis.AppController  : invalid request: null
2018-11-07 18:13:30.968 ERROR 14432 --- [nio-8080-exec-1] c.s.s.apis.AppController  : invalid request: null
2018-11-07 18:13:30.969 ERROR 14432 --- [nio-8080-exec-1] c.s.s.apis.AppController  : invalid request:
2018-11-07 18:13:30.969 ERROR 14432 --- [nio-8080-exec-1] c.s.s.apis.AppController  : invalid request: /error

【问题讨论】:

标签: java spring-mvc spring-boot


【解决方案1】:

你可以使用Request Dispatcherhere获取实际的uri

request.getAttribute(RequestDispatcher.FORWARD_REQUEST_URI);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-08
    • 2018-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-05
    • 2021-05-06
    相关资源
    最近更新 更多