Polish auto-configuration for Spring Web Services

Closes gh-5645
pull/5706/head
Stephane Nicoll 9 years ago
parent a7b3a913c2
commit f561d9d9d8

@ -29,8 +29,7 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import; import org.springframework.ws.config.annotation.EnableWs;
import org.springframework.ws.config.annotation.DelegatingWsConfiguration;
import org.springframework.ws.config.annotation.WsConfigurationSupport; import org.springframework.ws.config.annotation.WsConfigurationSupport;
import org.springframework.ws.transport.http.MessageDispatcherServlet; import org.springframework.ws.transport.http.MessageDispatcherServlet;
@ -38,6 +37,7 @@ import org.springframework.ws.transport.http.MessageDispatcherServlet;
* {@link EnableAutoConfiguration Auto-configuration} for Spring Web Services. * {@link EnableAutoConfiguration Auto-configuration} for Spring Web Services.
* *
* @author Vedran Pavic * @author Vedran Pavic
* @author Stephane Nicoll
* @since 1.4.0 * @since 1.4.0
*/ */
@Configuration @Configuration
@ -48,7 +48,7 @@ import org.springframework.ws.transport.http.MessageDispatcherServlet;
@AutoConfigureAfter(EmbeddedServletContainerAutoConfiguration.class) @AutoConfigureAfter(EmbeddedServletContainerAutoConfiguration.class)
public class WsAutoConfiguration { public class WsAutoConfiguration {
private WsProperties properties; private final WsProperties properties;
public WsAutoConfiguration(WsProperties properties) { public WsAutoConfiguration(WsProperties properties) {
this.properties = properties; this.properties = properties;
@ -63,15 +63,16 @@ public class WsAutoConfiguration {
String urlMapping = (path.endsWith("/") ? path + "*" : path + "/*"); String urlMapping = (path.endsWith("/") ? path + "*" : path + "/*");
ServletRegistrationBean registration = new ServletRegistrationBean( ServletRegistrationBean registration = new ServletRegistrationBean(
servlet, urlMapping); servlet, urlMapping);
registration.setLoadOnStartup(this.properties.getServlet().getLoadOnStartup()); WsProperties.Servlet servletProperties = this.properties.getServlet();
for (Map.Entry<String, String> entry : this.properties.getInit().entrySet()) { registration.setLoadOnStartup(servletProperties.getLoadOnStartup());
for (Map.Entry<String, String> entry : servletProperties.getInit().entrySet()) {
registration.addInitParameter(entry.getKey(), entry.getValue()); registration.addInitParameter(entry.getKey(), entry.getValue());
} }
return registration; return registration;
} }
@Configuration @Configuration
@Import(DelegatingWsConfiguration.class) @EnableWs
protected static class WsConfiguration { protected static class WsConfiguration {
} }

@ -28,6 +28,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
* {@link ConfigurationProperties} for Spring Web Services. * {@link ConfigurationProperties} for Spring Web Services.
* *
* @author Vedran Pavic * @author Vedran Pavic
* @author Stephane Nicoll
* @since 1.4.0 * @since 1.4.0
*/ */
@ConfigurationProperties("spring.ws") @ConfigurationProperties("spring.ws")
@ -40,11 +41,6 @@ public class WsProperties {
@Pattern(regexp = "/[^?#]*", message = "Path must start with /") @Pattern(regexp = "/[^?#]*", message = "Path must start with /")
private String path = "/services"; private String path = "/services";
/**
* Init parameters to pass to Spring Web Services via the servlet.
*/
private Map<String, String> init = new HashMap<String, String>();
private final Servlet servlet = new Servlet(); private final Servlet servlet = new Servlet();
public String getPath() { public String getPath() {
@ -55,25 +51,31 @@ public class WsProperties {
this.path = path; this.path = path;
} }
public Map<String, String> getInit() {
return this.init;
}
public void setInit(Map<String, String> init) {
this.init = init;
}
public Servlet getServlet() { public Servlet getServlet() {
return this.servlet; return this.servlet;
} }
public static class Servlet { public static class Servlet {
/**
* Servlet init parameters to pass to Spring Web Services.
*/
private Map<String, String> init = new HashMap<String, String>();
/** /**
* Load on startup priority of the Spring Web Services servlet. * Load on startup priority of the Spring Web Services servlet.
*/ */
private int loadOnStartup = -1; private int loadOnStartup = -1;
public Map<String, String> getInit() {
return this.init;
}
public void setInit(Map<String, String> init) {
this.init = init;
}
public int getLoadOnStartup() { public int getLoadOnStartup() {
return this.loadOnStartup; return this.loadOnStartup;
} }

@ -17,7 +17,6 @@
package org.springframework.boot.autoconfigure.ws; package org.springframework.boot.autoconfigure.ws;
import org.junit.After; import org.junit.After;
import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.ExpectedException; import org.junit.rules.ExpectedException;
@ -35,18 +34,14 @@ import static org.assertj.core.api.Assertions.assertThat;
* Tests for {@link WsAutoConfiguration}. * Tests for {@link WsAutoConfiguration}.
* *
* @author Vedran Pavic * @author Vedran Pavic
* @author Stephane Nicoll
*/ */
public class WsAutoConfigurationTests { public class WsAutoConfigurationTests {
private AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
@Rule @Rule
public ExpectedException thrown = ExpectedException.none(); public ExpectedException thrown = ExpectedException.none();
@Before private AnnotationConfigWebApplicationContext context;
public void setupContext() {
this.context.setServletContext(new MockServletContext());
}
@After @After
public void close() { public void close() {
@ -57,7 +52,7 @@ public class WsAutoConfigurationTests {
@Test @Test
public void defaultConfiguration() { public void defaultConfiguration() {
registerAndRefresh(WsAutoConfiguration.class); load(WsAutoConfiguration.class);
assertThat(this.context.getBeansOfType(ServletRegistrationBean.class)).hasSize(1); assertThat(this.context.getBeansOfType(ServletRegistrationBean.class)).hasSize(1);
} }
@ -66,27 +61,19 @@ public class WsAutoConfigurationTests {
public void customPathMustBeginWithASlash() { public void customPathMustBeginWithASlash() {
this.thrown.expect(BeanCreationException.class); this.thrown.expect(BeanCreationException.class);
this.thrown.expectMessage("Path must start with /"); this.thrown.expectMessage("Path must start with /");
EnvironmentTestUtils.addEnvironment(this.context, load(WsAutoConfiguration.class, "spring.ws.path=invalid");
"spring.ws.path=invalid");
registerAndRefresh(WsAutoConfiguration.class);
} }
@Test @Test
public void customPathWithTrailingSlash() { public void customPathWithTrailingSlash() {
EnvironmentTestUtils.addEnvironment(this.context, load(WsAutoConfiguration.class, "spring.ws.path=/valid/");
"spring.ws.path=/valid/");
registerAndRefresh(WsAutoConfiguration.class);
assertThat(this.context.getBean(ServletRegistrationBean.class).getUrlMappings()) assertThat(this.context.getBean(ServletRegistrationBean.class).getUrlMappings())
.contains("/valid/*"); .contains("/valid/*");
} }
@Test @Test
public void customPath() { public void customPath() {
EnvironmentTestUtils.addEnvironment(this.context, load(WsAutoConfiguration.class, "spring.ws.path=/valid");
"spring.ws.path=/valid");
registerAndRefresh(WsAutoConfiguration.class);
assertThat(this.context.getBeansOfType(ServletRegistrationBean.class)).hasSize(1); assertThat(this.context.getBeansOfType(ServletRegistrationBean.class)).hasSize(1);
assertThat(this.context.getBean(ServletRegistrationBean.class).getUrlMappings()) assertThat(this.context.getBean(ServletRegistrationBean.class).getUrlMappings())
.contains("/valid/*"); .contains("/valid/*");
@ -94,10 +81,7 @@ public class WsAutoConfigurationTests {
@Test @Test
public void customLoadOnStartup() { public void customLoadOnStartup() {
EnvironmentTestUtils.addEnvironment(this.context, load(WsAutoConfiguration.class, "spring.ws.servlet.load-on-startup=1");
"spring.ws.servlet.load-on-startup=1");
registerAndRefresh(WsAutoConfiguration.class);
ServletRegistrationBean registrationBean = this.context ServletRegistrationBean registrationBean = this.context
.getBean(ServletRegistrationBean.class); .getBean(ServletRegistrationBean.class);
assertThat(ReflectionTestUtils.getField(registrationBean, "loadOnStartup")) assertThat(ReflectionTestUtils.getField(registrationBean, "loadOnStartup"))
@ -106,19 +90,21 @@ public class WsAutoConfigurationTests {
@Test @Test
public void customInitParameters() { public void customInitParameters() {
EnvironmentTestUtils.addEnvironment(this.context, load(WsAutoConfiguration.class, "spring.ws.servlet.init.key1=value1",
"spring.ws.init.key1=value1", "spring.ws.init.key2=value2"); "spring.ws.servlet.init.key2=value2");
registerAndRefresh(WsAutoConfiguration.class);
ServletRegistrationBean registrationBean = this.context ServletRegistrationBean registrationBean = this.context
.getBean(ServletRegistrationBean.class); .getBean(ServletRegistrationBean.class);
assertThat(registrationBean.getInitParameters()).containsEntry("key1", "value1"); assertThat(registrationBean.getInitParameters()).containsEntry("key1", "value1");
assertThat(registrationBean.getInitParameters()).containsEntry("key2", "value2"); assertThat(registrationBean.getInitParameters()).containsEntry("key2", "value2");
} }
private void registerAndRefresh(Class<?>... annotatedClasses) { private void load(Class<?> config, String... environment) {
this.context.register(annotatedClasses); AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
this.context.refresh(); ctx.setServletContext(new MockServletContext());
EnvironmentTestUtils.addEnvironment(ctx, environment);
ctx.register(config);
ctx.refresh();
this.context = ctx;
} }
} }

@ -843,6 +843,7 @@
<spring-boot-repo>${spring-boot-repo}</spring-boot-repo> <spring-boot-repo>${spring-boot-repo}</spring-boot-repo>
<spring-docs-version>${spring.version}</spring-docs-version> <spring-docs-version>${spring.version}</spring-docs-version>
<spring-security-docs-version>${spring-security.version}</spring-security-docs-version> <spring-security-docs-version>${spring-security.version}</spring-security-docs-version>
<spring-ws-docs-version>${spring-ws.version}</spring-ws-docs-version>
<github-tag>${github-tag}</github-tag> <github-tag>${github-tag}</github-tag>
</attributes> </attributes>
</configuration> </configuration>

@ -406,6 +406,11 @@ content into your application; rather pick only the properties that you need.
spring.velocity.toolbox-config-location= # Velocity Toolbox config location. For instance `/WEB-INF/toolbox.xml` spring.velocity.toolbox-config-location= # Velocity Toolbox config location. For instance `/WEB-INF/toolbox.xml`
spring.velocity.view-names= # White list of view names that can be resolved. spring.velocity.view-names= # White list of view names that can be resolved.
# WEB SERVICES ({sc-spring-boot-autoconfigure}/ws/WsProperties.{sc-ext}[WsProperties])
spring.ws.path=/services # Path that serves as the base URI for the services.
spring.ws.servlet.init= # Servlet init parameters to pass to Spring Web Services.
spring.ws.servlet.load-on-startup=-1 # Load on startup priority of the Spring Web Services servlet.
[[common-application-properties-security]] [[common-application-properties-security]]
# ---------------------------------------- # ----------------------------------------

@ -37,6 +37,7 @@ Phillip Webb; Dave Syer; Josh Long; Stéphane Nicoll; Rob Winch; Andy Wilkinson;
:spring-boot-maven-plugin-site: http://docs.spring.io/spring-boot/docs/{spring-boot-docs-version}/maven-plugin :spring-boot-maven-plugin-site: http://docs.spring.io/spring-boot/docs/{spring-boot-docs-version}/maven-plugin
:spring-reference: http://docs.spring.io/spring/docs/{spring-docs-version}/spring-framework-reference/htmlsingle :spring-reference: http://docs.spring.io/spring/docs/{spring-docs-version}/spring-framework-reference/htmlsingle
:spring-security-reference: http://docs.spring.io/spring-security/site/docs/{spring-security-docs-version}/reference/htmlsingle :spring-security-reference: http://docs.spring.io/spring-security/site/docs/{spring-security-docs-version}/reference/htmlsingle
:spring-ws-reference: http://docs.spring.io/spring-ws/docs/{spring-ws-docs-version}/reference/htmlsingle
:spring-javadoc: http://docs.spring.io/spring/docs/{spring-docs-version}/javadoc-api/org/springframework :spring-javadoc: http://docs.spring.io/spring/docs/{spring-docs-version}/javadoc-api/org/springframework
:spring-amqp-javadoc: http://docs.spring.io/spring-amqp/docs/current/api/org/springframework/amqp :spring-amqp-javadoc: http://docs.spring.io/spring-amqp/docs/current/api/org/springframework/amqp
:spring-data-javadoc: http://docs.spring.io/spring-data/jpa/docs/current/api/org/springframework/data/jpa :spring-data-javadoc: http://docs.spring.io/spring-data/jpa/docs/current/api/org/springframework/data/jpa

@ -4990,6 +4990,28 @@ public class MyTest {
[[boot-features-websockets]]
== WebSockets
Spring Boot provides WebSockets auto-configuration for embedded Tomcat (8 and 7), Jetty 9
and Undertow. If you're deploying a war file to a standalone container, Spring Boot
assumes that the container will be responsible for the configuration of its WebSocket
support.
Spring Framework provides {spring-reference}/#websocket[rich WebSocket support] that can
be easily accessed via the `spring-boot-starter-websocket` module.
[[boot-features-ws]]
== Web Services
Spring Boot provides Web Services auto-configuration so that all is required is defining
your `Endpoints`.
The {spring-ws-reference}[Spring Web Services features] can be easily accessed via the
`spring-boot-starter-ws` module.
[[boot-features-developing-auto-configuration]] [[boot-features-developing-auto-configuration]]
== Creating your own auto-configuration == Creating your own auto-configuration
If you work in a company that develops shared libraries, or if you work on an open-source If you work in a company that develops shared libraries, or if you work on an open-source
@ -5198,18 +5220,6 @@ library.
[[boot-features-websockets]]
== WebSockets
Spring Boot provides WebSockets auto-configuration for embedded Tomcat (8 and 7), Jetty 9
and Undertow. If you're deploying a war file to a standalone container, Spring Boot
assumes that the container will be responsible for the configuration of its WebSocket
support.
Spring Framework provides {spring-reference}/#websocket[rich WebSocket support] that can
be easily accessed via the `spring-boot-starter-websocket` module.
[[boot-features-whats-next]] [[boot-features-whats-next]]
== What to read next == What to read next
If you want to learn more about any of the classes discussed in this section you can If you want to learn more about any of the classes discussed in this section you can

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2014 the original author or authors. * Copyright 2012-2016 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.

Loading…
Cancel
Save