9

My app uses Spring Boot on the back end and a SPA (Angular) site on the front end. Currently I am serving the index.html page from the webapp folder, which works automatically no configuration needed. Now I integrated a build process for the front end using gulp and all the created sources are "copied" into a build directory. Now I would like to serve the index.html file from the build directory as my main page.

I tried spring.application.index=build/index.htmland some other spring boot settings, but nothing worked. I believe no code is needed from my current code base but if anything is missing let me know.

Is there a way to configure this in the applications.properties file? Do I need to create a controller for the index page? Or is there any other way to change the default behavior?

thanks

1
  • Can we know more about your configuration Commented Jun 5, 2015 at 21:57

1 Answer 1

10

Going by the common Spring Boot properties, you should be able to modify this property:

spring.application.index=

Admittedly, I do tend to create a minimal 'home' controller with a @RequestMapping("/") anyway. :)

It's worth noting that the build directory will only be on the classpath if it's under src/main/resources. It's also worth mentioning that the contents of src/main/webapp don't get bundled into the jar automatically. src/main/resources/static is where Spring Boot will look for your static web files. As such, there are a couple of alternatives for you.

Option 1: Configure your Grunt build to output to a directory under src/main/resources/static.

Option 2: Configure your Java build tool to take the Grunt output and put it in your resources directory, so that it's on the classpath. For example, using Maven, the following would move the contents of a directory called build into your src/main/rescources/static.

<plugin>
  <artifactId>maven-resources-plugin</artifactId>
  <version>2.6</version>
  <executions>
    <execution>
      <id>copy-resources</id>
      <phase>validate</phase>
      <goals>
        <goal>copy-resources</goal>
      </goals>
      <configuration>
        <outputDirectory>${basedir}/target/classes/static</outputDirectory>
        <resources>
          <resource>
            <directory>build</directory>
            <filtering>true</filtering>
          </resource>
        </resources>
      </configuration>
    </execution>
  </executions>
</plugin>
Sign up to request clarification or add additional context in comments.

4 Comments

I tried "/build/index.html", "build/index.html", "classpath:build/index.html" and some other versions, nothing changes. I also tried the suggested solution from - stackoverflow.com/questions/27836584/…. I will try it with a "home" controller.
Ok the home controller worked, I guess I will leave at that and won't waste time to figure out why the other ways didn't, thanks.
I'm not sure whether they're useful alternatives, but I have added some options on how to configure the build to move the Grunt-generated resources into the right places. But if a couple of lines in a controller is working for you, then as you say, it's probably not worth spending too much time experimenting.
Hrmm... the link you provide doesn't have any reference to the property spring.application.index?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.