diff --git a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/servlet/WebMvcTags.java b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/servlet/WebMvcTags.java index fcee87db738..8d933ec3559 100644 --- a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/servlet/WebMvcTags.java +++ b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/servlet/WebMvcTags.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -84,6 +84,7 @@ public final class WebMvcTags { if (!StringUtils.hasText(uri)) { uri = "/"; } + uri = uri.replaceAll("//+", "/").replaceAll("/$", ""); return Tag.of("uri", uri.isEmpty() ? "root" : uri); } diff --git a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/web/servlet/WebMvcTagsTests.java b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/web/servlet/WebMvcTagsTests.java index 61491afa04d..4ed229ad05d 100644 --- a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/web/servlet/WebMvcTagsTests.java +++ b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/web/servlet/WebMvcTagsTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,6 +22,7 @@ import org.junit.Test; import org.springframework.boot.actuate.metrics.web.servlet.WebMvcTags; import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.mock.web.MockHttpServletResponse; +import org.springframework.web.servlet.HandlerMapping; import static org.assertj.core.api.Assertions.assertThat; @@ -36,6 +37,13 @@ public class WebMvcTagsTests { private final MockHttpServletResponse response = new MockHttpServletResponse(); + @Test + public void uriTrailingSlashesAreSuppressed() { + this.request.setAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE, + "//foo/"); + assertThat(WebMvcTags.uri(this.request, null).getValue()).isEqualTo("/foo"); + } + @Test public void uriTagValueIsRedirectionWhenResponseStatusIs3xx() { this.response.setStatus(301); @@ -54,7 +62,7 @@ public class WebMvcTagsTests { public void uriTagToleratesCustomResponseStatus() { this.response.setStatus(601); Tag tag = WebMvcTags.uri(this.request, this.response); - assertThat(tag.getValue()).isEqualTo("/"); + assertThat(tag.getValue()).isEqualTo("root"); } }