gpt4 book ai didi

Spring HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE 并不总是设置

转载 作者:行者123 更新时间:2023-12-03 12:14:50 28 4
gpt4 key购买 nike

好奇是否有人可以分享一些关于为什么 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

最佳答案

这些属性由为请求找到匹配处理程序的 HandlerMapping 设置。正如 Javadoc 所指出的,并非所有 HandlerMapping 实现都需要这样做,但 AbstractUrlHandlerMapping 和 RequestMappingInfoHandlerMapping 会这样做,这涵盖了内置实现。

对于缺失的属性,它们要么不添加,要么稍后被删除,这两种情况似乎都不太可能。为了进一步调试,我建议首先找出哪个 HandlerMapping 用于特定请求,例如将 org.springframework.web 的日志级别设置为 DEBUG。这将告诉您映射是否支持添加这些属性。如果它是上面提到的一个的子类,那么它应该添加它们。

您还可以在代码中找到设置属性的位置并放置断点以查看会发生什么。假设您发现确实添加了属性,那么您可以调试以查看它们是否因某种原因被删除。

关于Spring HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE 并不总是设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52824525/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com