Spring HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE并非始终设置

时间:2018-10-15 20:45:00

标签: spring spring-mvc

好奇是否有人可以分享一些关于为什么HttpServletRequest可能没有从Spring Framework设置特定属性的反馈。我们正在使用从HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE键入的属性。 (https://docs.spring.io/spring-framework/docs/4.3.4.RELEASE/javadoc-api/org/springframework/web/servlet/HandlerMapping.html

示例代码在调用链后位于OncePerRequestFilter.doFilterInternal()中。

protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
    throws ServletException, IOException {
    String path = (new UrlPathHelper()).getPathWithinApplication(request);
    chain.doFilter(request, response);
    String scrubbedPath = scrubPath(request, path);
}

private String scrubPath(HttpServletRequest request, String path) {
    String message = "Response Metric Info";
    String scrubbedPath = path;
    Object bestMatchingPattern = request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);
    if (bestMatchingPattern != null) {
        //This changes path from .../collection/{alias} to ../collection/alias
        scrubbedPath = fixSpecialCharacters(bestMatchingPattern.toString());
    } else {
        // this is temporary to help with understanding why we get multiple metric names for the same path
        message = "BEST_MATCHING_PATTERN_ATTRIBUTE null";
    }

    logHandlerInfo(message, request, path);

    return scrubbedPath;
}

// temp helper method to understand why some metrics are being formatted differently for the same path
// causing multiple metrics for the same path
private void logHandlerInfo(String message, HttpServletRequest request, String path) {
    final String LINE_SEPARATOR = System.lineSeparator();
    StringBuilder metricInfoBuilder = new StringBuilder();
    metricInfoBuilder.append(message);
    metricInfoBuilder.append(LINE_SEPARATOR).append(String.format("Input Path=%s", path));
    metricInfoBuilder.append(LINE_SEPARATOR).append(String.format("HandlerMapping.Path=%s", request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE)));
    metricInfoBuilder.append(LINE_SEPARATOR).append(String.format("HandlerMapping.Best=%s", request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE)));
    metricInfoBuilder.append(LINE_SEPARATOR).append(String.format("HandlerMapping.Introspect=%s", request.getAttribute(HandlerMapping.INTROSPECT_TYPE_LEVEL_MAPPING)));
    metricInfoBuilder.append(LINE_SEPARATOR).append(String.format("HandlerMapping.UriTemplate=%s", request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE)));
    metricInfoBuilder.append(LINE_SEPARATOR).append(String.format("HandlerMapping.Matrix=%s", request.getAttribute(HandlerMapping.MATRIX_VARIABLES_ATTRIBUTE)));
    metricInfoBuilder.append(LINE_SEPARATOR).append(String.format("HandlerMapping.MediaTypes=%s", request.getAttribute(HandlerMapping.PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE)));
    log.info(metricInfoBuilder.toString());
}

这是示例日志。

2018-10-08 20:51:58:300 [qtp1346560976-53746897] []  INFO  c.e.m.e.metrics.MetricsFilter - Response Metric Info
Input Path=/v1/group/test/collection/test-dev/status
HandlerMapping.Path=/v1/group/test/collection/test-dev/status
HandlerMapping.Best=/v1/group/{groupAlias}/collection/{alias}/status
HandlerMapping.Introspect=null
HandlerMapping.UriTemplate={groupAlias=test, alias=test-dev}

2018-10-08 20:46:57:745 [qtp1346560976-53746897] []  INFO  c.e.m.e.metrics.MetricsFilter - BEST_MATCHING_PATTERN_ATTRIBUTE null
Input Path=/v1/group/test/collection/test-dev/status
HandlerMapping.Path=null
HandlerMapping.Best=null
HandlerMapping.Introspect=null
HandlerMapping.UriTemplate=null

1 个答案:

答案 0 :(得分:0)

由HandlerMapping设置属性,该HandlerMapping为请求找到匹配的处理程序。正如Javadoc所指出的那样,并不是所有的HandlerMapping实现都需要这样做,但是AbstractUrlHandlerMapping和RequestMappingInfoHandlerMapping可以完成内置的实现。

对于丢失的属性,要么不添加,要么以后删除,这似乎都不大可能。为了进一步调试,我建议首先找出用于特定请求的HandlerMapping,例如,将org.springframework.web的日志级别设置为DEBUG。这将告诉您映射是否支持添加这些属性。如果它是上述之一的子类,则应该添加它们。

您还可以在代码中找到设置属性的位置,并放置一个断点以查看会发生什么。假设您发现确实添加了属性,则可以进行调试以查看是否由于某些原因而删除了它们。