I'm trying to set up a Spring Boot such that:
- /resources/**/* goes to classpath:/resources/**/*
- Any controllers are handled correctly
- Anything else renders index.ftl without changing the URL (To support Javascript URL Routing)
I've gotten the first two of these working easily enough, and I can get "/" to route to index.ftl easily enough using a addViewControllers
, but the instant I add a View Controller for /**
it breaks the Resource Handlers that I previously got working.
I've tried playing around with ViewControllerRegistry.setOrder()
and ResourceHandlerRegistry.setOrder()
and it just refuses to co-operate.
What's the correct way of getting this to work?
Edit: Spring Boot Configuration:
The project is written using Kotlin, and built using Maven with the spring-boot-maven-plugin
. I'm using XML configuration files instead of Java for the most part, but I do have one Java config class.
The main Application looks like:
/**
* The main entry point to the application
*/
@EnableAutoConfiguration
@Configuration
@EnableAdminServer
@ImportResource(
"classpath:/spring/context.xml"
)
open internal class Application
/**
* Run the application
* @param args The command line arguments
*/
fun main(args: Array<String>) {
SpringApplication.run(Application::class.java, *args)
}
/spring/context.xml looks like:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<bean id="clock" class="java.time.Clock" factory-method="systemUTC" />
<bean id="restTemplate" class="org.springframework.web.client.RestTemplate" />
<import resource="users.xml" />
<import resource="authorization.xml" />
<import resource="authentication-google.xml" />
<bean class="uk.co.grahamcox.spring.MvcConfiguration">
</bean>
</beans>
And MvcConfiguration looks like:
/**
* Spring MVC Configuration
*/
@Configuration
open class MvcConfiguration() : WebMvcConfigurerAdapter() {
/**
* Add support for serving up static resources
*/
override fun addResourceHandlers(registry: ResourceHandlerRegistry) {
registry.addResourceHandler("/resources/**")
.addResourceLocations("classpath:/resources/")
.setCacheControl(CacheControl.noStore())
}
/**
* {@inheritDoc}
*
* This implementation is empty.
*/
override fun addViewControllers(registry: ViewControllerRegistry) {
registry.addViewController("/")
.setViewName("index")
}
}
Adding in this to addViewControllers
will, depending on the presence and value of registry.setOrder()
calls in both addViewControllers
and addResourceHandlers
, either do nothing or else break the Resource Handlers:
registry.addViewController("/**")
.setViewName("index")