001/*
002 * Copyright 2002-2019 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.web.servlet.config.annotation;
018
019import java.util.List;
020
021import org.springframework.core.convert.converter.Converter;
022import org.springframework.format.Formatter;
023import org.springframework.format.FormatterRegistry;
024import org.springframework.http.converter.HttpMessageConverter;
025import org.springframework.lang.Nullable;
026import org.springframework.validation.MessageCodesResolver;
027import org.springframework.validation.Validator;
028import org.springframework.web.method.support.HandlerMethodArgumentResolver;
029import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
030import org.springframework.web.servlet.DispatcherServlet;
031import org.springframework.web.servlet.HandlerExceptionResolver;
032import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
033
034/**
035 * Defines callback methods to customize the Java-based configuration for
036 * Spring MVC enabled via {@code @EnableWebMvc}.
037 *
038 * <p>{@code @EnableWebMvc}-annotated configuration classes may implement
039 * this interface to be called back and given a chance to customize the
040 * default configuration.
041 *
042 * @author Rossen Stoyanchev
043 * @author Keith Donald
044 * @author David Syer
045 * @since 3.1
046 */
047public interface WebMvcConfigurer {
048
049        /**
050         * Helps with configuring HandlerMappings path matching options such as trailing slash match,
051         * suffix registration, path matcher and path helper.
052         * Configured path matcher and path helper instances are shared for:
053         * <ul>
054         * <li>RequestMappings</li>
055         * <li>ViewControllerMappings</li>
056         * <li>ResourcesMappings</li>
057         * </ul>
058         * @since 4.0.3
059         */
060        default void configurePathMatch(PathMatchConfigurer configurer) {
061        }
062
063        /**
064         * Configure content negotiation options.
065         */
066        default void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
067        }
068
069        /**
070         * Configure asynchronous request handling options.
071         */
072        default void configureAsyncSupport(AsyncSupportConfigurer configurer) {
073        }
074
075        /**
076         * Configure a handler to delegate unhandled requests by forwarding to the
077         * Servlet container's "default" servlet. A common use case for this is when
078         * the {@link DispatcherServlet} is mapped to "/" thus overriding the
079         * Servlet container's default handling of static resources.
080         */
081        default void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
082        }
083
084        /**
085         * Add {@link Converter Converters} and {@link Formatter Formatters} in addition to the ones
086         * registered by default.
087         */
088        default void addFormatters(FormatterRegistry registry) {
089        }
090
091        /**
092         * Add Spring MVC lifecycle interceptors for pre- and post-processing of
093         * controller method invocations and resource handler requests.
094         * Interceptors can be registered to apply to all requests or be limited
095         * to a subset of URL patterns.
096         */
097        default void addInterceptors(InterceptorRegistry registry) {
098        }
099
100        /**
101         * Add handlers to serve static resources such as images, js, and, css
102         * files from specific locations under web application root, the classpath,
103         * and others.
104         */
105        default void addResourceHandlers(ResourceHandlerRegistry registry) {
106        }
107
108        /**
109         * Configure cross origin requests processing.
110         * @since 4.2
111         */
112        default void addCorsMappings(CorsRegistry registry) {
113        }
114
115        /**
116         * Configure simple automated controllers pre-configured with the response
117         * status code and/or a view to render the response body. This is useful in
118         * cases where there is no need for custom controller logic -- e.g. render a
119         * home page, perform simple site URL redirects, return a 404 status with
120         * HTML content, a 204 with no content, and more.
121         */
122        default void addViewControllers(ViewControllerRegistry registry) {
123        }
124
125        /**
126         * Configure view resolvers to translate String-based view names returned from
127         * controllers into concrete {@link org.springframework.web.servlet.View}
128         * implementations to perform rendering with.
129         * @since 4.1
130         */
131        default void configureViewResolvers(ViewResolverRegistry registry) {
132        }
133
134        /**
135         * Add resolvers to support custom controller method argument types.
136         * <p>This does not override the built-in support for resolving handler
137         * method arguments. To customize the built-in support for argument
138         * resolution, configure {@link RequestMappingHandlerAdapter} directly.
139         * @param resolvers initially an empty list
140         */
141        default void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
142        }
143
144        /**
145         * Add handlers to support custom controller method return value types.
146         * <p>Using this option does not override the built-in support for handling
147         * return values. To customize the built-in support for handling return
148         * values, configure RequestMappingHandlerAdapter directly.
149         * @param handlers initially an empty list
150         */
151        default void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> handlers) {
152        }
153
154        /**
155         * Configure the {@link HttpMessageConverter HttpMessageConverters} to use for reading or writing
156         * to the body of the request or response. If no converters are added, a
157         * default list of converters is registered.
158         * <p><strong>Note</strong> that adding converters to the list, turns off
159         * default converter registration. To simply add a converter without impacting
160         * default registration, consider using the method
161         * {@link #extendMessageConverters(java.util.List)} instead.
162         * @param converters initially an empty list of converters
163         */
164        default void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
165        }
166
167        /**
168         * A hook for extending or modifying the list of converters after it has been
169         * configured. This may be useful for example to allow default converters to
170         * be registered and then insert a custom converter through this method.
171         * @param converters the list of configured converters to extend.
172         * @since 4.1.3
173         */
174        default void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
175        }
176
177        /**
178         * Configure exception resolvers.
179         * <p>The given list starts out empty. If it is left empty, the framework
180         * configures a default set of resolvers, see
181         * {@link WebMvcConfigurationSupport#addDefaultHandlerExceptionResolvers(List, org.springframework.web.accept.ContentNegotiationManager)}.
182         * Or if any exception resolvers are added to the list, then the application
183         * effectively takes over and must provide, fully initialized, exception
184         * resolvers.
185         * <p>Alternatively you can use
186         * {@link #extendHandlerExceptionResolvers(List)} which allows you to extend
187         * or modify the list of exception resolvers configured by default.
188         * @param resolvers initially an empty list
189         * @see #extendHandlerExceptionResolvers(List)
190         * @see WebMvcConfigurationSupport#addDefaultHandlerExceptionResolvers(List, org.springframework.web.accept.ContentNegotiationManager)
191         */
192        default void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> resolvers) {
193        }
194
195        /**
196         * Extending or modify the list of exception resolvers configured by default.
197         * This can be useful for inserting a custom exception resolver without
198         * interfering with default ones.
199         * @param resolvers the list of configured resolvers to extend
200         * @since 4.3
201         * @see WebMvcConfigurationSupport#addDefaultHandlerExceptionResolvers(List, org.springframework.web.accept.ContentNegotiationManager)
202         */
203        default void extendHandlerExceptionResolvers(List<HandlerExceptionResolver> resolvers) {
204        }
205
206        /**
207         * Provide a custom {@link Validator} instead of the one created by default.
208         * The default implementation, assuming JSR-303 is on the classpath, is:
209         * {@link org.springframework.validation.beanvalidation.OptionalValidatorFactoryBean}.
210         * Leave the return value as {@code null} to keep the default.
211         */
212        @Nullable
213        default Validator getValidator() {
214                return null;
215        }
216
217        /**
218         * Provide a custom {@link MessageCodesResolver} for building message codes
219         * from data binding and validation error codes. Leave the return value as
220         * {@code null} to keep the default.
221         */
222        @Nullable
223        default MessageCodesResolver getMessageCodesResolver() {
224                return null;
225        }
226
227}