Browse Source

Polishing

pull/1897/head
Juergen Hoeller 8 years ago
parent
commit
2b2bf27933
  1. 28
      spring-web/src/test/java/org/springframework/http/codec/support/CodecConfigurerTests.java
  2. 11
      spring-webflux/src/main/java/org/springframework/web/reactive/config/WebFluxConfigurationSupport.java
  3. 128
      spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.java

28
spring-web/src/test/java/org/springframework/http/codec/support/CodecConfigurerTests.java

@ -256,19 +256,6 @@ public class CodecConfigurerTests {
.filter(e -> e == decoder).orElse(null)); .filter(e -> e == decoder).orElse(null));
} }
@Test
public void protobufDecoderOverride() {
ProtobufDecoder decoder = new ProtobufDecoder(ExtensionRegistry.newInstance());
this.configurer.defaultCodecs().protobufDecoder(decoder);
assertSame(decoder, this.configurer.getReaders().stream()
.filter(writer -> writer instanceof DecoderHttpMessageReader)
.map(writer -> ((DecoderHttpMessageReader<?>) writer).getDecoder())
.filter(e -> ProtobufDecoder.class.equals(e.getClass()))
.findFirst()
.filter(e -> e == decoder).orElse(null));
}
@Test @Test
public void jackson2EncoderOverride() { public void jackson2EncoderOverride() {
Jackson2JsonEncoder encoder = new Jackson2JsonEncoder(); Jackson2JsonEncoder encoder = new Jackson2JsonEncoder();
@ -283,7 +270,20 @@ public class CodecConfigurerTests {
} }
@Test @Test
public void protobufWriterOverride() { public void protobufDecoderOverride() {
ProtobufDecoder decoder = new ProtobufDecoder(ExtensionRegistry.newInstance());
this.configurer.defaultCodecs().protobufDecoder(decoder);
assertSame(decoder, this.configurer.getReaders().stream()
.filter(writer -> writer instanceof DecoderHttpMessageReader)
.map(writer -> ((DecoderHttpMessageReader<?>) writer).getDecoder())
.filter(e -> ProtobufDecoder.class.equals(e.getClass()))
.findFirst()
.filter(e -> e == decoder).orElse(null));
}
@Test
public void protobufEncoderOverride() {
ProtobufEncoder encoder = new ProtobufEncoder(); ProtobufEncoder encoder = new ProtobufEncoder();
this.configurer.defaultCodecs().protobufEncoder(encoder); this.configurer.defaultCodecs().protobufEncoder(encoder);

11
spring-webflux/src/main/java/org/springframework/web/reactive/config/WebFluxConfigurationSupport.java

@ -126,17 +126,14 @@ public class WebFluxConfigurationSupport implements ApplicationContextAware {
mapping.setCorsConfigurations(getCorsConfigurations()); mapping.setCorsConfigurations(getCorsConfigurations());
PathMatchConfigurer configurer = getPathMatchConfigurer(); PathMatchConfigurer configurer = getPathMatchConfigurer();
Boolean useTrailingSlashMatch = configurer.isUseTrailingSlashMatch(); Boolean useTrailingSlashMatch = configurer.isUseTrailingSlashMatch();
if (useTrailingSlashMatch != null) { if (useTrailingSlashMatch != null) {
mapping.setUseTrailingSlashMatch(useTrailingSlashMatch); mapping.setUseTrailingSlashMatch(useTrailingSlashMatch);
} }
Boolean useCaseSensitiveMatch = configurer.isUseCaseSensitiveMatch(); Boolean useCaseSensitiveMatch = configurer.isUseCaseSensitiveMatch();
if (useCaseSensitiveMatch != null) { if (useCaseSensitiveMatch != null) {
mapping.setUseCaseSensitiveMatch(useCaseSensitiveMatch); mapping.setUseCaseSensitiveMatch(useCaseSensitiveMatch);
} }
Map<String, Predicate<Class<?>>> pathPrefixes = configurer.getPathPrefixes(); Map<String, Predicate<Class<?>>> pathPrefixes = configurer.getPathPrefixes();
if (pathPrefixes != null) { if (pathPrefixes != null) {
mapping.setPathPrefixes(pathPrefixes); mapping.setPathPrefixes(pathPrefixes);
@ -331,6 +328,10 @@ public class WebFluxConfigurationSupport implements ApplicationContextAware {
return initializer; return initializer;
} }
/**
* Return a {@link FormattingConversionService} for use with annotated controllers.
* <p>See {@link #addFormatters} as an alternative to overriding this method.
*/
@Bean @Bean
public FormattingConversionService webFluxConversionService() { public FormattingConversionService webFluxConversionService() {
FormattingConversionService service = new DefaultFormattingConversionService(); FormattingConversionService service = new DefaultFormattingConversionService();
@ -339,7 +340,9 @@ public class WebFluxConfigurationSupport implements ApplicationContextAware {
} }
/** /**
* Override to add custom {@link Converter}s and {@link Formatter Formatter}s. * Override this method to add custom {@link Converter} and/or {@link Formatter}
* delegates to the common {@link FormattingConversionService}.
* @see #webFluxConversionService()
*/ */
protected void addFormatters(FormatterRegistry registry) { protected void addFormatters(FormatterRegistry registry) {
} }

128
spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.java

@ -284,12 +284,10 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv
if (useSuffixPatternMatch != null) { if (useSuffixPatternMatch != null) {
mapping.setUseSuffixPatternMatch(useSuffixPatternMatch); mapping.setUseSuffixPatternMatch(useSuffixPatternMatch);
} }
Boolean useRegisteredSuffixPatternMatch = configurer.isUseRegisteredSuffixPatternMatch(); Boolean useRegisteredSuffixPatternMatch = configurer.isUseRegisteredSuffixPatternMatch();
if (useRegisteredSuffixPatternMatch != null) { if (useRegisteredSuffixPatternMatch != null) {
mapping.setUseRegisteredSuffixPatternMatch(useRegisteredSuffixPatternMatch); mapping.setUseRegisteredSuffixPatternMatch(useRegisteredSuffixPatternMatch);
} }
Boolean useTrailingSlashMatch = configurer.isUseTrailingSlashMatch(); Boolean useTrailingSlashMatch = configurer.isUseTrailingSlashMatch();
if (useTrailingSlashMatch != null) { if (useTrailingSlashMatch != null) {
mapping.setUseTrailingSlashMatch(useTrailingSlashMatch); mapping.setUseTrailingSlashMatch(useTrailingSlashMatch);
@ -299,12 +297,10 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv
if (pathHelper != null) { if (pathHelper != null) {
mapping.setUrlPathHelper(pathHelper); mapping.setUrlPathHelper(pathHelper);
} }
PathMatcher pathMatcher = configurer.getPathMatcher(); PathMatcher pathMatcher = configurer.getPathMatcher();
if (pathMatcher != null) { if (pathMatcher != null) {
mapping.setPathMatcher(pathMatcher); mapping.setPathMatcher(pathMatcher);
} }
Map<String, Predicate<Class<?>>> pathPrefixes = configurer.getPathPrefixes(); Map<String, Predicate<Class<?>>> pathPrefixes = configurer.getPathPrefixes();
if (pathPrefixes != null) { if (pathPrefixes != null) {
mapping.setPathPrefixes(pathPrefixes); mapping.setPathPrefixes(pathPrefixes);
@ -324,8 +320,8 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv
/** /**
* Provide access to the shared handler interceptors used to configure * Provide access to the shared handler interceptors used to configure
* {@link HandlerMapping} instances with. This method cannot be overridden, * {@link HandlerMapping} instances with.
* use {@link #addInterceptors(InterceptorRegistry)} instead. * <p>This method cannot be overridden; use {@link #addInterceptors} instead.
*/ */
protected final Object[] getInterceptors() { protected final Object[] getInterceptors() {
if (this.interceptors == null) { if (this.interceptors == null) {
@ -628,9 +624,8 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv
} }
/** /**
* Return a {@link FormattingConversionService} for use with annotated * Return a {@link FormattingConversionService} for use with annotated controllers.
* controller methods and the {@code spring:eval} JSP tag. * <p>See {@link #addFormatters} as an alternative to overriding this method.
* Also see {@link #addFormatters} as an alternative to overriding this method.
*/ */
@Bean @Bean
public FormattingConversionService mvcConversionService() { public FormattingConversionService mvcConversionService() {
@ -640,8 +635,9 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv
} }
/** /**
* Override this method to add custom {@link Converter}s and * Override this method to add custom {@link Converter} and/or {@link Formatter}
* {@link Formatter Converter}s and {@link Formatters}. * delegates to the common {@link FormattingConversionService}.
* @see #mvcConversionService()
*/ */
protected void addFormatters(FormatterRegistry registry) { protected void addFormatters(FormatterRegistry registry) {
} }
@ -686,9 +682,8 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv
/** /**
* Provide access to the shared custom argument resolvers used by the * Provide access to the shared custom argument resolvers used by the
* {@link RequestMappingHandlerAdapter} and the * {@link RequestMappingHandlerAdapter} and the {@link ExceptionHandlerExceptionResolver}.
* {@link ExceptionHandlerExceptionResolver}. This method cannot be * <p>This method cannot be overridden; use {@link #addArgumentResolvers} instead.
* overridden, use {@link #addArgumentResolvers(List)} instead.
* @since 4.3 * @since 4.3
*/ */
protected final List<HandlerMethodArgumentResolver> getArgumentResolvers() { protected final List<HandlerMethodArgumentResolver> getArgumentResolvers() {
@ -700,24 +695,21 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv
} }
/** /**
* Add custom {@link HandlerMethodArgumentResolver HandlerMethodArgumentResolvers} to use in addition to * Add custom {@link HandlerMethodArgumentResolver HandlerMethodArgumentResolvers}
* the ones registered by default. * to use in addition to the ones registered by default.
* <p>Custom argument resolvers are invoked before built-in resolvers * <p>Custom argument resolvers are invoked before built-in resolvers except for
* except for those that rely on the presence of annotations (e.g. * those that rely on the presence of annotations (e.g. {@code @RequestParameter},
* {@code @RequestParameter}, {@code @PathVariable}, etc.). * {@code @PathVariable}, etc). The latter can be customized by configuring the
* The latter can be customized by configuring the
* {@link RequestMappingHandlerAdapter} directly. * {@link RequestMappingHandlerAdapter} directly.
* @param argumentResolvers the list of custom converters; * @param argumentResolvers the list of custom converters (initially an empty list)
* initially an empty list.
*/ */
protected void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) { protected void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
} }
/** /**
* Provide access to the shared return value handlers used by the * Provide access to the shared return value handlers used by the
* {@link RequestMappingHandlerAdapter} and the * {@link RequestMappingHandlerAdapter} and the {@link ExceptionHandlerExceptionResolver}.
* {@link ExceptionHandlerExceptionResolver}. This method cannot be * <p>This method cannot be overridden; use {@link #addReturnValueHandlers} instead.
* overridden, use {@link #addReturnValueHandlers(List)} instead.
* @since 4.3 * @since 4.3
*/ */
protected final List<HandlerMethodReturnValueHandler> getReturnValueHandlers() { protected final List<HandlerMethodReturnValueHandler> getReturnValueHandlers() {
@ -729,27 +721,23 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv
} }
/** /**
* Add custom {@link HandlerMethodReturnValueHandler HandlerMethodReturnValueHandlers} in addition to the * Add custom {@link HandlerMethodReturnValueHandler HandlerMethodReturnValueHandlers}
* ones registered by default. * in addition to the ones registered by default.
* <p>Custom return value handlers are invoked before built-in ones except * <p>Custom return value handlers are invoked before built-in ones except for
* for those that rely on the presence of annotations (e.g. * those that rely on the presence of annotations (e.g. {@code @ResponseBody},
* {@code @ResponseBody}, {@code @ModelAttribute}, etc.). * {@code @ModelAttribute}, etc). The latter can be customized by configuring the
* The latter can be customized by configuring the
* {@link RequestMappingHandlerAdapter} directly. * {@link RequestMappingHandlerAdapter} directly.
* @param returnValueHandlers the list of custom handlers; * @param returnValueHandlers the list of custom handlers (initially an empty list)
* initially an empty list.
*/ */
protected void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> returnValueHandlers) { protected void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> returnValueHandlers) {
} }
/** /**
* Provides access to the shared {@link HttpMessageConverter HttpMessageConverters} used by the * Provides access to the shared {@link HttpMessageConverter HttpMessageConverters}
* {@link RequestMappingHandlerAdapter} and the * used by the {@link RequestMappingHandlerAdapter} and the
* {@link ExceptionHandlerExceptionResolver}. * {@link ExceptionHandlerExceptionResolver}.
* This method cannot be overridden. * <p>This method cannot be overridden; use {@link #configureMessageConverters} instead.
* Use {@link #configureMessageConverters(List)} instead. * Also see {@link #addDefaultHttpMessageConverters} for adding default message converters.
* Also see {@link #addDefaultHttpMessageConverters(List)} that can be
* used to add default message converters.
*/ */
protected final List<HttpMessageConverter<?>> getMessageConverters() { protected final List<HttpMessageConverter<?>> getMessageConverters() {
if (this.messageConverters == null) { if (this.messageConverters == null) {
@ -764,24 +752,22 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv
} }
/** /**
* Override this method to add custom {@link HttpMessageConverter HttpMessageConverters} to use * Override this method to add custom {@link HttpMessageConverter HttpMessageConverters}
* with the {@link RequestMappingHandlerAdapter} and the * to use with the {@link RequestMappingHandlerAdapter} and the
* {@link ExceptionHandlerExceptionResolver}. Adding converters to the * {@link ExceptionHandlerExceptionResolver}.
* list turns off the default converters that would otherwise be registered * <p>Adding converters to the list turns off the default converters that would
* by default. Also see {@link #addDefaultHttpMessageConverters(List)} that * otherwise be registered by default. Also see {@link #addDefaultHttpMessageConverters}
* can be used to add default message converters. * for adding default message converters.
* @param converters a list to add message converters to; * @param converters a list to add message converters to (initially an empty list)
* initially an empty list.
*/ */
protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) { protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
} }
/** /**
* Override this method to extend or modify the list of converters after it * Override this method to extend or modify the list of converters after it has
* has been configured. This may be useful for example to allow default * been configured. This may be useful for example to allow default converters
* converters to be registered and then insert a custom converter through * to be registered and then insert a custom converter through this method.
* this method. * @param converters the list of configured converters to extend
* @param converters the list of configured converters to extend.
* @since 4.1.3 * @since 4.1.3
*/ */
protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) { protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
@ -789,7 +775,7 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv
/** /**
* Adds a set of default HttpMessageConverter instances to the given list. * Adds a set of default HttpMessageConverter instances to the given list.
* Subclasses can call this method from {@link #configureMessageConverters(List)}. * Subclasses can call this method from {@link #configureMessageConverters}.
* @param messageConverters the list to add the default message converters to * @param messageConverters the list to add the default message converters to
*/ */
protected final void addDefaultHttpMessageConverters(List<HttpMessageConverter<?>> messageConverters) { protected final void addDefaultHttpMessageConverters(List<HttpMessageConverter<?>> messageConverters) {
@ -803,8 +789,8 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv
try { try {
messageConverters.add(new SourceHttpMessageConverter<>()); messageConverters.add(new SourceHttpMessageConverter<>());
} }
catch (Error err) { catch (Throwable ex) {
// Ignore when no TransformerFactory implementation is available // Ignore when no TransformerFactory implementation is available...
} }
messageConverters.add(new AllEncompassingFormHttpMessageConverter()); messageConverters.add(new AllEncompassingFormHttpMessageConverter());
@ -884,14 +870,12 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv
} }
/** /**
* Returns a {@link HandlerExceptionResolverComposite} containing a list * Returns a {@link HandlerExceptionResolverComposite} containing a list of exception
* of exception resolvers obtained either through * resolvers obtained either through {@link #configureHandlerExceptionResolvers} or
* {@link #configureHandlerExceptionResolvers(List)} or through * through {@link #addDefaultHandlerExceptionResolvers}.
* {@link #addDefaultHandlerExceptionResolvers(List)}. * <p><strong>Note:</strong> This method cannot be made final due to CGLIB constraints.
* <p><strong>Note:</strong> This method cannot be made final due to CGLib * Rather than overriding it, consider overriding {@link #configureHandlerExceptionResolvers}
* constraints. Rather than overriding it, consider overriding * which allows for providing a list of resolvers.
* {@link #configureHandlerExceptionResolvers(List)}, which allows
* providing a list of resolvers.
*/ */
@Bean @Bean
public HandlerExceptionResolver handlerExceptionResolver() { public HandlerExceptionResolver handlerExceptionResolver() {
@ -909,21 +893,20 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv
/** /**
* Override this method to configure the list of * Override this method to configure the list of
* {@link HandlerExceptionResolver HandlerExceptionResolvers} to use. Adding resolvers to the list * {@link HandlerExceptionResolver HandlerExceptionResolvers} to use.
* turns off the default resolvers that would otherwise be registered by * <p>Adding resolvers to the list turns off the default resolvers that would otherwise
* default. Also see {@link #addDefaultHandlerExceptionResolvers(List)} * be registered by default. Also see {@link #addDefaultHandlerExceptionResolvers}
* that can be used to add the default exception resolvers. * that can be used to add the default exception resolvers.
* @param exceptionResolvers a list to add exception resolvers to; * @param exceptionResolvers a list to add exception resolvers to (initially an empty list)
* initially an empty list.
*/ */
protected void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) { protected void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
} }
/** /**
* Override this method to extend or modify the list of * Override this method to extend or modify the list of
* {@link HandlerExceptionResolver HandlerExceptionResolvers} after it has been configured. This may * {@link HandlerExceptionResolver HandlerExceptionResolvers} after it has been configured.
* be useful for example to allow default resolvers to be registered and then * <p>This may be useful for example to allow default resolvers to be registered
* insert a custom one through this method. * and then insert a custom one through this method.
* @param exceptionResolvers the list of configured resolvers to extend. * @param exceptionResolvers the list of configured resolvers to extend.
* @since 4.3 * @since 4.3
*/ */
@ -931,7 +914,8 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv
} }
/** /**
* A method available to subclasses for adding default {@link HandlerExceptionResolver HandlerExceptionResolvers}. * A method available to subclasses for adding default
* {@link HandlerExceptionResolver HandlerExceptionResolvers}.
* <p>Adds the following exception resolvers: * <p>Adds the following exception resolvers:
* <ul> * <ul>
* <li>{@link ExceptionHandlerExceptionResolver} for handling exceptions through * <li>{@link ExceptionHandlerExceptionResolver} for handling exceptions through

Loading…
Cancel
Save