diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java index d0be8c72f65..7704eb283a4 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java @@ -37,6 +37,7 @@ import org.springframework.boot.convert.DurationUnit; import org.springframework.boot.web.server.Compression; import org.springframework.boot.web.server.Cookie; import org.springframework.boot.web.server.Http2; +import org.springframework.boot.web.server.MimeMappings; import org.springframework.boot.web.server.Shutdown; import org.springframework.boot.web.server.Ssl; import org.springframework.boot.web.servlet.server.Encoding; @@ -71,6 +72,7 @@ import org.springframework.util.unit.DataSize; * @author Parviz Rozikov * @author Florian Storz * @author Michael Weidmann + * @author Lasse Wulff * @since 1.0.0 */ @ConfigurationProperties(prefix = "server", ignoreUnknownFields = true) @@ -115,6 +117,8 @@ public class ServerProperties { @NestedConfigurationProperty private final Compression compression = new Compression(); + private final MimeMappings mimeMappings = MimeMappings.lazyCopy(MimeMappings.DEFAULT); + @NestedConfigurationProperty private final Http2 http2 = new Http2(); @@ -186,6 +190,14 @@ public class ServerProperties { return this.compression; } + public MimeMappings getMimeMappings() { + return this.mimeMappings; + } + + public void setMimeMappings(Map customMappings) { + customMappings.forEach(this.mimeMappings::add); + } + public Http2 getHttp2() { return this.http2; } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/ServletWebServerFactoryCustomizer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/ServletWebServerFactoryCustomizer.java index 70706f93974..66c68a2d450 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/ServletWebServerFactoryCustomizer.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/ServletWebServerFactoryCustomizer.java @@ -38,6 +38,7 @@ import org.springframework.util.CollectionUtils; * @author Olivier Lamy * @author Yunkun Huang * @author Scott Frederick + * @author Lasse Wulff * @since 2.0.0 */ public class ServletWebServerFactoryCustomizer @@ -94,6 +95,7 @@ public class ServletWebServerFactoryCustomizer map.from(() -> this.cookieSameSiteSuppliers) .whenNot(CollectionUtils::isEmpty) .to(factory::setCookieSameSiteSuppliers); + map.from(this.serverProperties::getMimeMappings).to(factory::setMimeMappings); this.webListenerRegistrars.forEach((registrar) -> registrar.register(factory)); } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java index ff8b377d2d9..c5f580ff7ad 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java @@ -46,6 +46,8 @@ import org.springframework.boot.testsupport.web.servlet.DirtiesUrlFactories; import org.springframework.boot.web.embedded.jetty.JettyServletWebServerFactory; import org.springframework.boot.web.embedded.jetty.JettyWebServer; import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory; +import org.springframework.boot.web.server.MimeMappings; +import org.springframework.boot.web.server.MimeMappings.Mapping; import org.springframework.test.util.ReflectionTestUtils; import org.springframework.util.unit.DataSize; @@ -66,6 +68,7 @@ import static org.assertj.core.api.Assertions.assertThat; * @author Rafiullah Hamedy * @author Chris Bono * @author Parviz Rozikov + * @author Lasse Wulff */ @DirtiesUrlFactories class ServerPropertiesTests { @@ -182,6 +185,21 @@ class ServerPropertiesTests { assertThat(this.properties.getServlet().getContextPath()).isEqualTo("/assets /copy"); } + @Test + void testDefaultMimeMapping() { + assertThat(this.properties.getMimeMappings()) + .containsExactly(MimeMappings.DEFAULT.getAll().toArray(new Mapping[0])); + } + + @Test + void testCustomizedMimeMapping() { + MimeMappings expectedMappings = MimeMappings.lazyCopy(MimeMappings.DEFAULT); + expectedMappings.add("mjs", "text/javascript"); + bind("server.mime-mappings.mjs", "text/javascript"); + assertThat(this.properties.getMimeMappings()) + .containsExactly(expectedMappings.getAll().toArray(new Mapping[0])); + } + @Test void testCustomizeUriEncoding() { bind("server.tomcat.uri-encoding", "US-ASCII"); diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/ServletWebServerFactoryCustomizerTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/ServletWebServerFactoryCustomizerTests.java index c428ff17819..cc77a1a8c2c 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/ServletWebServerFactoryCustomizerTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/ServletWebServerFactoryCustomizerTests.java @@ -45,6 +45,7 @@ import static org.mockito.Mockito.mock; * * @author Brian Clozel * @author Yunkun Huang + * @author Lasse Wulff */ class ServletWebServerFactoryCustomizerTests { @@ -72,6 +73,13 @@ class ServletWebServerFactoryCustomizerTests { then(factory).should().setDisplayName("TestName"); } + @Test + void testCustomMimeMappings() { + ConfigurableServletWebServerFactory factory = mock(ConfigurableServletWebServerFactory.class); + this.customizer.customize(factory); + then(factory).should().setMimeMappings(this.properties.getMimeMappings()); + } + @Test void testCustomizeDefaultServlet() { ConfigurableServletWebServerFactory factory = mock(ConfigurableServletWebServerFactory.class);