0

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")
Graham
  • 4,095
  • 4
  • 29
  • 37
  • Sounds similar to this? http://stackoverflow.com/questions/36594644/spring-controller-to-handle-all-requests-not-matched-by-other-controllers – Shiraaz.M May 01 '17 at 20:27
  • Unfortunately not. I can get the catchall View Controller to work easily enough, but I can't get it to work *and* have a Resource Handler working at the same time on a subset of what the catchall View Controller covers. – Graham May 01 '17 at 20:45
  • Can you post how you're wiring your spring boot app? Not sure if this is a 'standard spring boot app' or whether there's some custom configuration. – Shiraaz.M May 01 '17 at 20:56
  • Added in above. It's relatively standard, only that I prefer Kotlin for the language and XML for the configuration where I can. – Graham May 02 '17 at 07:05

0 Answers0