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
Two things I suggest to change:
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).#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 separatedwebappfolder, see Maven War Plugin for reference. In your case:With
WebConfig.javaAnd
AppInitializer.java:}
The bootstrap resource will be available at
http://<host>:<port>/gui/3rdparty/bootstrap.min.js