user2212726
user2212726

Reputation: 1285

Spring boot war cannot find index.html

Im a beginner in Spring boot and tomcat, and i get this issue:

When deploying a spring boot war file on a the tomcat server, i get a problem when i want to add a static index.html alongside with it as a war.

to enable it to be deployed as war i added this to my application class:

@SpringBootApplication
@ComponentScan(basePackages = {"com.app"})
@EnableMongoRepositories(basePackages = {"com.app"})
public class MyApp extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(MyApp.class);
    }

    public static void main(String[] args) {
        SpringApplication.run(MyApp.class, args);
    }
}

now the application works, but the index.html (which is located under src/main/resources) won't show (im getting Whitelabel Error Page with "There was an unexpected error (type=Method Not Allowed, status=405). Request method 'GET' not supported")

but if i create it like this:

@SpringBootApplication
@ComponentScan(basePackages = {"com.app"})
@EnableMongoRepositories(basePackages = {"com.app"})
public class MyApp {

    public static void main(String[] args) {
        SpringApplication.run(MyApp.class, args);
    }
}

it shows me the index.html (but the application won't work for a GET request from the browser which work earlier i get HTTP Status 404 - The requested resource is not available. ).

How can i configure it so they both will work?

this is my pom.xml

<packaging>war</packaging>
<build>
    <finalName>myapp</finalName>
</build>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.1.RELEASE</version>
</parent>

<dependencies>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-mongodb</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-mongodb</artifactId>
        <version>1.8.1.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>

    <!-- Test -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>com.jayway.restassured</groupId>
        <artifactId>rest-assured</artifactId>
        <version>2.7.0</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <scope>test</scope>
    </dependency>

</dependencies>

Upvotes: 4

Views: 4168

Answers (1)

Ali Dehghani
Ali Dehghani

Reputation: 48213

which is located under src/main/resources

Add the index.html to src/main/resources/public directory

According to spring boot's doc:

By default Spring Boot will serve static content from a directory called /static (or /public or /resources or /META-INF/resources) in the classpath or from the root of the ServletContext

Upvotes: 1

Related Questions