My application is build using backbone on frontend and spring framework on backend. It is a single html application. Routes are handled by backbone, so I have a backend route with the next structure:
@RequestMapping(value="/**", method=RequestMethod.GET)
public String Pages()
{
return "index";
}
To point everything to my index.html. The thing is that the static content files are pointed to this route too, and I don't want this. I've tried to config WebMvcConfigurerAdapter by overriding addResourceHandler method for static content, but it doesn't work.
@Configuration
public class StaticResourceConfiguration extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/js/**").addResourceLocations("/resources/js");
}
}
How can I point every route to my index.html except /js/** and /assets/** ?
Thank you
The first thing is that your controller method that's mapped to /**
will be taking priority over any resource requests. You can address this by increasing the precedence of ResourceHandlerRegistry
. Add a call to registry.setOrder(Ordered.HIGHEST_PRECEDENCE)
in the addResourceHandlers
method of StaticResourceConfiguration
:
@Configuration
public class StaticResourceConfiguration extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
registry.addResourceHandler("/js/**").addResourceLocations("/resources/js");
}
}
The second thing is that, by default, Spring Boot configures two resource handlers for you by default, one mapped to /**
and one mapped to /webjars/**
. Due to the change described above, this will now take priority over the method in your controller that's also mapped to /**
. To overcome this, you should turn off default resource handling via a setting in application.properties
:
spring.resources.addMappings=false
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With