Add ServletRegistrationBean for DispatcherServlet
Mapping is exposed via server.servletPath. Fixes gh-379
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Copyright 2012-2013 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure.web;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.boot.context.embedded.ServletRegistrationBean;
|
||||
import org.springframework.boot.test.EnvironmentTestUtils;
|
||||
import org.springframework.mock.web.MockServletContext;
|
||||
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
|
||||
import org.springframework.web.servlet.DispatcherServlet;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*/
|
||||
public class DispatcherServletAutoConfigurationTests {
|
||||
|
||||
private AnnotationConfigWebApplicationContext context;
|
||||
|
||||
@Test
|
||||
public void registrationProperties() throws Exception {
|
||||
this.context = new AnnotationConfigWebApplicationContext();
|
||||
this.context.register(ServerPropertiesAutoConfiguration.class,
|
||||
DispatcherServletAutoConfiguration.class);
|
||||
this.context.setServletContext(new MockServletContext());
|
||||
this.context.refresh();
|
||||
assertNotNull(this.context.getBean(DispatcherServlet.class));
|
||||
ServletRegistrationBean registration = this.context
|
||||
.getBean(ServletRegistrationBean.class);
|
||||
assertEquals("[/]", registration.getUrlMappings().toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void servletPath() throws Exception {
|
||||
this.context = new AnnotationConfigWebApplicationContext();
|
||||
this.context.setServletContext(new MockServletContext());
|
||||
this.context.register(ServerPropertiesAutoConfiguration.class,
|
||||
DispatcherServletAutoConfiguration.class);
|
||||
EnvironmentTestUtils.addEnvironment(this.context, "server.servlet_path:/spring");
|
||||
this.context.refresh();
|
||||
assertNotNull(this.context.getBean(DispatcherServlet.class));
|
||||
ServletRegistrationBean registration = this.context
|
||||
.getBean(ServletRegistrationBean.class);
|
||||
assertEquals("[/spring]", registration.getUrlMappings().toString());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -32,6 +32,7 @@ import org.springframework.boot.context.embedded.MockEmbeddedServletContainerFac
|
||||
import org.springframework.boot.context.embedded.ServletRegistrationBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.servlet.DispatcherServlet;
|
||||
import org.springframework.web.servlet.FrameworkServlet;
|
||||
@@ -52,28 +53,21 @@ public class EmbeddedServletContainerAutoConfigurationTests {
|
||||
@Test
|
||||
public void createFromConfigClass() throws Exception {
|
||||
this.context = new AnnotationConfigEmbeddedWebApplicationContext(
|
||||
EmbeddedContainerConfiguration.class,
|
||||
EmbeddedServletContainerAutoConfiguration.class,
|
||||
DispatcherServletAutoConfiguration.class);
|
||||
BaseConfiguration.class);
|
||||
verifyContext();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void contextAlreadyHasDispatcherServletWithDefaultName() throws Exception {
|
||||
this.context = new AnnotationConfigEmbeddedWebApplicationContext(
|
||||
DispatcherServletConfiguration.class,
|
||||
EmbeddedContainerConfiguration.class,
|
||||
EmbeddedServletContainerAutoConfiguration.class,
|
||||
DispatcherServletAutoConfiguration.class);
|
||||
DispatcherServletConfiguration.class, BaseConfiguration.class);
|
||||
verifyContext();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void contextAlreadyHasDispatcherServlet() throws Exception {
|
||||
this.context = new AnnotationConfigEmbeddedWebApplicationContext(
|
||||
SpringServletConfiguration.class, EmbeddedContainerConfiguration.class,
|
||||
EmbeddedServletContainerAutoConfiguration.class,
|
||||
DispatcherServletAutoConfiguration.class);
|
||||
SpringServletConfiguration.class, BaseConfiguration.class);
|
||||
verifyContext();
|
||||
assertEquals(2, this.context.getBeanNamesForType(DispatcherServlet.class).length);
|
||||
}
|
||||
@@ -81,10 +75,7 @@ public class EmbeddedServletContainerAutoConfigurationTests {
|
||||
@Test
|
||||
public void contextAlreadyHasNonDispatcherServlet() throws Exception {
|
||||
this.context = new AnnotationConfigEmbeddedWebApplicationContext(
|
||||
NonSpringServletConfiguration.class,
|
||||
EmbeddedContainerConfiguration.class,
|
||||
EmbeddedServletContainerAutoConfiguration.class,
|
||||
DispatcherServletAutoConfiguration.class);
|
||||
NonSpringServletConfiguration.class, BaseConfiguration.class);
|
||||
verifyContext(); // the non default servlet is still registered
|
||||
assertEquals(0, this.context.getBeanNamesForType(DispatcherServlet.class).length);
|
||||
}
|
||||
@@ -92,9 +83,7 @@ public class EmbeddedServletContainerAutoConfigurationTests {
|
||||
@Test
|
||||
public void contextAlreadyHasNonServlet() throws Exception {
|
||||
this.context = new AnnotationConfigEmbeddedWebApplicationContext(
|
||||
NonServletConfiguration.class, EmbeddedContainerConfiguration.class,
|
||||
EmbeddedServletContainerAutoConfiguration.class,
|
||||
DispatcherServletAutoConfiguration.class);
|
||||
NonServletConfiguration.class, BaseConfiguration.class);
|
||||
assertEquals(0, this.context.getBeanNamesForType(DispatcherServlet.class).length);
|
||||
assertEquals(0, this.context.getBeanNamesForType(Servlet.class).length);
|
||||
}
|
||||
@@ -103,9 +92,7 @@ public class EmbeddedServletContainerAutoConfigurationTests {
|
||||
public void contextAlreadyHasDispatcherServletAndRegistration() throws Exception {
|
||||
this.context = new AnnotationConfigEmbeddedWebApplicationContext(
|
||||
DispatcherServletWithRegistrationConfiguration.class,
|
||||
EmbeddedContainerConfiguration.class,
|
||||
EmbeddedServletContainerAutoConfiguration.class,
|
||||
DispatcherServletAutoConfiguration.class);
|
||||
BaseConfiguration.class);
|
||||
verifyContext();
|
||||
assertEquals(1, this.context.getBeanNamesForType(DispatcherServlet.class).length);
|
||||
}
|
||||
@@ -113,20 +100,14 @@ public class EmbeddedServletContainerAutoConfigurationTests {
|
||||
@Test
|
||||
public void containerHasNoServletContext() throws Exception {
|
||||
this.context = new AnnotationConfigEmbeddedWebApplicationContext(
|
||||
EmbeddedContainerConfiguration.class,
|
||||
EnsureContainerHasNoServletContext.class,
|
||||
EmbeddedServletContainerAutoConfiguration.class,
|
||||
DispatcherServletAutoConfiguration.class);
|
||||
EnsureContainerHasNoServletContext.class, BaseConfiguration.class);
|
||||
verifyContext();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customizeContainerThroughCallback() throws Exception {
|
||||
this.context = new AnnotationConfigEmbeddedWebApplicationContext(
|
||||
EmbeddedContainerConfiguration.class,
|
||||
CallbackEmbeddedContainerCustomizer.class,
|
||||
EmbeddedServletContainerAutoConfiguration.class,
|
||||
DispatcherServletAutoConfiguration.class);
|
||||
CallbackEmbeddedContainerCustomizer.class, BaseConfiguration.class);
|
||||
verifyContext();
|
||||
assertEquals(9000, getContainerFactory().getPort());
|
||||
}
|
||||
@@ -144,6 +125,15 @@ public class EmbeddedServletContainerAutoConfigurationTests {
|
||||
return this.context.getBean(MockEmbeddedServletContainerFactory.class);
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@Import({ EmbeddedContainerConfiguration.class,
|
||||
EmbeddedServletContainerAutoConfiguration.class,
|
||||
DispatcherServletAutoConfiguration.class,
|
||||
ServerPropertiesAutoConfiguration.class })
|
||||
protected static class BaseConfiguration {
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@ConditionalOnExpression("true")
|
||||
public static class EmbeddedContainerConfiguration {
|
||||
|
||||
@@ -27,6 +27,7 @@ import org.springframework.boot.context.embedded.jetty.JettyEmbeddedServletConta
|
||||
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
@@ -64,10 +65,7 @@ public class MultipartAutoConfigurationTests {
|
||||
@Test
|
||||
public void containerWithNothing() {
|
||||
this.context = new AnnotationConfigEmbeddedWebApplicationContext(
|
||||
ContainerWithNothing.class,
|
||||
EmbeddedServletContainerAutoConfiguration.class,
|
||||
DispatcherServletAutoConfiguration.class,
|
||||
MultipartAutoConfiguration.class);
|
||||
ContainerWithNothing.class, BaseConfiguration.class);
|
||||
DispatcherServlet servlet = this.context.getBean(DispatcherServlet.class);
|
||||
assertNull(servlet.getMultipartResolver());
|
||||
assertEquals(0,
|
||||
@@ -83,10 +81,7 @@ public class MultipartAutoConfigurationTests {
|
||||
@Test
|
||||
public void containerWithNoMultipartJettyConfiguration() {
|
||||
this.context = new AnnotationConfigEmbeddedWebApplicationContext(
|
||||
ContainerWithNoMultipartJetty.class,
|
||||
EmbeddedServletContainerAutoConfiguration.class,
|
||||
DispatcherServletAutoConfiguration.class,
|
||||
MultipartAutoConfiguration.class);
|
||||
ContainerWithNoMultipartJetty.class, BaseConfiguration.class);
|
||||
DispatcherServlet servlet = this.context.getBean(DispatcherServlet.class);
|
||||
assertNull(servlet.getMultipartResolver());
|
||||
assertEquals(0,
|
||||
@@ -112,10 +107,7 @@ public class MultipartAutoConfigurationTests {
|
||||
@Test
|
||||
public void containerWithNoMultipartTomcatConfiguration() {
|
||||
this.context = new AnnotationConfigEmbeddedWebApplicationContext(
|
||||
ContainerWithNoMultipartTomcat.class,
|
||||
EmbeddedServletContainerAutoConfiguration.class,
|
||||
DispatcherServletAutoConfiguration.class,
|
||||
MultipartAutoConfiguration.class);
|
||||
ContainerWithNoMultipartTomcat.class, BaseConfiguration.class);
|
||||
DispatcherServlet servlet = this.context.getBean(DispatcherServlet.class);
|
||||
assertNull(servlet.getMultipartResolver());
|
||||
assertEquals(0,
|
||||
@@ -128,10 +120,7 @@ public class MultipartAutoConfigurationTests {
|
||||
@Test
|
||||
public void containerWithAutomatedMultipartJettyConfiguration() {
|
||||
this.context = new AnnotationConfigEmbeddedWebApplicationContext(
|
||||
ContainerWithEverythingJetty.class,
|
||||
EmbeddedServletContainerAutoConfiguration.class,
|
||||
DispatcherServletAutoConfiguration.class,
|
||||
MultipartAutoConfiguration.class);
|
||||
ContainerWithEverythingJetty.class, BaseConfiguration.class);
|
||||
this.context.getBean(MultipartConfigElement.class);
|
||||
assertSame(this.context.getBean(DispatcherServlet.class).getMultipartResolver(),
|
||||
this.context.getBean(StandardServletMultipartResolver.class));
|
||||
@@ -141,10 +130,7 @@ public class MultipartAutoConfigurationTests {
|
||||
@Test
|
||||
public void containerWithAutomatedMultipartTomcatConfiguration() throws Exception {
|
||||
this.context = new AnnotationConfigEmbeddedWebApplicationContext(
|
||||
ContainerWithEverythingTomcat.class,
|
||||
EmbeddedServletContainerAutoConfiguration.class,
|
||||
DispatcherServletAutoConfiguration.class,
|
||||
MultipartAutoConfiguration.class);
|
||||
ContainerWithEverythingTomcat.class, BaseConfiguration.class);
|
||||
new RestTemplate().getForObject("http://localhost:8080/", String.class);
|
||||
this.context.getBean(MultipartConfigElement.class);
|
||||
assertSame(this.context.getBean(DispatcherServlet.class).getMultipartResolver(),
|
||||
@@ -158,6 +144,14 @@ public class MultipartAutoConfigurationTests {
|
||||
"Hello");
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@Import({ EmbeddedServletContainerAutoConfiguration.class,
|
||||
DispatcherServletAutoConfiguration.class, MultipartAutoConfiguration.class,
|
||||
ServerPropertiesAutoConfiguration.class })
|
||||
protected static class BaseConfiguration {
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
public static class ContainerWithNoMultipartTomcat {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user