- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
好奇是否有人可以分享一些关于为什么 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/
我是一名优秀的程序员,十分优秀!