Spring 5 static resource mapping

392 Views Asked by At

I'm migrating to spring 5. I'm moving all my xml to java config, but I'm stuck on the static resources.

In my old xml config I have resources defined as such: <mvc:resources mapping="/3rdparty/**" location="/3rdparty/" />

Im adding them to my Java Config class

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.mypackages.*")
public class WebConfig implements WebMvcConfigurer {


    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        registry.addResourceHandler("/3rdparty/**").addResourceLocations("/3rdparty/");


    }

     @Bean
       public ViewResolver viewResolver() {
          InternalResourceViewResolver bean = new InternalResourceViewResolver();

          bean.setPrefix("/");
          bean.setSuffix(".jsp");

          return bean;
       }



}

Now I'm getting these errors for my resources No mapping for GET /myapp/gui/3rdparty/bootstrap-3.3.6-dist/js/bootstrap.min.js

It all worked well with the xml config.. Any ideas how I should define the pattern..

The folder structure is like this

ROOT
 |_WEB-INF
 |_META-INF
 |_gui
   |_3rdparty

I've also moved my startup to java config

public class AppInitializer implements WebApplicationInitializer{

     @Override
        public void onStartup(ServletContext container) {

             AnnotationConfigWebApplicationContext context  = new AnnotationConfigWebApplicationContext();
             context.setConfigLocation("com.mypackages.config");

             container.addListener(new ContextLoaderListener(context));


            ServletRegistration.Dynamic dispatcher = container
              .addServlet("dispatcher", new DispatcherServlet(context));

            dispatcher.setLoadOnStartup(1);
            dispatcher.addMapping("/");
        }
}

Thanks in advance for any help

1

There are 1 best solutions below

1
seb.wired On

Two things I suggest to change:

  1. Use AnnotationConfigWebApplicationContext#scan("com.mypackages.config") instead of #setConfigLocation(...) in order to scan classes annotated with @Configuration. Or register a specific class directly: #register(WebConfig.class).
  2. Double check the argument in #addResourceLocations("/3rdparty/"). Your folder structure indicates that #addResourceLocations("/gui/3rdparty/") might work.

In Maven projects packaged as .war, all web resources are typically in a separated webapp folder, see Maven War Plugin for reference. In your case:

|-- pom.xml
|-- src
    |-- main
        |-- java
        |   |-- com
        |       |-- mypackages
        |           |-- config
        |               |-- WebConfig.java
        |               |-- AppInitializer.java
        |-- resources
        |-- webapp
            |-- gui
                |-- 3rdparty
                    |-- bootstrap.min.js

With WebConfig.java

@ComponentScan(basePackages = "com.stackoverflow.*")
@Configuration 
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {

     @Override 
     public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/gui/3rdparty/**").addResourceLocations("/gui/3rdparty/");
     }
}

And AppInitializer.java:

public class AppInitializer implements WebApplicationInitializer {

    @Override 
    public void onStartup(ServletContext container) throws ServletException {
        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
        context.register(WebConfig.class);

        container.addListener(new ContextLoaderListener(context));
        ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new DispatcherServlet(context));

        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/");
}

}

The bootstrap resource will be available at http://<host>:<port>/gui/3rdparty/bootstrap.min.js