diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ResourceProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ResourceProperties.java index 71080b136b..4cd73676a2 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ResourceProperties.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ResourceProperties.java @@ -86,7 +86,16 @@ public class ResourceProperties implements ResourceLoaderAware { } public void setStaticLocations(String[] staticLocations) { - this.staticLocations = staticLocations; + this.staticLocations = appendSlashIfNecessary(staticLocations); + } + + private String[] appendSlashIfNecessary(String[] staticLocations) { + String[] normalized = new String[staticLocations.length]; + for (int i = 0; i < staticLocations.length; i++) { + String location = staticLocations[i]; + normalized[i] = location.endsWith("/") ? location : location + "/"; + } + return normalized; } public Resource getWelcomePage() { diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ResourcePropertiesTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ResourcePropertiesTests.java index a3fdf00956..48393aec54 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ResourcePropertiesTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ResourcePropertiesTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2016 the original author or authors. + * Copyright 2012-2017 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. @@ -18,7 +18,10 @@ package org.springframework.boot.autoconfigure.web; import org.junit.Test; +import org.springframework.boot.testutil.Matched; + import static org.assertj.core.api.Assertions.assertThat; +import static org.hamcrest.CoreMatchers.endsWith; /** * Tests for {@link ResourceProperties}. @@ -52,4 +55,17 @@ public class ResourcePropertiesTests { assertThat(this.properties.getChain().getEnabled()).isFalse(); } + @Test + public void defaultStaticLocationsAllEndWithTrailingSlash() { + assertThat(this.properties.getStaticLocations()).are(Matched.by(endsWith("/"))); + } + + @Test + public void customStaticLocationsAreNormalizedToEndWithTrailingSlash() { + this.properties.setStaticLocations(new String[] { "/foo", "/bar", "/baz/" }); + assertThat(this.properties.getStaticLocations()).containsExactly("/foo/", "/bar/", + "/baz/"); + + } + }