【问题标题】:Deploying a WAR in tomcat在 tomcat 中部署 WAR
【发布时间】:2024-01-07 19:50:02
【问题描述】:

我已经在archlinux上安装了tomcat,我尝试了tomcat7和tomcat8。根据包括官方文档在内的多个来源,部署 WAR 文件就像将其放入 webapps 文件夹(在我的例子中是 /var/lib/tomcat7/webapps)中一样简单。 WAR 文件被分解。我不知道的是如何访问我的网络应用程序。在 localhost:8080 上有一个 tomcat 网页。我还尝试了 localhost:8080/name-of-the-war-file,但这只允许 HTTP 状态 404。

我用来测试的应用程序是第一个关于 spring boot 的指南: http://spring.io/guides/gs/spring-boot/

我已经修改了 maven 构建文件 pom.xml 以在运行 'mvn package' 时生成一个 WAR 文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.springframework</groupId>
    <artifactId>gs-spring-boot</artifactId>
    <version>0.1.0</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.2.2.RELEASE</version>
    </parent>

    <packaging>war</packaging>

    <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>

    <properties>
        <java.version>1.7</java.version>
    </properties>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

【问题讨论】:

  • hm.. 我希望这是一个简单的...
  • 嗯,投反对票?你混淆了按钮吗?

标签: tomcat spring-boot


【解决方案1】:

除了修改 pom.xml 以生成 WAR 文件外,还需要为 web 应用程序配置 tomcat。这以前是通过 web.xml 文件完成的。如今,这可以从应用程序本身内部归档。 SpringBootServletInitializer 和 WebApplicationInitializer 是要寻找的线索。我发现让我全部运行的最简单方法是按以下方式修改 Application.java:

package hello;

import java.util.Arrays;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }

    public static void main(String[] args) {
        ApplicationContext ctx = SpringApplication.run(Application.class, args);

        System.out.println("Let's inspect the beans provided by Spring Boot:");

        String[] beanNames = ctx.getBeanDefinitionNames();
        Arrays.sort(beanNames);
        for (String beanName : beanNames) {
            System.out.println(beanName);
        }
    }
}

现在我可以看到“来自 Spring Boot 的问候!”在http://localhost:8080/gs-spring-boot-0.1.0/ 上(其中 gs-spring-boot-0.1.0.war 是部署的 WAR 文件的名称)。

【讨论】: