Skip to content

Commit bd2735a

Browse files
committed
Add alwaysMapUrl flag to ServletRegistrationBean
Add an additional constructor to ServletRegistrationBean that indicates if a URL mapping is always required. If set to false, the registration will not longer use the default '/*' mapping. Fixes gh-2596
1 parent 90bff0b commit bd2735a

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

spring-boot/src/main/java/org/springframework/boot/context/embedded/ServletRegistrationBean.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.apache.commons.logging.Log;
3232
import org.apache.commons.logging.LogFactory;
3333
import org.springframework.util.Assert;
34+
import org.springframework.util.ObjectUtils;
3435

3536
/**
3637
* A {@link ServletContextInitializer} to register {@link Servlet}s in a Servlet 3.0+
@@ -40,7 +41,9 @@
4041
* <p>
4142
* The {@link #setServlet(Servlet) servlet} must be specified before calling
4243
* {@link #onStartup}. URL mapping can be configured used {@link #setUrlMappings} or
43-
* omitted when mapping to '/*'. The servlet name will be deduced if not specified.
44+
* omitted when mapping to '/*' (unless
45+
* {@link #ServletRegistrationBean(Servlet, boolean, String...) alwaysMapUrl} is set to
46+
* {@code false}). The servlet name will be deduced if not specified.
4447
*
4548
* @author Phillip Webb
4649
* @see ServletContextInitializer
@@ -56,6 +59,8 @@ public class ServletRegistrationBean extends RegistrationBean {
5659

5760
private Set<String> urlMappings = new LinkedHashSet<String>();
5861

62+
private boolean alwaysMapUrl = true;
63+
5964
private int loadOnStartup = -1;
6065

6166
private MultipartConfigElement multipartConfig;
@@ -73,9 +78,22 @@ public ServletRegistrationBean() {
7378
* @param urlMappings the URLs being mapped
7479
*/
7580
public ServletRegistrationBean(Servlet servlet, String... urlMappings) {
81+
this(servlet, true, urlMappings);
82+
}
83+
84+
/**
85+
* Create a new {@link ServletRegistrationBean} instance with the specified
86+
* {@link Servlet} and URL mappings.
87+
* @param servlet the servlet being mapped
88+
* @param alwaysMapUrl if omitted URL mappings should be replaced with '/*'
89+
* @param urlMappings the URLs being mapped
90+
*/
91+
public ServletRegistrationBean(Servlet servlet, boolean alwaysMapUrl,
92+
String... urlMappings) {
7693
Assert.notNull(servlet, "Servlet must not be null");
7794
Assert.notNull(urlMappings, "UrlMappings must not be null");
7895
this.servlet = servlet;
96+
this.alwaysMapUrl = alwaysMapUrl;
7997
this.urlMappings.addAll(Arrays.asList(urlMappings));
8098
}
8199

@@ -164,7 +182,7 @@ public void onStartup(ServletContext servletContext) throws ServletException {
164182
Assert.notNull(this.servlet, "Servlet must not be null");
165183
String name = getServletName();
166184
if (!isEnabled()) {
167-
logger.info("Filter " + name + " was not registered (disabled)");
185+
logger.info("Servlet " + name + " was not registered (disabled)");
168186
return;
169187
}
170188
logger.info("Mapping servlet: '" + name + "' to " + this.urlMappings);
@@ -186,10 +204,12 @@ protected void configure(ServletRegistration.Dynamic registration) {
186204
super.configure(registration);
187205
String[] urlMapping = this.urlMappings
188206
.toArray(new String[this.urlMappings.size()]);
189-
if (urlMapping.length == 0) {
207+
if (urlMapping.length == 0 && this.alwaysMapUrl) {
190208
urlMapping = DEFAULT_MAPPINGS;
191209
}
192-
registration.addMapping(urlMapping);
210+
if (!ObjectUtils.isEmpty(urlMapping)) {
211+
registration.addMapping(urlMapping);
212+
}
193213
registration.setLoadOnStartup(this.loadOnStartup);
194214
if (this.multipartConfig != null) {
195215
registration.setMultipartConfig(this.multipartConfig);

spring-boot/src/test/java/org/springframework/boot/context/embedded/ServletRegistrationBeanTests.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.mockito.MockitoAnnotations;
3737

3838
import static org.mockito.BDDMockito.given;
39+
import static org.mockito.Matchers.any;
3940
import static org.mockito.Matchers.anyObject;
4041
import static org.mockito.Matchers.anyString;
4142
import static org.mockito.Mockito.never;
@@ -196,4 +197,11 @@ public void modifyInitParameters() throws Exception {
196197
verify(this.registration).setInitParameters(Collections.singletonMap("a", "c"));
197198
}
198199

200+
@Test
201+
public void withoutDefaultMappings() throws Exception {
202+
ServletRegistrationBean bean = new ServletRegistrationBean(this.servlet, false);
203+
bean.onStartup(this.servletContext);
204+
verify(this.registration, never()).addMapping((String[]) any());
205+
}
206+
199207
}

0 commit comments

Comments
 (0)