diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/ManagementServerProperties.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/ManagementServerProperties.java index 5653d1f96bc..55855dc6a77 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/ManagementServerProperties.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/ManagementServerProperties.java @@ -26,11 +26,13 @@ import org.springframework.boot.autoconfigure.web.ServerProperties; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.security.config.http.SessionCreationPolicy; import org.springframework.util.ClassUtils; +import org.springframework.util.StringUtils; /** * Properties for the management server (e.g. port and path settings). * * @author Dave Syer + * @author Stephane Nicoll * @see ServerProperties */ @ConfigurationProperties(prefix = "management", ignoreUnknownFields = true) @@ -103,12 +105,22 @@ public class ManagementServerProperties implements SecurityPrerequisite { this.address = address; } + /** + * Return the context path with no trailing slash (i.e. the '/' root context is + * represented as the empty string). + * @return the context path (no trailing slash) + */ public String getContextPath() { return this.contextPath; } public void setContextPath(String contextPath) { - this.contextPath = contextPath; + if (StringUtils.hasText(contextPath) && contextPath.endsWith("/")) { + this.contextPath = contextPath.substring(0, contextPath.length() - 1); + } + else { + this.contextPath = contextPath; + } } public Security getSecurity() { diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/ManagementServerPropertiesAutoConfigurationTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/ManagementServerPropertiesAutoConfigurationTests.java index 850b316803a..96917c57c34 100644 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/ManagementServerPropertiesAutoConfigurationTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/ManagementServerPropertiesAutoConfigurationTests.java @@ -17,9 +17,6 @@ package org.springframework.boot.actuate.autoconfigure; import org.junit.Test; -import org.springframework.context.annotation.AnnotationConfigApplicationContext; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.nullValue; @@ -29,37 +26,38 @@ import static org.junit.Assert.assertThat; * Tests for {@link ManagementServerPropertiesAutoConfiguration}. * * @author Phillip Webb + * @author Stephane Nicoll */ public class ManagementServerPropertiesAutoConfigurationTests { @Test public void defaultManagementServerProperties() { - AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext( - ManagementServerPropertiesAutoConfiguration.class); - assertThat(context.getBean(ManagementServerProperties.class).getPort(), - nullValue()); - context.close(); + ManagementServerProperties properties = new ManagementServerProperties(); + assertThat(properties.getPort(), nullValue()); + assertThat(properties.getContextPath(), equalTo("")); } @Test - public void definedManagementServerProperties() throws Exception { - AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext( - Config.class, ManagementServerPropertiesAutoConfiguration.class); - assertThat(context.getBean(ManagementServerProperties.class).getPort(), - equalTo(Integer.valueOf(123))); - context.close(); + public void definedManagementServerProperties() { + ManagementServerProperties properties = new ManagementServerProperties(); + properties.setPort(123); + properties.setContextPath("/foo"); + assertThat(properties.getPort(), equalTo(123)); + assertThat(properties.getContextPath(), equalTo("/foo")); } - @Configuration - public static class Config { - - @Bean - public ManagementServerProperties managementServerProperties() { - ManagementServerProperties properties = new ManagementServerProperties(); - properties.setPort(123); - return properties; - } + @Test + public void trailingSlashOfContextPathIsRemoved() { + ManagementServerProperties properties = new ManagementServerProperties(); + properties.setContextPath("/foo/"); + assertThat(properties.getContextPath(), equalTo("/foo")); + } + @Test + public void slashOfContextPathIsDefaultValue() { + ManagementServerProperties properties = new ManagementServerProperties(); + properties.setContextPath("/"); + assertThat(properties.getContextPath(), equalTo("")); } }