Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Spring boot interceptor WebMvcConfigurerAdapter, and a higher version of the replacement scheme

Shulou Source: shulou.com Published: 2022-06-03 06:19:31 09月30日 Update

The recent project uses spring icloud, and the spring boot version is 1.5.x. WebMvcConfigurerAdapter will be cancelled after spring boot 2.0 and Spring 5.0. The following is a general introduction, I hope it will be helpful to all of you.

The following WebMvcConfigurerAdapter commonly used rewrite interfaces / * * solve cross-domain problems * * / public void addCorsMappings (CorsRegistry registry); / * * add interceptors * * / void addInterceptors (InterceptorRegistry registry); / * * configure view parsers * * / void configureViewResolvers (ViewResolverRegistry registry); / * * configure some options for content adjudication * * / void configureContentNegotiation (ContentNegotiationConfigurer configurer); / * * View Jump Controller * * / void addViewControllers (ViewControllerRegistry registry) / * * static resource processing * * / void addResourceHandlers (ResourceHandlerRegistry registry); / * default static resource processor * * / void configureDefaultServletHandling (DefaultServletHandlerConfigurer configurer); there are currently two new versions of solutions

Scenario 1 directly implements WebMvcConfigurer

@ Configurationpublic class WebMvcConfg implements WebMvcConfigurer {@ Override public void addViewControllers (ViewControllerRegistry registry) {registry.addViewController ("/ index") .setViewName ("index");}}

Scenario 2 inherits WebMvcConfigurationSupport directly

@ Configurationpublic class WebMvcConfg extends WebMvcConfigurationSupport {@ Override public void addViewControllers (ViewControllerRegistry registry) {registry.addViewController ("/ index") .setViewName ("index");}}

In fact, WebMvcConfigurerAdapter under the source code implements the WebMvcConfigurer interface, so it is possible to implement the WebMvcConfigurer interface directly. WebMvcConfigurationSupport is in the same directory as WebMvcConfigurerAdapter and interface WebMvcConfigurer. WebMvcConfigurationSupport contains the methods in WebMvcConfigurer, so it seems that the WebMvcConfigurationSupport class should be recommended in the version, and WebMvcConfigurationSupport should be the replacement and extension of WebMvcConfigurerAdapter in the new version. [personal opinion, if there is any mistake, please help correct]

Tags: Interface scheme version content view resource static processing configuration interceptor individual processor common use controller method source code from this point of view directory opinion Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux MariaDB Huawei Docker Xiaomi