【问题标题】:Spring boot war cannot find index.htmlSpring Boot War 找不到 index.html
【发布时间】:2016-04-16 07:41:58
【问题描述】:

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

在 tomcat 服务器上部署 Spring Boot 战争文件时,当我想将静态 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>

【问题讨论】:

    标签: java spring maven tomcat spring-boot


    【解决方案1】:

    位于 src/main/resources 下

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

    根据spring boot's doc

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

    【讨论】:

    • 它没有用。我试图将 index.hml 放在以下路径中: /src/main/resources/public/index.html /src/main/resources/static/index.html /src/main/resources/index.html src/ main/resources/META-INF/index.html 它没有用,同样的 Whitelabel 错误页面。 (MyApp 类在 src/main/java/com/app/main 下)
    • 您是否将 index.html 添加到所有文件中?什么错误?
    • 是的,我已经一次性将其添加到所有这些文件中。我得到的错误是:Whitelabel 错误页面此应用程序没有明确的 /error 映射,因此您将其视为后备。 2016 年 1 月 12 日星期二 00:20:42 IST 出现意外错误(类型=不允许方法,状态=405)。不支持请求方法“GET”
    • 您向哪个网址发送请求? /index.html?
    • 您是否在/ 上定义了非GET 控制器方法?如果是这样,请将您的请求发送至/index.html
    猜你喜欢
    • 1970-01-01
    • 2018-03-09
    • 1970-01-01
    • 1970-01-01
    • 2015-12-27
    • 1970-01-01
    • 2017-05-01
    • 2016-11-21
    • 2021-11-07
    相关资源
    最近更新 更多