小编典典

Spring Boot War部署到Tomcat

spring

我想将Spring Boot应用程序部署到Tomcat,因为我想部署到AWS。我创建了一个WAR文件,但是即使它可见,它似乎也不能在Tomcat上运行。

详细信息:
0。这是我的应用程序:

@Configuration
@ComponentScan
@EnableAutoConfiguration
public class App {
    public static void main(String[] args) {
        SpringApplication.run(SampleController.class, args);
    }
}

@Controller
@EnableAutoConfiguration
public class SampleController {
    @RequestMapping("/help")
    @ResponseBody
    String home() {
        String input = "Hi! Please use 'tag','check' and 'close' resources.";
        return input;
    }
}

application.properties具有以下内容:

server.port=${port:7777}
  1. 阅读了许多页面和问题后,我在POM中添加了以下内容:

http://maven.apache.org/xsd/maven-4.0.0.xsd“> 4.0.0

<groupId>com.niewlabs</groupId>
<artifactId>highlighter</artifactId>
<version>1.0-SNAPSHOT</version>

<packaging>war</packaging>

<properties>
    <java.version>1.8</java.version>
</properties>    
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.1.9.RELEASE</version>
</parent>    
<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>
</dependencies>
  1. 我运行了“ mvn软件包”,并得到了WAR文件(大小为250Mb),并将其放入“ webapps”文件夹中。

  2. 我启动了Tomcat,并能够看到列出的应用程序,在本例中为“ /highlighter-1.0-SNAPSHOT”。

  3. 单击应用程序的链接将显示“状态404”页面。
  4. 当我单独运行Spring Boot应用程序而没有容器时,它运行在localhost:7777上,但是当我在Tomcat中运行它时却什么也没有。

阅读 347

收藏
2020-04-11

共1个答案

小编典典

本质上,我需要添加以下类:

public class WebInitializer extends SpringBootServletInitializer {   
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(App.class);
    }    
}

我还向POM添加了以下属性:

<properties>        
    <start-class>mypackage.App</start-class>
</properties>
2020-04-11