Skip to content

Commit c7c01f8

Browse files
committed
Add property for registering default Servlet
This commit adds a new configuration property for configuring the registration of the default Servlet in Servlet containers. `"server.servlet.default-servlet.registered=false"` The default of this property is still `true`, as it's been the case in previous releases. Closes gh-21214
1 parent 7671561 commit c7c01f8

File tree

4 files changed

+58
-0
lines changed

4 files changed

+58
-0
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.springframework.boot.web.server.Http2;
3939
import org.springframework.boot.web.server.Shutdown;
4040
import org.springframework.boot.web.server.Ssl;
41+
import org.springframework.boot.web.servlet.server.DefaultServlet;
4142
import org.springframework.boot.web.servlet.server.Encoding;
4243
import org.springframework.boot.web.servlet.server.Jsp;
4344
import org.springframework.boot.web.servlet.server.Session;
@@ -226,6 +227,9 @@ public static class Servlet {
226227
*/
227228
private String applicationDisplayName = "application";
228229

230+
@NestedConfigurationProperty
231+
private final DefaultServlet defaultServlet = new DefaultServlet();
232+
229233
@NestedConfigurationProperty
230234
private final Encoding encoding = new Encoding();
231235

@@ -263,6 +267,10 @@ public Map<String, String> getContextParameters() {
263267
return this.contextParameters;
264268
}
265269

270+
public DefaultServlet getDefaultServlet() {
271+
return this.defaultServlet;
272+
}
273+
266274
public Encoding getEncoding() {
267275
return this.encoding;
268276
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/ServletWebServerFactoryCustomizer.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ public void customize(ConfigurableServletWebServerFactory factory) {
5353
map.from(this.serverProperties::getAddress).to(factory::setAddress);
5454
map.from(this.serverProperties.getServlet()::getContextPath).to(factory::setContextPath);
5555
map.from(this.serverProperties.getServlet()::getApplicationDisplayName).to(factory::setDisplayName);
56+
map.from(this.serverProperties.getServlet().getDefaultServlet()::isRegistered)
57+
.to(factory::setRegisterDefaultServlet);
5658
map.from(this.serverProperties.getServlet()::getSession).to(factory::setSession);
5759
map.from(this.serverProperties::getSsl).to(factory::setSsl);
5860
map.from(this.serverProperties.getServlet()::getJsp).to(factory::setJsp);

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/ServletWebServerFactoryCustomizerTests.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,14 @@ void testCustomizeDisplayName() {
7474
verify(factory).setDisplayName("TestName");
7575
}
7676

77+
@Test
78+
void testCustomizeDefaultServlet() {
79+
ConfigurableServletWebServerFactory factory = mock(ConfigurableServletWebServerFactory.class);
80+
this.properties.getServlet().getDefaultServlet().setRegistered(true);
81+
this.customizer.customize(factory);
82+
verify(factory).setRegisterDefaultServlet(true);
83+
}
84+
7785
@Test
7886
void testCustomizeSsl() {
7987
ConfigurableServletWebServerFactory factory = mock(ConfigurableServletWebServerFactory.class);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2012-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.web.servlet.server;
18+
19+
/**
20+
* Default Servlet properties.
21+
*
22+
* @author Brian Clozel
23+
* @since 2.3.0
24+
*/
25+
public class DefaultServlet {
26+
27+
/**
28+
* Whether to enable the registration of the default Servlet with the container.
29+
*/
30+
private boolean registered = true;
31+
32+
public boolean isRegistered() {
33+
return this.registered;
34+
}
35+
36+
public void setRegistered(boolean registered) {
37+
this.registered = registered;
38+
}
39+
40+
}

0 commit comments

Comments
 (0)