我有spring引导应用程序,当我用spring引导运行它时,它可以工作,但是当我构建war并将其部署到tomcat时,我得到404状态
我将这些行从spring docs添加到maven:
<properties>
<start-class>com.example.deploytest.DeploytestApplication</start-class>
</properties>
<packaging>war</packaging>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
这是我的主类:
@SpringBootApplication
public class DeploytestApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(DeploytestApplication.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(DeploytestApplication.class);
}
}
和简单控制器:
@RestController
public class MainController {
@GetMapping
public String hello() {
return "hello world";
}
}
tomcat版本是10.0.27
怎么了?
pom.xml应该像这样:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
如果没有,你也可以尝试在你的主类中添加SpringBootServletInitializer接口:
@SpringBootApplication
public class SpringBootTomcatApplication extends SpringBootServletInitializer {
}
尝试给你的GetMapping一个url
@GetMapping("/")
servlet API版本不匹配,尝试使用与嵌入式tomcat相同的版本