The type WebMvcConfigurerAdapter is deprecated

前端 未结 4 789
灰色年华
灰色年华 2020-12-04 08:10

I just migrate to spring mvc version 5.0.1.RELEASE but suddenly in eclipse STS WebMvcConfigurerAdapter is marked as deprecated

public class MvcC         


        
4条回答
  •  星月不相逢
    2020-12-04 08:30

    In Spring every request will go through the DispatcherServlet. To avoid Static file request through DispatcherServlet(Front contoller) we configure MVC Static content.

    Spring 3.1. introduced the ResourceHandlerRegistry to configure ResourceHttpRequestHandlers for serving static resources from the classpath, the WAR, or the file system. We can configure the ResourceHandlerRegistry programmatically inside our web context configuration class.

    • we have added the /js/** pattern to the ResourceHandler, lets include the foo.js resource located in the webapp/js/ directory
    • we have added the /resources/static/** pattern to the ResourceHandler, lets include the foo.html resource located in the webapp/resources/ directory
    @Configuration
    @EnableWebMvc
    public class StaticResourceConfiguration implements WebMvcConfigurer {
    
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            System.out.println("WebMvcConfigurer - addResourceHandlers() function get loaded...");
            registry.addResourceHandler("/resources/static/**")
                    .addResourceLocations("/resources/");
    
            registry
                .addResourceHandler("/js/**")
                .addResourceLocations("/js/")
                .setCachePeriod(3600)
                .resourceChain(true)
                .addResolver(new GzipResourceResolver())
                .addResolver(new PathResourceResolver());
        }
    }
    

    XML Configuration

    
      
    

    Spring Boot MVC Static Content if the file is located in the WAR’s webapp/resources folder.

    spring.mvc.static-path-pattern=/resources/static/**
    

提交回复
热议问题