diff --git a/spring-web/src/main/java/org/springframework/web/accept/ContentNegotiationManager.java b/spring-web/src/main/java/org/springframework/web/accept/ContentNegotiationManager.java index 39805bc0ad9d..3913d4a6a95d 100644 --- a/spring-web/src/main/java/org/springframework/web/accept/ContentNegotiationManager.java +++ b/spring-web/src/main/java/org/springframework/web/accept/ContentNegotiationManager.java @@ -39,6 +39,7 @@ * {@code MediaTypeFileExtensionResolver} instances. * * @author Rossen Stoyanchev + * @author Juergen Hoeller * @since 3.2 */ public class ContentNegotiationManager implements ContentNegotiationStrategy, MediaTypeFileExtensionResolver { @@ -65,6 +66,7 @@ public ContentNegotiationManager(ContentNegotiationStrategy... strategies) { * A collection-based alternative to * {@link #ContentNegotiationManager(ContentNegotiationStrategy...)}. * @param strategies the strategies to use + * @since 3.2.2 */ public ContentNegotiationManager(Collection strategies) { Assert.notEmpty(strategies, "At least one ContentNegotiationStrategy is expected"); @@ -95,7 +97,7 @@ public List getStrategies() { /** * Find a {@code ContentNegotiationStrategy} of the given type. * @param strategyType the strategy type - * @return the first matching strategy or {@code null}. + * @return the first matching strategy, or {@code null} if none * @since 4.3 */ @SuppressWarnings("unchecked") diff --git a/spring-web/src/main/java/org/springframework/web/accept/ContentNegotiationManagerFactoryBean.java b/spring-web/src/main/java/org/springframework/web/accept/ContentNegotiationManagerFactoryBean.java index dfcd84990e8c..3c875529d2a7 100644 --- a/spring-web/src/main/java/org/springframework/web/accept/ContentNegotiationManagerFactoryBean.java +++ b/spring-web/src/main/java/org/springframework/web/accept/ContentNegotiationManagerFactoryBean.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2017 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. @@ -85,6 +85,7 @@ * extension to a MediaType. You may {@link #setUseJaf suppress} the use of JAF. * * @author Rossen Stoyanchev + * @author Brian Clozel * @since 3.2 */ public class ContentNegotiationManagerFactoryBean @@ -255,8 +256,7 @@ public void afterPropertiesSet() { if (this.favorPathExtension) { PathExtensionContentNegotiationStrategy strategy; if (this.servletContext != null && !isUseJafTurnedOff()) { - strategy = new ServletPathExtensionContentNegotiationStrategy( - this.servletContext, this.mediaTypes); + strategy = new ServletPathExtensionContentNegotiationStrategy(this.servletContext, this.mediaTypes); } else { strategy = new PathExtensionContentNegotiationStrategy(this.mediaTypes); @@ -269,8 +269,7 @@ public void afterPropertiesSet() { } if (this.favorParameter) { - ParameterContentNegotiationStrategy strategy = - new ParameterContentNegotiationStrategy(this.mediaTypes); + ParameterContentNegotiationStrategy strategy = new ParameterContentNegotiationStrategy(this.mediaTypes); strategy.setParameterName(this.parameterName); strategies.add(strategy); } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/AsyncSupportConfigurer.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/AsyncSupportConfigurer.java index a1dc62a8d46f..27cbda385ecb 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/AsyncSupportConfigurer.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/AsyncSupportConfigurer.java @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.springframework.web.servlet.config.annotation; import java.util.ArrayList; @@ -22,7 +23,6 @@ import org.springframework.core.task.AsyncTaskExecutor; import org.springframework.core.task.SimpleAsyncTaskExecutor; -import org.springframework.util.Assert; import org.springframework.web.context.request.async.CallableProcessingInterceptor; import org.springframework.web.context.request.async.DeferredResult; import org.springframework.web.context.request.async.DeferredResultProcessingInterceptor; @@ -82,7 +82,6 @@ public AsyncSupportConfigurer setDefaultTimeout(long timeout) { * @param interceptors the interceptors to register */ public AsyncSupportConfigurer registerCallableInterceptors(CallableProcessingInterceptor... interceptors) { - Assert.notNull(interceptors, "Interceptors are required"); this.callableInterceptors.addAll(Arrays.asList(interceptors)); return this; } @@ -93,7 +92,6 @@ public AsyncSupportConfigurer registerCallableInterceptors(CallableProcessingInt * @param interceptors the interceptors to register */ public AsyncSupportConfigurer registerDeferredResultInterceptors(DeferredResultProcessingInterceptor... interceptors) { - Assert.notNull(interceptors, "Interceptors are required"); this.deferredResultInterceptors.addAll(Arrays.asList(interceptors)); return this; } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ContentNegotiationConfigurer.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ContentNegotiationConfigurer.java index 15227575e800..2bb588be6e25 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ContentNegotiationConfigurer.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ContentNegotiationConfigurer.java @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.springframework.web.servlet.config.annotation; import java.util.HashMap; @@ -32,9 +33,6 @@ * Creates a {@code ContentNegotiationManager} and configures it with * one or more {@link ContentNegotiationStrategy} instances. * - *

As of 5.0 you can set the exact strategies to use via - * {@link #strategies(List)}. - * *

As an alternative you can also rely on the set of defaults described below * which can be turned on or off or customized through the methods of this * builder: @@ -86,12 +84,14 @@ * of JAF. * * @author Rossen Stoyanchev + * @author Brian Clozel + * @author Juergen Hoeller * @since 3.2 + * @see ContentNegotiationManagerFactoryBean */ public class ContentNegotiationConfigurer { - private final ContentNegotiationManagerFactoryBean factory = - new ContentNegotiationManagerFactoryBean(); + private final ContentNegotiationManagerFactoryBean factory = new ContentNegotiationManagerFactoryBean(); private final Map mediaTypes = new HashMap(); @@ -226,18 +226,32 @@ public ContentNegotiationConfigurer defaultContentType(MediaType defaultContentT * Set a custom {@link ContentNegotiationStrategy} to use to determine * the content type to use when no content type is requested. *

By default this is not set. - * @see #defaultContentType * @since 4.1.2 + * @see #defaultContentType */ public ContentNegotiationConfigurer defaultContentTypeStrategy(ContentNegotiationStrategy defaultStrategy) { this.factory.setDefaultContentTypeStrategy(defaultStrategy); return this; } - protected ContentNegotiationManager getContentNegotiationManager() throws Exception { + + /** + * Build a {@link ContentNegotiationManager} based on this configurer's settings. + * @since 4.3.12 + * @see ContentNegotiationManagerFactoryBean#getObject() + */ + protected ContentNegotiationManager buildContentNegotiationManager() { this.factory.addMediaTypes(this.mediaTypes); this.factory.afterPropertiesSet(); return this.factory.getObject(); } + /** + * @deprecated as of 4.3.12, in favor of {@link #buildContentNegotiationManager()} + */ + @Deprecated + protected ContentNegotiationManager getContentNegotiationManager() throws Exception { + return buildContentNegotiationManager(); + } + } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/DefaultServletHandlerConfigurer.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/DefaultServletHandlerConfigurer.java index bfc70a2aab03..5197843053ba 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/DefaultServletHandlerConfigurer.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/DefaultServletHandlerConfigurer.java @@ -16,12 +16,10 @@ package org.springframework.web.servlet.config.annotation; -import java.util.HashMap; -import java.util.Map; +import java.util.Collections; import javax.servlet.ServletContext; import org.springframework.util.Assert; -import org.springframework.web.HttpRequestHandler; import org.springframework.web.servlet.DispatcherServlet; import org.springframework.web.servlet.handler.AbstractHandlerMapping; import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping; @@ -82,23 +80,30 @@ public void enable(String defaultServletName) { this.handler.setServletContext(this.servletContext); } + /** * Return a handler mapping instance ordered at {@link Integer#MAX_VALUE} containing the * {@link DefaultServletHttpRequestHandler} instance mapped to {@code "/**"}; * or {@code null} if default servlet handling was not been enabled. + * @since 4.3.12 */ - protected AbstractHandlerMapping getHandlerMapping() { + protected SimpleUrlHandlerMapping buildHandlerMapping() { if (this.handler == null) { return null; } - Map urlMap = new HashMap(); - urlMap.put("/**", this.handler); - SimpleUrlHandlerMapping handlerMapping = new SimpleUrlHandlerMapping(); + handlerMapping.setUrlMap(Collections.singletonMap("/**", this.handler)); handlerMapping.setOrder(Integer.MAX_VALUE); - handlerMapping.setUrlMap(urlMap); return handlerMapping; } + /** + * @deprecated as of 4.3.12, in favor of {@link #buildHandlerMapping()} + */ + @Deprecated + protected AbstractHandlerMapping getHandlerMapping() { + return buildHandlerMapping(); + } + } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/PathMatchConfigurer.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/PathMatchConfigurer.java index 35fa13e31720..72cc44c343c5 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/PathMatchConfigurer.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/PathMatchConfigurer.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2017 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. @@ -78,9 +78,7 @@ public PathMatchConfigurer setUseTrailingSlashMatch(Boolean trailingSlashMatch) *

By default this is set to "false". * @see WebMvcConfigurer#configureContentNegotiation */ - public PathMatchConfigurer setUseRegisteredSuffixPatternMatch( - Boolean registeredSuffixPatternMatch) { - + public PathMatchConfigurer setUseRegisteredSuffixPatternMatch(Boolean registeredSuffixPatternMatch) { this.registeredSuffixPatternMatch = registeredSuffixPatternMatch; return this; } @@ -106,6 +104,7 @@ public PathMatchConfigurer setPathMatcher(PathMatcher pathMatcher) { return this; } + public Boolean isUseSuffixPatternMatch() { return this.suffixPatternMatch; } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ViewControllerRegistry.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ViewControllerRegistry.java index bcc0a10cf0f7..61f60e6bbc10 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ViewControllerRegistry.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ViewControllerRegistry.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2017 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. @@ -36,6 +36,8 @@ */ public class ViewControllerRegistry { + private ApplicationContext applicationContext; + private final List registrations = new ArrayList(4); private final List redirectRegistrations = @@ -43,7 +45,18 @@ public class ViewControllerRegistry { private int order = 1; - private ApplicationContext applicationContext; + + /** + * Class constructor with {@link ApplicationContext}. + * @since 4.3.12 + */ + public ViewControllerRegistry(ApplicationContext applicationContext) { + this.applicationContext = applicationContext; + } + + @Deprecated + public ViewControllerRegistry() { + } /** @@ -96,19 +109,17 @@ public void setOrder(int order) { this.order = order; } - protected void setApplicationContext(ApplicationContext applicationContext) { - this.applicationContext = applicationContext; - } - /** * Return the {@code HandlerMapping} that contains the registered view * controller mappings, or {@code null} for no registrations. + * @since 4.3.12 */ - protected AbstractHandlerMapping getHandlerMapping() { + protected SimpleUrlHandlerMapping buildHandlerMapping() { if (this.registrations.isEmpty() && this.redirectRegistrations.isEmpty()) { return null; } + Map urlMap = new LinkedHashMap(); for (ViewControllerRegistration registration : this.registrations) { urlMap.put(registration.getUrlPath(), registration.getViewController()); @@ -116,10 +127,24 @@ protected AbstractHandlerMapping getHandlerMapping() { for (RedirectViewControllerRegistration registration : this.redirectRegistrations) { urlMap.put(registration.getUrlPath(), registration.getViewController()); } + SimpleUrlHandlerMapping handlerMapping = new SimpleUrlHandlerMapping(); - handlerMapping.setOrder(this.order); handlerMapping.setUrlMap(urlMap); + handlerMapping.setOrder(this.order); return handlerMapping; } + /** + * @deprecated as of 4.3.12, in favor of {@link #buildHandlerMapping()} + */ + @Deprecated + protected AbstractHandlerMapping getHandlerMapping() { + return buildHandlerMapping(); + } + + @Deprecated + protected void setApplicationContext(ApplicationContext applicationContext) { + this.applicationContext = applicationContext; + } + } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ViewResolverRegistry.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ViewResolverRegistry.java index 79a703330550..e3ad0de3847d 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ViewResolverRegistry.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ViewResolverRegistry.java @@ -53,25 +53,31 @@ */ public class ViewResolverRegistry { + private ContentNegotiationManager contentNegotiationManager; + + private ApplicationContext applicationContext; + private ContentNegotiatingViewResolver contentNegotiatingResolver; private final List viewResolvers = new ArrayList(4); private Integer order; - private ContentNegotiationManager contentNegotiationManager; - private ApplicationContext applicationContext; - - - protected void setContentNegotiationManager(ContentNegotiationManager contentNegotiationManager) { + /** + * Class constructor with {@link ContentNegotiationManager} and {@link ApplicationContext}. + * @since 4.3.12 + */ + public ViewResolverRegistry(ContentNegotiationManager contentNegotiationManager, ApplicationContext context) { this.contentNegotiationManager = contentNegotiationManager; + this.applicationContext = context; } - protected void setApplicationContext(ApplicationContext applicationContext) { - this.applicationContext = applicationContext; + @Deprecated + public ViewResolverRegistry() { } + /** * Whether any view resolvers have been registered. */ @@ -79,7 +85,6 @@ public boolean hasRegistrations() { return (this.contentNegotiatingResolver != null || !this.viewResolvers.isEmpty()); } - /** * Enable use of a {@link ContentNegotiatingViewResolver} to front all other * configured view resolvers and select among all selected Views based on @@ -161,7 +166,7 @@ public UrlBasedViewResolverRegistration jsp(String prefix, String suffix) { * {@link org.springframework.web.servlet.view.tiles3.TilesConfigurer} bean. */ public UrlBasedViewResolverRegistration tiles() { - if (this.applicationContext != null && !hasBeanOfType(TilesConfigurer.class)) { + if (!checkBeanOfType(TilesConfigurer.class)) { throw new BeanInitializationException("In addition to a Tiles view resolver " + "there must also be a single TilesConfigurer bean in this web application context " + "(or its parent)."); @@ -178,7 +183,7 @@ public UrlBasedViewResolverRegistration tiles() { * {@link org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer} bean. */ public UrlBasedViewResolverRegistration freeMarker() { - if (this.applicationContext != null && !hasBeanOfType(FreeMarkerConfigurer.class)) { + if (!checkBeanOfType(FreeMarkerConfigurer.class)) { throw new BeanInitializationException("In addition to a FreeMarker view resolver " + "there must also be a single FreeMarkerConfig bean in this web application context " + "(or its parent): FreeMarkerConfigurer is the usual implementation. " + @@ -198,7 +203,7 @@ public UrlBasedViewResolverRegistration freeMarker() { */ @Deprecated public UrlBasedViewResolverRegistration velocity() { - if (this.applicationContext != null && !hasBeanOfType(org.springframework.web.servlet.view.velocity.VelocityConfigurer.class)) { + if (!checkBeanOfType(org.springframework.web.servlet.view.velocity.VelocityConfigurer.class)) { throw new BeanInitializationException("In addition to a Velocity view resolver " + "there must also be a single VelocityConfig bean in this web application context " + "(or its parent): VelocityConfigurer is the usual implementation. " + @@ -214,7 +219,7 @@ public UrlBasedViewResolverRegistration velocity() { * prefix and a default suffix of ".tpl". */ public UrlBasedViewResolverRegistration groovy() { - if (this.applicationContext != null && !hasBeanOfType(GroovyMarkupConfigurer.class)) { + if (!checkBeanOfType(GroovyMarkupConfigurer.class)) { throw new BeanInitializationException("In addition to a Groovy markup view resolver " + "there must also be a single GroovyMarkupConfig bean in this web application context " + "(or its parent): GroovyMarkupConfigurer is the usual implementation. " + @@ -230,7 +235,7 @@ public UrlBasedViewResolverRegistration groovy() { * @since 4.2 */ public UrlBasedViewResolverRegistration scriptTemplate() { - if (this.applicationContext != null && !hasBeanOfType(ScriptTemplateConfigurer.class)) { + if (!checkBeanOfType(ScriptTemplateConfigurer.class)) { throw new BeanInitializationException("In addition to a script template view resolver " + "there must also be a single ScriptTemplateConfig bean in this web application context " + "(or its parent): ScriptTemplateConfigurer is the usual implementation. " + @@ -281,11 +286,6 @@ public void order(int order) { this.order = order; } - protected boolean hasBeanOfType(Class beanType) { - return !ObjectUtils.isEmpty(BeanFactoryUtils.beanNamesForTypeIncludingAncestors( - this.applicationContext, beanType, false, false)); - } - protected int getOrder() { return (this.order != null ? this.order : Ordered.LOWEST_PRECEDENCE); @@ -300,6 +300,28 @@ protected List getViewResolvers() { } } + private boolean checkBeanOfType(Class beanType) { + return (this.applicationContext == null || + !ObjectUtils.isEmpty(BeanFactoryUtils.beanNamesForTypeIncludingAncestors( + this.applicationContext, beanType, false, false))); + } + + @Deprecated + protected boolean hasBeanOfType(Class beanType) { + return !ObjectUtils.isEmpty(BeanFactoryUtils.beanNamesForTypeIncludingAncestors( + this.applicationContext, beanType, false, false)); + } + + @Deprecated + protected void setContentNegotiationManager(ContentNegotiationManager contentNegotiationManager) { + this.contentNegotiationManager = contentNegotiationManager; + } + + @Deprecated + protected void setApplicationContext(ApplicationContext applicationContext) { + this.applicationContext = applicationContext; + } + private static class TilesRegistration extends UrlBasedViewResolverRegistration { @@ -308,6 +330,7 @@ public TilesRegistration() { } } + private static class VelocityRegistration extends UrlBasedViewResolverRegistration { @SuppressWarnings("deprecation") @@ -317,6 +340,7 @@ public VelocityRegistration() { } } + private static class FreeMarkerRegistration extends UrlBasedViewResolverRegistration { public FreeMarkerRegistration() { @@ -325,6 +349,7 @@ public FreeMarkerRegistration() { } } + private static class GroovyMarkupRegistration extends UrlBasedViewResolverRegistration { public GroovyMarkupRegistration() { @@ -333,6 +358,7 @@ public GroovyMarkupRegistration() { } } + private static class ScriptRegistration extends UrlBasedViewResolverRegistration { public ScriptRegistration() { diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.java index 6d4ef78474e3..dd3c2dcffbe9 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.java @@ -246,32 +246,32 @@ public ServletContext getServletContext() { */ @Bean public RequestMappingHandlerMapping requestMappingHandlerMapping() { - RequestMappingHandlerMapping handlerMapping = createRequestMappingHandlerMapping(); - handlerMapping.setOrder(0); - handlerMapping.setInterceptors(getInterceptors()); - handlerMapping.setContentNegotiationManager(mvcContentNegotiationManager()); - handlerMapping.setCorsConfigurations(getCorsConfigurations()); + RequestMappingHandlerMapping mapping = createRequestMappingHandlerMapping(); + mapping.setOrder(0); + mapping.setInterceptors(getInterceptors()); + mapping.setContentNegotiationManager(mvcContentNegotiationManager()); + mapping.setCorsConfigurations(getCorsConfigurations()); PathMatchConfigurer configurer = getPathMatchConfigurer(); if (configurer.isUseSuffixPatternMatch() != null) { - handlerMapping.setUseSuffixPatternMatch(configurer.isUseSuffixPatternMatch()); + mapping.setUseSuffixPatternMatch(configurer.isUseSuffixPatternMatch()); } if (configurer.isUseRegisteredSuffixPatternMatch() != null) { - handlerMapping.setUseRegisteredSuffixPatternMatch(configurer.isUseRegisteredSuffixPatternMatch()); + mapping.setUseRegisteredSuffixPatternMatch(configurer.isUseRegisteredSuffixPatternMatch()); } if (configurer.isUseTrailingSlashMatch() != null) { - handlerMapping.setUseTrailingSlashMatch(configurer.isUseTrailingSlashMatch()); + mapping.setUseTrailingSlashMatch(configurer.isUseTrailingSlashMatch()); } UrlPathHelper pathHelper = configurer.getUrlPathHelper(); if (pathHelper != null) { - handlerMapping.setUrlPathHelper(pathHelper); + mapping.setUrlPathHelper(pathHelper); } PathMatcher pathMatcher = configurer.getPathMatcher(); if (pathMatcher != null) { - handlerMapping.setPathMatcher(pathMatcher); + mapping.setPathMatcher(pathMatcher); } - return handlerMapping; + return mapping; } /** @@ -364,12 +364,7 @@ public ContentNegotiationManager mvcContentNegotiationManager() { ContentNegotiationConfigurer configurer = new ContentNegotiationConfigurer(this.servletContext); configurer.mediaTypes(getDefaultMediaTypes()); configureContentNegotiation(configurer); - try { - this.contentNegotiationManager = configurer.getContentNegotiationManager(); - } - catch (Throwable ex) { - throw new BeanInitializationException("Could not create ContentNegotiationManager", ex); - } + this.contentNegotiationManager = configurer.buildContentNegotiationManager(); } return this.contentNegotiationManager; } @@ -403,11 +398,10 @@ protected void configureContentNegotiation(ContentNegotiationConfigurer configur */ @Bean public HandlerMapping viewControllerHandlerMapping() { - ViewControllerRegistry registry = new ViewControllerRegistry(); - registry.setApplicationContext(this.applicationContext); + ViewControllerRegistry registry = new ViewControllerRegistry(this.applicationContext); addViewControllers(registry); - AbstractHandlerMapping handlerMapping = registry.getHandlerMapping(); + AbstractHandlerMapping handlerMapping = registry.buildHandlerMapping(); handlerMapping = (handlerMapping != null ? handlerMapping : new EmptyHandlerMapping()); handlerMapping.setPathMatcher(mvcPathMatcher()); handlerMapping.setUrlPathHelper(mvcUrlPathHelper()); @@ -492,11 +486,11 @@ public ResourceUrlProvider mvcResourceUrlProvider() { */ @Bean public HandlerMapping defaultServletHandlerMapping() { - DefaultServletHandlerConfigurer configurer = new DefaultServletHandlerConfigurer(servletContext); + DefaultServletHandlerConfigurer configurer = new DefaultServletHandlerConfigurer(this.servletContext); configureDefaultServletHandling(configurer); - AbstractHandlerMapping handlerMapping = configurer.getHandlerMapping(); - handlerMapping = handlerMapping != null ? handlerMapping : new EmptyHandlerMapping(); - return handlerMapping; + + HandlerMapping handlerMapping = configurer.buildHandlerMapping(); + return (handlerMapping != null ? handlerMapping : new EmptyHandlerMapping()); } /** @@ -911,9 +905,8 @@ protected ExceptionHandlerExceptionResolver createExceptionHandlerExceptionResol */ @Bean public ViewResolver mvcViewResolver() { - ViewResolverRegistry registry = new ViewResolverRegistry(); - registry.setContentNegotiationManager(mvcContentNegotiationManager()); - registry.setApplicationContext(this.applicationContext); + ViewResolverRegistry registry = new ViewResolverRegistry( + mvcContentNegotiationManager(), this.applicationContext); configureViewResolvers(registry); if (registry.getViewResolvers().isEmpty()) { diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/ContentNegotiationConfigurerTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/ContentNegotiationConfigurerTests.java index 902c09af894f..d2a5e5443506 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/ContentNegotiationConfigurerTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/ContentNegotiationConfigurerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2017 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. @@ -42,6 +42,7 @@ public class ContentNegotiationConfigurerTests { private MockHttpServletRequest servletRequest; + @Before public void setup() { this.servletRequest = new MockHttpServletRequest(); @@ -49,9 +50,10 @@ public void setup() { this.configurer = new ContentNegotiationConfigurer(this.servletRequest.getServletContext()); } + @Test public void defaultSettings() throws Exception { - ContentNegotiationManager manager = this.configurer.getContentNegotiationManager(); + ContentNegotiationManager manager = this.configurer.buildContentNegotiationManager(); this.servletRequest.setRequestURI("/flower.gif"); @@ -74,7 +76,7 @@ public void defaultSettings() throws Exception { @Test public void addMediaTypes() throws Exception { this.configurer.mediaTypes(Collections.singletonMap("json", MediaType.APPLICATION_JSON)); - ContentNegotiationManager manager = this.configurer.getContentNegotiationManager(); + ContentNegotiationManager manager = this.configurer.buildContentNegotiationManager(); this.servletRequest.setRequestURI("/flower.json"); assertEquals(Arrays.asList(MediaType.APPLICATION_JSON), manager.resolveMediaTypes(this.webRequest)); @@ -85,7 +87,7 @@ public void favorParameter() throws Exception { this.configurer.favorParameter(true); this.configurer.parameterName("f"); this.configurer.mediaTypes(Collections.singletonMap("json", MediaType.APPLICATION_JSON)); - ContentNegotiationManager manager = this.configurer.getContentNegotiationManager(); + ContentNegotiationManager manager = this.configurer.buildContentNegotiationManager(); this.servletRequest.setRequestURI("/flower"); this.servletRequest.addParameter("f", "json"); @@ -96,7 +98,7 @@ public void favorParameter() throws Exception { @Test public void ignoreAcceptHeader() throws Exception { this.configurer.ignoreAcceptHeader(true); - ContentNegotiationManager manager = this.configurer.getContentNegotiationManager(); + ContentNegotiationManager manager = this.configurer.buildContentNegotiationManager(); this.servletRequest.setRequestURI("/flower"); this.servletRequest.addHeader("Accept", MediaType.IMAGE_GIF_VALUE); @@ -107,7 +109,7 @@ public void ignoreAcceptHeader() throws Exception { @Test public void setDefaultContentType() throws Exception { this.configurer.defaultContentType(MediaType.APPLICATION_JSON); - ContentNegotiationManager manager = this.configurer.getContentNegotiationManager(); + ContentNegotiationManager manager = this.configurer.buildContentNegotiationManager(); assertEquals(Arrays.asList(MediaType.APPLICATION_JSON), manager.resolveMediaTypes(this.webRequest)); } @@ -115,8 +117,9 @@ public void setDefaultContentType() throws Exception { @Test public void setDefaultContentTypeStrategy() throws Exception { this.configurer.defaultContentTypeStrategy(new FixedContentNegotiationStrategy(MediaType.APPLICATION_JSON)); - ContentNegotiationManager manager = this.configurer.getContentNegotiationManager(); + ContentNegotiationManager manager = this.configurer.buildContentNegotiationManager(); assertEquals(Arrays.asList(MediaType.APPLICATION_JSON), manager.resolveMediaTypes(this.webRequest)); } + } diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/DefaultServletHandlerConfigurerTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/DefaultServletHandlerConfigurerTests.java index e9c45776d5e4..c50dd0a6c74f 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/DefaultServletHandlerConfigurerTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/DefaultServletHandlerConfigurerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2017 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. @@ -43,23 +43,25 @@ public class DefaultServletHandlerConfigurerTests { private MockHttpServletResponse response; + @Before - public void setUp() { + public void setup() { response = new MockHttpServletResponse(); servletContext = new DispatchingMockServletContext(); configurer = new DefaultServletHandlerConfigurer(servletContext); } + @Test public void notEnabled() { - assertNull(configurer.getHandlerMapping()); + assertNull(configurer.buildHandlerMapping()); } @Test public void enable() throws Exception { configurer.enable(); - SimpleUrlHandlerMapping getHandlerMapping = getHandlerMapping(); - SimpleUrlHandlerMapping handlerMapping = getHandlerMapping; + SimpleUrlHandlerMapping getHandlerMapping = configurer.buildHandlerMapping(); + SimpleUrlHandlerMapping handlerMapping = configurer.buildHandlerMapping(); DefaultServletHttpRequestHandler handler = (DefaultServletHttpRequestHandler) handlerMapping.getUrlMap().get("/**"); assertNotNull(handler); @@ -75,7 +77,7 @@ public void enable() throws Exception { @Test public void enableWithServletName() throws Exception { configurer.enable("defaultServlet"); - SimpleUrlHandlerMapping handlerMapping = getHandlerMapping(); + SimpleUrlHandlerMapping handlerMapping = configurer.buildHandlerMapping(); DefaultServletHttpRequestHandler handler = (DefaultServletHttpRequestHandler) handlerMapping.getUrlMap().get("/**"); assertNotNull(handler); @@ -88,6 +90,7 @@ public void enableWithServletName() throws Exception { assertEquals("The request was not forwarded", expected, response.getForwardedUrl()); } + private static class DispatchingMockServletContext extends MockServletContext { private String url; @@ -99,8 +102,4 @@ public RequestDispatcher getNamedDispatcher(String url) { } } - private SimpleUrlHandlerMapping getHandlerMapping() { - return (SimpleUrlHandlerMapping) configurer.getHandlerMapping(); - } - }