Detailed WebMvcConfigurerAdapter alternatives and after obsolete

First, what is WebMvcConfigurerAdapter

Spring interior of an arrangement is
in the form of JavaBean to replace the traditional form of xml configuration file framework for customization

Two, WebMvcConfigurerAdapter common method

/ Cross-domain problem solving ** * / 
public void addCorsMappings (CorsRegistry Registry); 

/ * Add an interceptor * / 
void addInterceptors (InterceptorRegistry Registry); 

/ ** view resolver configured here * / 
void configureViewResolvers (Registry ViewResolverRegistry ); 

some of the options / configuration of the award ** ** / 
void configureContentNegotiation (ContentNegotiationConfigurer Configurer); 

/ ** view Jump controller * / 
void addViewControllers (ViewControllerRegistry Registry); 

/ ** static resources to handle * / 
void addResourceHandlers (ResourceHandlerRegistry Registry); 

/ ** default static resource processor ** / 
void configureDefaultServletHandling (DefaultServletHandlerConfigurer Configurer);

1, addInterceptors: Interceptor

  • addInterceptor: a need to implement the interface interceptor instance HandlerInterceptor
  • addPathPatterns: interceptor provided for filtering path rule

excludePathPatterns: used to set filtering rules do not need to intercept

@Override
public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**").excludePathPatterns("/toLogin","/login");
        super.addInterceptors(registry);
}

2, addCorsMappings: cross-domain

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**").allowedOrigins("*").allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE").maxAge(3600).allowCredentials(true);
    }

 3, addViewControllers: Jump specified page

    / ** 
     * To access a page before you need to create a Controller class control, writing method to jump to the page 
     * here after configuration does not require so much trouble, direct access http: // localhost: 8080 / toLogin Jump login.html to the page 
     * 
     * @param Registry 
     * / 
    @Override 
    public void addViewControllers (ViewControllerRegistry Registry) { 
        registry.addViewController ( "/ toLogin") setViewName ( "Login");. 
        . registry.addViewController ( "/") setViewName ( "/ index"); 
        registry.addViewController ( "/ Login") setViewName ( "Forward: /index.html");. 
        super.addViewControllers (Registry); 
    }

4, resourceViewResolver: view resolver

/ ** 
 * Configuration Request view mapping 
 * @return 
 * / 
@Bean 
public the InternalResourceViewResolver resourceViewResolver () 
{ 
	the InternalResourceViewResolver InternalResourceViewResolver the InternalResourceViewResolver new new = (); 
	// file view request address prefix 
	internalResourceViewResolver.setPrefix ( "/ WEB-INF / jsp /" ); 
	// file view request suffix 
	internalResourceViewResolver.setSuffix ( "JSP."); 
	return InternalResourceViewResolver; 
} 

/ ** 
 * view configuration 
 * @param Registry 
 * / 
@Override 
public void configureViewResolvers (ViewResolverRegistry Registry) { 
	super.configureViewResolvers (Registry ); 
	registry.viewResolver (resourceViewResolver ()); 
	/*registry.jsp("/WEB-INF/jsp/",".jsp");*/
}

5, configureMessageConverters: Transcriber

/ ** 
* Message content conversion configuration 
 * Configuration fastJson return json conversion 
 * @param Converters 
 * / 
@Override 
public void configureMessageConverters (? List <HttpMessageConverter <>> Converters) { 
    // call the parent class configuration 
    super.configureMessageConverters (converters); 
    // Create message converter fastJson 
    FastJsonHttpMessageConverter fastConverter new new FastJsonHttpMessageConverter = (); 
    // create a configuration based 
    FastJsonConfig fastJsonConfig new new FastJsonConfig = (); 
    // modify the configuration of the returned content filtering 
    fastJsonConfig.setSerializerFeatures ( 
            SerializerFeature.DisableCircularReferenceDetect, 
            SerializerFeature.WriteMapNullValue, 
            SerializerFeature. WriteNullStringAsEmpty
    ); 
    FastConverter.setFastJsonConfig (fastJsonConfig); 
    // add to the fastjson the list view message converter 
    converters.add (fastConverter); 

}

6, addResourceHandlers: static resources

    @Override 
    public void addResourceHandlers (ResourceHandlerRegistry Registry) { 
        // custom in the project directory 
        //registry.addResourceHandler("/my/**").addResourceLocations("classpath:/my/ "); 
        // points to an external directory 
        registry. addResourceHandler ( "/ My / **") addResourceLocations ( "File: E: / My /");. 
        super.addResourceHandlers (Registry); 
    }

Third, the use WebMvcConfigurerAdapter

1, old-fashioned way: inheritance WebMvcConfigurerAdapter

 The method after spring boot 2.0, Spring 5.0, has been abandoned 

@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
	//TODO
}

2, instead of the program

 ① direct implementation WebMvcConfigurer

@Configuration
public class WebMvcConfg implements WebMvcConfigurer {
	//TODO
}

② direct successor WebMvcConfigurationSupport

@Configuration
public class WebMvcConfg extends WebMvcConfigurationSupport {
	//TODO
}

View source found: WebMvcConfigurerAdapter just an empty implementation of WebMvcCofigurer

WebMvcConfigurationSupport and WebMvcConfigurerAdapter, the interface WebMvcConfigurer in the same directory

WebMvcConfigurationSupport contains WebMvcConfigurer inside the method, and the method to achieve a more comprehensive WebMvcConfigurationSupport

But the inheritance WebMvcConfigurationSupport will find Spring Boot of WebMvc automatic configuration fails (WebMvcAutoConfiguration automated configuration), resulting in not view resolver can not be resolved and returned to the corresponding view.

About the use of the new program will appear, you can refer to the following article.

 

Guess you like

Origin www.cnblogs.com/sueyyyy/p/11608900.html