【发布时间】: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