Spring boot war找不到index.html

时间:2016-01-11 21:01:43

标签: java spring maven tomcat spring-boot

我是Spring boot和tomcat的初学者,我遇到了这个问题:

在tomcat服务器上部署spring boot war文件时,如果我想将静态index.html与它一起添加为战争,我会遇到问题。

要将其部署为战争我将其添加到我的应用程序类:

@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);
    }
}

现在应用程序正常工作,但index.html(位于src / main / resources下)将不会显示(我正在获取Whitelabel错误页面,其中包含“出现意外错误(type = Method Not Allowed,status = 405)。 请求方法'GET'不受支持“)

但如果我像这样创建它:

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

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

它向我展示了index.html(但是应用程序不适用于早先工作的浏览器的GET请求,我得到HTTP状态404 - 请求的资源不可用。)。

我如何配置它们以便它们都能正常工作?

这是我的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>

1 个答案:

答案 0 :(得分:1)

  

位于src / main / resources

index.html添加到src/main/resources/public目录

根据spring boot's doc

  

默认情况下,Spring Boot将提供目录中的静态内容   在/中调用/ static(或/ public或/ resources或/ META-INF / resources)   类路径或来自ServletContext的根