Replace WebMvcConfigurerAdapter with default methods
Issue: SPR-15465
This commit is contained in:
@@ -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.
|
||||
@@ -38,14 +38,13 @@ import org.springframework.context.annotation.Import;
|
||||
* </pre>
|
||||
*
|
||||
* <p>To customize the imported configuration, implement the interface
|
||||
* {@link WebMvcConfigurer} or more likely extend the empty method base class
|
||||
* {@link WebMvcConfigurerAdapter} and override individual methods, e.g.:
|
||||
* {@link WebMvcConfigurer} and override individual methods, e.g.:
|
||||
*
|
||||
* <pre class="code">
|
||||
* @Configuration
|
||||
* @EnableWebMvc
|
||||
* @ComponentScan(basePackageClasses = MyConfiguration.class)
|
||||
* public class MyConfiguration extends WebMvcConfigurerAdapter {
|
||||
* public class MyConfiguration implements WebMvcConfigurer {
|
||||
*
|
||||
* @Override
|
||||
* public void addFormatters(FormatterRegistry formatterRegistry) {
|
||||
@@ -93,7 +92,6 @@ import org.springframework.context.annotation.Import;
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 3.1
|
||||
* @see org.springframework.web.servlet.config.annotation.WebMvcConfigurer
|
||||
* @see org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter
|
||||
* @see org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport
|
||||
* @see org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration
|
||||
*/
|
||||
|
||||
@@ -168,7 +168,6 @@ import org.springframework.web.util.UrlPathHelper;
|
||||
* @since 3.1
|
||||
* @see EnableWebMvc
|
||||
* @see WebMvcConfigurer
|
||||
* @see WebMvcConfigurerAdapter
|
||||
*/
|
||||
public class WebMvcConfigurationSupport implements ApplicationContextAware, ServletContextAware {
|
||||
|
||||
|
||||
@@ -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.
|
||||
@@ -36,8 +36,7 @@ import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandl
|
||||
*
|
||||
* <p>{@code @EnableWebMvc}-annotated configuration classes may implement
|
||||
* this interface to be called back and given a chance to customize the
|
||||
* default configuration. Consider extending {@link WebMvcConfigurerAdapter},
|
||||
* which provides a stub implementation of all interface methods.
|
||||
* default configuration.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @author Keith Donald
|
||||
@@ -57,17 +56,20 @@ public interface WebMvcConfigurer {
|
||||
* </ul>
|
||||
* @since 4.0.3
|
||||
*/
|
||||
void configurePathMatch(PathMatchConfigurer configurer);
|
||||
default void configurePathMatch(PathMatchConfigurer configurer) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure content negotiation options.
|
||||
*/
|
||||
void configureContentNegotiation(ContentNegotiationConfigurer configurer);
|
||||
default void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure asynchronous request handling options.
|
||||
*/
|
||||
void configureAsyncSupport(AsyncSupportConfigurer configurer);
|
||||
default void configureAsyncSupport(AsyncSupportConfigurer configurer) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure a handler to delegate unhandled requests by forwarding to the
|
||||
@@ -75,13 +77,15 @@ public interface WebMvcConfigurer {
|
||||
* the {@link DispatcherServlet} is mapped to "/" thus overriding the
|
||||
* Servlet container's default handling of static resources.
|
||||
*/
|
||||
void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer);
|
||||
default void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Add {@link Converter}s and {@link Formatter}s in addition to the ones
|
||||
* registered by default.
|
||||
*/
|
||||
void addFormatters(FormatterRegistry registry);
|
||||
default void addFormatters(FormatterRegistry registry) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Add Spring MVC lifecycle interceptors for pre- and post-processing of
|
||||
@@ -95,20 +99,23 @@ public interface WebMvcConfigurer {
|
||||
* {@link org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport
|
||||
* WebMvcConfigurationSupport} and then override {@code resourceHandlerMapping}.
|
||||
*/
|
||||
void addInterceptors(InterceptorRegistry registry);
|
||||
default void addInterceptors(InterceptorRegistry registry) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Add handlers to serve static resources such as images, js, and, css
|
||||
* files from specific locations under web application root, the classpath,
|
||||
* and others.
|
||||
*/
|
||||
void addResourceHandlers(ResourceHandlerRegistry registry);
|
||||
default void addResourceHandlers(ResourceHandlerRegistry registry) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure cross origin requests processing.
|
||||
* @since 4.2
|
||||
*/
|
||||
void addCorsMappings(CorsRegistry registry);
|
||||
default void addCorsMappings(CorsRegistry registry) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure simple automated controllers pre-configured with the response
|
||||
@@ -117,7 +124,8 @@ public interface WebMvcConfigurer {
|
||||
* home page, perform simple site URL redirects, return a 404 status with
|
||||
* HTML content, a 204 with no content, and more.
|
||||
*/
|
||||
void addViewControllers(ViewControllerRegistry registry);
|
||||
default void addViewControllers(ViewControllerRegistry registry) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure view resolvers to translate String-based view names returned from
|
||||
@@ -125,25 +133,28 @@ public interface WebMvcConfigurer {
|
||||
* implementations to perform rendering with.
|
||||
* @since 4.1
|
||||
*/
|
||||
void configureViewResolvers(ViewResolverRegistry registry);
|
||||
default void configureViewResolvers(ViewResolverRegistry registry) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Add resolvers to support custom controller method argument types.
|
||||
* <p>This does not override the built-in support for resolving handler
|
||||
* method arguments. To customize the built-in support for argument
|
||||
* resolution, configure {@link RequestMappingHandlerAdapter} directly.
|
||||
* @param argumentResolvers initially an empty list
|
||||
* @param resolvers initially an empty list
|
||||
*/
|
||||
void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers);
|
||||
default void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Add handlers to support custom controller method return value types.
|
||||
* <p>Using this option does not override the built-in support for handling
|
||||
* return values. To customize the built-in support for handling return
|
||||
* values, configure RequestMappingHandlerAdapter directly.
|
||||
* @param returnValueHandlers initially an empty list
|
||||
* @param handlers initially an empty list
|
||||
*/
|
||||
void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> returnValueHandlers);
|
||||
default void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> handlers) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure the {@link HttpMessageConverter}s to use for reading or writing
|
||||
@@ -155,7 +166,8 @@ public interface WebMvcConfigurer {
|
||||
* {@link #extendMessageConverters(java.util.List)} instead.
|
||||
* @param converters initially an empty list of converters
|
||||
*/
|
||||
void configureMessageConverters(List<HttpMessageConverter<?>> converters);
|
||||
default void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
|
||||
}
|
||||
|
||||
/**
|
||||
* A hook for extending or modifying the list of converters after it has been
|
||||
@@ -164,7 +176,8 @@ public interface WebMvcConfigurer {
|
||||
* @param converters the list of configured converters to extend.
|
||||
* @since 4.1.3
|
||||
*/
|
||||
void extendMessageConverters(List<HttpMessageConverter<?>> converters);
|
||||
default void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure exception resolvers.
|
||||
@@ -177,21 +190,23 @@ public interface WebMvcConfigurer {
|
||||
* <p>Alternatively you can use
|
||||
* {@link #extendHandlerExceptionResolvers(List)} which allows you to extend
|
||||
* or modify the list of exception resolvers configured by default.
|
||||
* @param exceptionResolvers initially an empty list
|
||||
* @param resolvers initially an empty list
|
||||
* @see #extendHandlerExceptionResolvers(List)
|
||||
* @see WebMvcConfigurationSupport#addDefaultHandlerExceptionResolvers(List)
|
||||
*/
|
||||
void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers);
|
||||
default void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> resolvers) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Extending or modify the list of exception resolvers configured by default.
|
||||
* This can be useful for inserting a custom exception resolver without
|
||||
* interfering with default ones.
|
||||
* @param exceptionResolvers the list of configured resolvers to extend
|
||||
* @param resolvers the list of configured resolvers to extend
|
||||
* @since 4.3
|
||||
* @see WebMvcConfigurationSupport#addDefaultHandlerExceptionResolvers(List)
|
||||
*/
|
||||
void extendHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers);
|
||||
default void extendHandlerExceptionResolvers(List<HandlerExceptionResolver> resolvers) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Provide a custom {@link Validator} instead of the one created by default.
|
||||
@@ -199,13 +214,17 @@ public interface WebMvcConfigurer {
|
||||
* {@link org.springframework.validation.beanvalidation.OptionalValidatorFactoryBean}.
|
||||
* Leave the return value as {@code null} to keep the default.
|
||||
*/
|
||||
Validator getValidator();
|
||||
default Validator getValidator() {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Provide a custom {@link MessageCodesResolver} for building message codes
|
||||
* from data binding and validation error codes. Leave the return value as
|
||||
* {@code null} to keep the default.
|
||||
*/
|
||||
MessageCodesResolver getMessageCodesResolver();
|
||||
default MessageCodesResolver getMessageCodesResolver() {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -32,7 +32,11 @@ import org.springframework.web.servlet.HandlerExceptionResolver;
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 3.1
|
||||
* @deprecated as of 5.0 {@link WebMvcConfigurer} has default methods (made
|
||||
* possible by a Java 8 baseline) and can be implemented directly without the
|
||||
* need for this adapter
|
||||
*/
|
||||
@Deprecated
|
||||
public abstract class WebMvcConfigurerAdapter implements WebMvcConfigurer {
|
||||
|
||||
/**
|
||||
|
||||
@@ -55,7 +55,7 @@ import org.springframework.util.StringUtils;
|
||||
* <p>In order to serve manifest files with the proper {@code "text/manifest"} content type,
|
||||
* it is required to configure it with
|
||||
* {@code contentNegotiationConfigurer.mediaType("appcache", MediaType.valueOf("text/manifest")}
|
||||
* in a {@code WebMvcConfigurerAdapter}.
|
||||
* in a {@code WebMvcConfigurer}.
|
||||
*
|
||||
* @author Brian Clozel
|
||||
* @since 4.1
|
||||
|
||||
@@ -46,8 +46,13 @@ import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandl
|
||||
import org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver;
|
||||
import org.springframework.web.util.UrlPathHelper;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.BDDMockito.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.BDDMockito.mock;
|
||||
import static org.mockito.BDDMockito.verify;
|
||||
|
||||
/**
|
||||
* A test fixture for {@link DelegatingWebMvcConfiguration} tests.
|
||||
@@ -118,7 +123,7 @@ public class DelegatingWebMvcConfigurationTests {
|
||||
final HttpMessageConverter customConverter = mock(HttpMessageConverter.class);
|
||||
final StringHttpMessageConverter stringConverter = new StringHttpMessageConverter();
|
||||
List<WebMvcConfigurer> configurers = new ArrayList<>();
|
||||
configurers.add(new WebMvcConfigurerAdapter() {
|
||||
configurers.add(new WebMvcConfigurer() {
|
||||
@Override
|
||||
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
|
||||
converters.add(stringConverter);
|
||||
@@ -176,7 +181,7 @@ public class DelegatingWebMvcConfigurationTests {
|
||||
@Test
|
||||
public void configureExceptionResolvers() throws Exception {
|
||||
List<WebMvcConfigurer> configurers = new ArrayList<>();
|
||||
configurers.add(new WebMvcConfigurerAdapter() {
|
||||
configurers.add(new WebMvcConfigurer() {
|
||||
@Override
|
||||
public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
|
||||
exceptionResolvers.add(new DefaultHandlerExceptionResolver());
|
||||
@@ -195,7 +200,7 @@ public class DelegatingWebMvcConfigurationTests {
|
||||
final UrlPathHelper pathHelper = mock(UrlPathHelper.class);
|
||||
|
||||
List<WebMvcConfigurer> configurers = new ArrayList<>();
|
||||
configurers.add(new WebMvcConfigurerAdapter() {
|
||||
configurers.add(new WebMvcConfigurer() {
|
||||
@Override
|
||||
public void configurePathMatch(PathMatchConfigurer configurer) {
|
||||
configurer.setUseRegisteredSuffixPatternMatch(true)
|
||||
|
||||
@@ -40,7 +40,7 @@ import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;
|
||||
import org.springframework.web.servlet.view.groovy.GroovyMarkupConfigurer;
|
||||
import org.springframework.web.servlet.view.tiles3.TilesConfigurer;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
* Integration tests for view resolution with {@code @EnableWebMvc}.
|
||||
@@ -128,7 +128,7 @@ public class ViewResolutionIntegrationTests {
|
||||
}
|
||||
|
||||
@EnableWebMvc
|
||||
static abstract class AbstractWebConfig extends WebMvcConfigurerAdapter {
|
||||
static abstract class AbstractWebConfig implements WebMvcConfigurer {
|
||||
|
||||
@Bean
|
||||
public SampleController sampleController() {
|
||||
|
||||
@@ -18,7 +18,6 @@ package org.springframework.web.servlet.config.annotation;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
@@ -363,7 +362,7 @@ public class WebMvcConfigurationSupportTests {
|
||||
|
||||
@EnableWebMvc
|
||||
@Configuration
|
||||
static class CustomViewResolverOrderConfig extends WebMvcConfigurerAdapter {
|
||||
static class CustomViewResolverOrderConfig implements WebMvcConfigurer {
|
||||
|
||||
@Override
|
||||
public void configureViewResolvers(ViewResolverRegistry registry) {
|
||||
@@ -374,7 +373,7 @@ public class WebMvcConfigurationSupportTests {
|
||||
|
||||
@EnableWebMvc
|
||||
@Configuration
|
||||
static class CustomArgumentResolverConfig extends WebMvcConfigurerAdapter {
|
||||
static class CustomArgumentResolverConfig implements WebMvcConfigurer {
|
||||
|
||||
@Override
|
||||
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
|
||||
|
||||
@@ -50,13 +50,21 @@ import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
|
||||
import org.springframework.web.servlet.DispatcherServlet;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
import org.springframework.web.util.UriComponents;
|
||||
import org.springframework.web.util.UriComponentsBuilder;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder.*;
|
||||
import static org.hamcrest.Matchers.contains;
|
||||
import static org.hamcrest.Matchers.containsInAnyOrder;
|
||||
import static org.hamcrest.Matchers.endsWith;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.hamcrest.Matchers.startsWith;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder.fromController;
|
||||
import static org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder.fromMethodCall;
|
||||
import static org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder.fromMethodName;
|
||||
import static org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder.on;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder}.
|
||||
@@ -500,7 +508,7 @@ public class MvcUriComponentsBuilderTests {
|
||||
|
||||
|
||||
@EnableWebMvc
|
||||
static class WebConfig extends WebMvcConfigurerAdapter {
|
||||
static class WebConfig implements WebMvcConfigurer {
|
||||
|
||||
@Bean
|
||||
public PersonsAddressesController controller() {
|
||||
|
||||
@@ -65,10 +65,10 @@ import org.springframework.web.multipart.commons.CommonsMultipartResolver;
|
||||
import org.springframework.web.multipart.support.StandardServletMultipartResolver;
|
||||
import org.springframework.web.servlet.DispatcherServlet;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.springframework.web.bind.annotation.RequestMethod.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.springframework.web.bind.annotation.RequestMethod.POST;
|
||||
|
||||
/**
|
||||
* Test access to parts of a multipart request with {@link RequestPart}.
|
||||
@@ -198,7 +198,7 @@ public class RequestPartIntegrationTests {
|
||||
|
||||
@Configuration
|
||||
@EnableWebMvc
|
||||
static class RequestPartTestConfig extends WebMvcConfigurerAdapter {
|
||||
static class RequestPartTestConfig implements WebMvcConfigurer {
|
||||
|
||||
@Bean
|
||||
public RequestPartTestController controller() {
|
||||
|
||||
Reference in New Issue
Block a user