From 6c098b330103b28c3a87ed5caae1e4d5990dbca2 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Wed, 2 Nov 2016 09:53:27 +0200 Subject: [PATCH] Polish --- .../DelegatingWebReactiveConfiguration.java | 4 +- .../reactive/config/EnableWebReactive.java | 24 ++++--- .../WebReactiveConfigurationSupport.java | 8 ++- .../config/WebReactiveConfigurer.java | 67 ++++++++++++------- .../WebReactiveConfigurerComposite.java | 4 +- .../resource/AppCacheManifestTransformer.java | 2 +- ...legatingWebReactiveConfigurationTests.java | 4 +- .../config/annotation/EnableWebMvc.java | 17 +++-- 8 files changed, 79 insertions(+), 51 deletions(-) diff --git a/spring-web-reactive/src/main/java/org/springframework/web/reactive/config/DelegatingWebReactiveConfiguration.java b/spring-web-reactive/src/main/java/org/springframework/web/reactive/config/DelegatingWebReactiveConfiguration.java index bfc3a7ac3de..6be4d79fc74 100644 --- a/spring-web-reactive/src/main/java/org/springframework/web/reactive/config/DelegatingWebReactiveConfiguration.java +++ b/spring-web-reactive/src/main/java/org/springframework/web/reactive/config/DelegatingWebReactiveConfiguration.java @@ -51,8 +51,8 @@ public class DelegatingWebReactiveConfiguration extends WebReactiveConfiguration } @Override - protected void configureRequestedContentTypeResolver(RequestedContentTypeResolverBuilder builder) { - this.configurers.configureRequestedContentTypeResolver(builder); + protected void configureContentTypeResolver(RequestedContentTypeResolverBuilder builder) { + this.configurers.configureContentTypeResolver(builder); } @Override diff --git a/spring-web-reactive/src/main/java/org/springframework/web/reactive/config/EnableWebReactive.java b/spring-web-reactive/src/main/java/org/springframework/web/reactive/config/EnableWebReactive.java index ed55479e151..dd84b554774 100644 --- a/spring-web-reactive/src/main/java/org/springframework/web/reactive/config/EnableWebReactive.java +++ b/spring-web-reactive/src/main/java/org/springframework/web/reactive/config/EnableWebReactive.java @@ -31,19 +31,19 @@ import org.springframework.context.annotation.Import; *
  * @Configuration
  * @EnableWebReactive
- * @ComponentScan(basePackageClasses = { MyConfiguration.class })
- * public class MyWebConfiguration {
+ * @ComponentScan(basePackageClasses = MyConfiguration.class)
+ * public class MyConfiguration {
  *
  * }
  * 
* - *

To customize the imported configuration, implement the interface - * {@link WebReactiveConfigurer} and override individual methods, e.g.: + *

To customize the imported configuration implement + * {@link WebReactiveConfigurer} and override individual methods as shown below: * *

  * @Configuration
- * @EnableWebMvc
- * @ComponentScan(basePackageClasses = { MyConfiguration.class })
+ * @EnableWebReactive
+ * @ComponentScan(basePackageClasses = MyConfiguration.class)
  * public class MyConfiguration implements WebReactiveConfigurer {
  *
  * 	   @Override
@@ -56,17 +56,23 @@ import org.springframework.context.annotation.Import;
  *         messageWriters.add(new MyHttpMessageWriter());
  * 	   }
  *
- *     // More overridden methods ...
  * }
  * 
* - *

If {@link WebReactiveConfigurer} does not expose some advanced setting that - * needs to be configured, consider removing the {@code @EnableWebReactive} + *

Note: only one {@code @Configuration} class may have the + * {@code @EnableWebReactive} annotation to import the Spring Web Reactive + * configuration. There can however be multiple {@code @Configuration} classes + * implementing {@code WebReactiveConfigurer} in order to customize the provided + * configuration. + * + *

If {@link WebReactiveConfigurer} does not expose some more advanced setting + * that needs to be configured consider removing the {@code @EnableWebReactive} * annotation and extending directly from {@link WebReactiveConfigurationSupport} * or {@link DelegatingWebReactiveConfiguration} if you still want to allow * {@link WebReactiveConfigurer} instances to customize the configuration. * * @author Brian Clozel + * @author Rossen Stoyanchev * @since 5.0 * @see WebReactiveConfigurer * @see WebReactiveConfigurationSupport diff --git a/spring-web-reactive/src/main/java/org/springframework/web/reactive/config/WebReactiveConfigurationSupport.java b/spring-web-reactive/src/main/java/org/springframework/web/reactive/config/WebReactiveConfigurationSupport.java index 342109f7fdc..ec6c3ff9044 100644 --- a/spring-web-reactive/src/main/java/org/springframework/web/reactive/config/WebReactiveConfigurationSupport.java +++ b/spring-web-reactive/src/main/java/org/springframework/web/reactive/config/WebReactiveConfigurationSupport.java @@ -167,7 +167,7 @@ public class WebReactiveConfigurationSupport implements ApplicationContextAware public CompositeContentTypeResolver webReactiveContentTypeResolver() { RequestedContentTypeResolverBuilder builder = new RequestedContentTypeResolverBuilder(); builder.mediaTypes(getDefaultMediaTypeMappings()); - configureRequestedContentTypeResolver(builder); + configureContentTypeResolver(builder); return builder.build(); } @@ -186,7 +186,7 @@ public class WebReactiveConfigurationSupport implements ApplicationContextAware /** * Override to configure how the requested content type is resolved. */ - protected void configureRequestedContentTypeResolver(RequestedContentTypeResolverBuilder builder) { + protected void configureContentTypeResolver(RequestedContentTypeResolverBuilder builder) { } /** @@ -319,7 +319,9 @@ public class WebReactiveConfigurationSupport implements ApplicationContextAware /** * Adds default converters that sub-classes can call from - * {@link #configureMessageReaders(List)}. + * {@link #configureMessageReaders(List)} for {@code byte[]}, + * {@code ByteBuffer}, {@code String}, {@code Resource}, JAXB2, and Jackson + * (if present on the classpath). */ protected final void addDefaultHttpMessageReaders(List> readers) { readers.add(new DecoderHttpMessageReader<>(new ByteArrayDecoder())); diff --git a/spring-web-reactive/src/main/java/org/springframework/web/reactive/config/WebReactiveConfigurer.java b/spring-web-reactive/src/main/java/org/springframework/web/reactive/config/WebReactiveConfigurer.java index 288c6e28153..71a9ced05b8 100644 --- a/spring-web-reactive/src/main/java/org/springframework/web/reactive/config/WebReactiveConfigurer.java +++ b/spring-web-reactive/src/main/java/org/springframework/web/reactive/config/WebReactiveConfigurer.java @@ -41,18 +41,20 @@ import org.springframework.web.reactive.result.method.HandlerMethodArgumentResol * overriding the relevant methods for your needs. * * @author Brian Clozel + * @author Rossen Stoyanchev * @since 5.0 */ public interface WebReactiveConfigurer { /** - * Configure how the requested content type is resolved. + * Configure how the content type requested for the response is resolved. *

The given builder will create a composite of multiple - * {@link RequestedContentTypeResolver}s, each defining a way to resolve the - * the requested content type (accept HTTP header, path extension, parameter, etc). - * @param builder factory that creates a {@link CompositeContentTypeResolver} instance + * {@link RequestedContentTypeResolver}s, each defining a way to resolve + * the the requested content type (accept HTTP header, path extension, + * parameter, etc). + * @param builder factory that creates a {@link CompositeContentTypeResolver} */ - default void configureRequestedContentTypeResolver(RequestedContentTypeResolverBuilder builder) { + default void configureContentTypeResolver(RequestedContentTypeResolverBuilder builder) { } /** @@ -79,71 +81,84 @@ public interface WebReactiveConfigurer { } /** - * Provide custom argument resolvers without overriding the built-in ones. - * @param resolvers a list of resolvers to add to the built-in ones + * Provide custom controller method argument resolvers. Such resolvers do + * not override and will be invoked after the built-in ones. + * @param resolvers a list of resolvers to add */ default void addArgumentResolvers(List resolvers) { } /** - * Configure the message readers to use for decoding controller method arguments. - *

If no message readers are specified, default readers will be added via + * Configure the message readers to use for decoding the request body where + * {@code @RequestBody} and {@code HttpEntity} controller method arguments + * are used. If none are specified, default ones are added based on * {@link WebReactiveConfigurationSupport#addDefaultHttpMessageReaders}. - * @param readers a list to add message readers to, initially an empty list + *

See {@link #extendMessageReaders(List)} for adding readers + * in addition to the default ones. + * @param readers an empty list to add message readers to */ default void configureMessageReaders(List> readers) { } /** - * Modify the list of message readers to use for decoding controller method arguments, - * for example to add some in addition to the ones already configured. + * An alternative to {@link #configureMessageReaders(List)} that allows + * modifying the message readers to use after default ones have been added. */ default void extendMessageReaders(List> readers) { } /** - * Add custom {@link Converter}s and {@link Formatter}s. + * Add custom {@link Converter}s and {@link Formatter}s for performing type + * conversion and formatting of controller method arguments. */ default void addFormatters(FormatterRegistry registry) { } /** - * Provide a custom {@link Validator}, instead of the instance configured by default. - *

Only a single instance is allowed, an error will be thrown if multiple - * {@code Validator}s are returned by {@code WebReactiveConfigurer}s. - * The default implementation returns {@code Optional.empty()}. + * Provide a custom {@link Validator}. + *

By default a validator for standard bean validation is created if + * bean validation api is present on the classpath. */ default Optional getValidator() { return Optional.empty(); } /** - * Provide a custom {@link MessageCodesResolver}, instead of using the one - * provided by {@link org.springframework.validation.DataBinder} instances. - * The default implementation returns {@code Optional.empty()}. + * Provide a custom {@link MessageCodesResolver} to use for data binding + * instead of the one created by default in + * {@link org.springframework.validation.DataBinder}. */ default Optional getMessageCodesResolver() { return Optional.empty(); } /** - * Configure the message writers to use for encoding return values. - *

If no message writers are specified, default writers will be added via + * Configure the message writers to use to encode the response body based on + * the return values of {@code @ResponseBody}, and {@code ResponseEntity} + * controller methods. If none are specified, default ones are added based on * {@link WebReactiveConfigurationSupport#addDefaultHttpMessageWriters(List)}. - * @param writers a list to add message writers to, initially an empty list + *

See {@link #extendMessageWriters(List)} for adding writers + * in addition to the default ones. + * @param writers a empty list to add message writers to */ default void configureMessageWriters(List> writers) { } /** - * Modify the list of message writers to use for encoding return values, - * for example to add some in addition to the ones already configured. + * An alternative to {@link #configureMessageWriters(List)} that allows + * modifying the message writers to use after default ones have been added. */ default void extendMessageWriters(List> writers) { } /** - * Configure view resolution for supporting template engines. + * Configure view resolution for processing the return values of controller + * methods that rely on resolving a + * {@link org.springframework.web.reactive.result.view.View} to render + * the response with. By default all controller methods rely on view + * resolution unless annotated with {@code @ResponseBody} or explicitly + * return {@code ResponseEntity}. A view may be specified explicitly with + * a String return value or implicitly, e.g. {@code void} return value. * @see ViewResolverRegistry */ default void configureViewResolvers(ViewResolverRegistry registry) { diff --git a/spring-web-reactive/src/main/java/org/springframework/web/reactive/config/WebReactiveConfigurerComposite.java b/spring-web-reactive/src/main/java/org/springframework/web/reactive/config/WebReactiveConfigurerComposite.java index b668c42ff5a..2a85049e928 100644 --- a/spring-web-reactive/src/main/java/org/springframework/web/reactive/config/WebReactiveConfigurerComposite.java +++ b/spring-web-reactive/src/main/java/org/springframework/web/reactive/config/WebReactiveConfigurerComposite.java @@ -51,8 +51,8 @@ public class WebReactiveConfigurerComposite implements WebReactiveConfigurer { @Override - public void configureRequestedContentTypeResolver(RequestedContentTypeResolverBuilder builder) { - this.delegates.stream().forEach(delegate -> delegate.configureRequestedContentTypeResolver(builder)); + public void configureContentTypeResolver(RequestedContentTypeResolverBuilder builder) { + this.delegates.stream().forEach(delegate -> delegate.configureContentTypeResolver(builder)); } @Override diff --git a/spring-web-reactive/src/main/java/org/springframework/web/reactive/resource/AppCacheManifestTransformer.java b/spring-web-reactive/src/main/java/org/springframework/web/reactive/resource/AppCacheManifestTransformer.java index e8ae7b2b200..1c782d2dea6 100644 --- a/spring-web-reactive/src/main/java/org/springframework/web/reactive/resource/AppCacheManifestTransformer.java +++ b/spring-web-reactive/src/main/java/org/springframework/web/reactive/resource/AppCacheManifestTransformer.java @@ -61,7 +61,7 @@ import org.springframework.web.server.ServerWebExchange; *

In order to serve manifest files with the proper {@code "text/manifest"} content type, * it is required to configure it with * {@code requestedContentTypeResolverBuilder.mediaType("appcache", MediaType.valueOf("text/manifest")} - * in {@code WebReactiveConfiguration.configureRequestedContentTypeResolver()}. + * in {@code WebReactiveConfiguration.configureContentTypeResolver()}. * * @author Rossen Stoyanchev * @author Brian Clozel diff --git a/spring-web-reactive/src/test/java/org/springframework/web/reactive/config/DelegatingWebReactiveConfigurationTests.java b/spring-web-reactive/src/test/java/org/springframework/web/reactive/config/DelegatingWebReactiveConfigurationTests.java index fcbc6c7f95e..6232937f594 100644 --- a/spring-web-reactive/src/test/java/org/springframework/web/reactive/config/DelegatingWebReactiveConfigurationTests.java +++ b/spring-web-reactive/src/test/java/org/springframework/web/reactive/config/DelegatingWebReactiveConfigurationTests.java @@ -80,7 +80,7 @@ public class DelegatingWebReactiveConfigurationTests { delegatingConfig.setConfigurers(Collections.singletonList(webReactiveConfigurer)); delegatingConfig.requestMappingHandlerMapping(); - verify(webReactiveConfigurer).configureRequestedContentTypeResolver(any(RequestedContentTypeResolverBuilder.class)); + verify(webReactiveConfigurer).configureContentTypeResolver(any(RequestedContentTypeResolverBuilder.class)); verify(webReactiveConfigurer).addCorsMappings(any(CorsRegistry.class)); verify(webReactiveConfigurer).configurePathMatching(any(PathMatchConfigurer.class)); } @@ -126,7 +126,7 @@ public class DelegatingWebReactiveConfigurationTests { verify(webReactiveConfigurer).configureMessageWriters(writers.capture()); verify(webReactiveConfigurer).extendMessageWriters(writers.capture()); - verify(webReactiveConfigurer).configureRequestedContentTypeResolver(any(RequestedContentTypeResolverBuilder.class)); + verify(webReactiveConfigurer).configureContentTypeResolver(any(RequestedContentTypeResolverBuilder.class)); } @Test diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/EnableWebMvc.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/EnableWebMvc.java index 8de751cc153..3960aa06838 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/EnableWebMvc.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/EnableWebMvc.java @@ -31,8 +31,8 @@ import org.springframework.context.annotation.Import; *

  * @Configuration
  * @EnableWebMvc
- * @ComponentScan(basePackageClasses = { MyConfiguration.class })
- * public class MyWebConfiguration {
+ * @ComponentScan(basePackageClasses = MyConfiguration.class)
+ * public class MyConfiguration {
  *
  * }
  * 
@@ -44,7 +44,7 @@ import org.springframework.context.annotation.Import; *
  * @Configuration
  * @EnableWebMvc
- * @ComponentScan(basePackageClasses = { MyConfiguration.class })
+ * @ComponentScan(basePackageClasses = MyConfiguration.class)
  * public class MyConfiguration extends WebMvcConfigurerAdapter {
  *
  * 	   @Override
@@ -57,12 +57,17 @@ import org.springframework.context.annotation.Import;
  *         converters.add(new MyHttpMessageConverter());
  * 	   }
  *
- *     // More overridden methods ...
  * }
  * 
* - *

If {@link WebMvcConfigurer} does not expose some advanced setting that - * needs to be configured, consider removing the {@code @EnableWebMvc} + *

Note: only one {@code @Configuration} class may have the + * {@code @EnableWebMvc} annotation to import the Spring Web MVC + * configuration. There can however be multiple {@code @Configuration} classes + * implementing {@code WebMvcConfigurer} in order to customize the provided + * configuration. + * + *

If {@link WebMvcConfigurer} does not expose some more advanced setting that + * needs to be configured consider removing the {@code @EnableWebMvc} * annotation and extending directly from {@link WebMvcConfigurationSupport} * or {@link DelegatingWebMvcConfiguration}, e.g.: *