【问题标题】:War file not getting executed in tomcat战争文件没有在tomcat中执行
【发布时间】:2016-01-18 18:18:51
【问题描述】:

我是 Web 应用程序开发的新手。

我一直致力于开发一个 REST Web 服务并将其本地部署在 apache tomcat 服务器上。我正在为此使用 sts。

现在,当我从 sts 执行我的应用程序时(作为 java 应用程序运行)。 它运行良好。我正在访问 localhost:8080 的 url,一切都很好。

现在,我正在做以下事情:

  1. 为我的应用程序创建了一个名为 sample-rest-service.war 的 .war 文件
  2. 在另一个系统上,我下载并安装了配置为在端口 9999 上运行的 tomcat 8
  3. 我将 .war 文件复制到 apache-tomcat-8.028 的 webapps 文件夹中
  4. 我使用 startup.sh 启动我的 tomcat 服务器
  5. 我在浏览器中输入 localhost:9999 并出现主页
  6. 我输入 localhost:9999/sample-rest-service,但我收到代码为 404 的错误消息
  7. 我输入 localhost:9999/examples,它工作正常,i.r.内容出现
  8. 我检查了 webapps 文件夹,.war 文件已被提取,war 文件和提取的文件夹都存在于那里。

我错过了什么?

通常,如果我通过 sts 运行我的应用程序,我会这样做:

  1. 作为 Java 应用程序运行
  2. 然后它让我选择,我选择了Application-hello,其中Application是有main()的类,hello是包
  3. 之后在控制台中我可以看到 Tomcat 在端口启动的消息:8080 (http)
  4. 现在在我的浏览器中输入:http://localhost:8080/greeting
  5. 我得到 JSON 格式的输出

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
  <display-name>sample-rest-service</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

为了引发战争,我做了以下事情:

  1. 在我的项目中添加了 maven war 插件
  2. 将 pom.xml 中的包装从 jar(默认)更改为 war
  3. 导出战争文件

pom.xml

<?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-rest-service</artifactId>
    <version>0.1.0</version>

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

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </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>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.6</version>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-releases</id>
            <url>https://repo.spring.io/libs-release</url>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-releases</id>
            <url>https://repo.spring.io/libs-release</url>
        </pluginRepository>
    </pluginRepositories>


    <packaging>war</packaging>
</project>

【问题讨论】:

  • Tomcat 访问日志中显示了什么?
  • @TimBiegeleisen 日志文件夹中有许多日志文件 - catalina.date.log、catalina.out、host-manager.log、manager.log、localhost.log、localhost-access.log。哪一个是相关的?
  • localhost-access.log 说什么?
  • 127.0.0.1 - - [20/Oct/2015:14:36:10 +0530] "GET /sample-rest-service HTTP/1.1" 302 - 127.0.0.1 - - [20/ Oct/2015:14:36:10 +0530] "GET /sample-rest-service/HTTP/1.1" 404 1034 127.0.0.1 - - [20/Oct/2015:14:36:38 +0530] "GET /样本休息服务/HTTP/1.1" 404 1034
  • 我推荐一个教程的原因是使用 Java WAR 的主要目的是能够在您的服务器上运行 servlet。你甚至没有这些,所以这个问题有点奇怪。

标签: rest tomcat


【解决方案1】:

事实证明,整个问题都在于构建 war 文件。

通过sts构建可部署和可运行war文件的正确方法解释here

  1. 使用 SpringBootServletInitializer 扩展您的 Application 类,并覆盖配置方法

    @SpringBootApplication 公共类应用扩展 SpringBootServletInitializer{

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
    
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }
    
  2. 在 pom.xml 中,在包装标签中提及 war

  3. 您需要将 spring-boot-starter-tomcat 依赖项标记为“已提供”

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-21
    • 2018-11-03
    • 1970-01-01
    • 1970-01-01
    • 2012-11-27
    • 1970-01-01
    • 2016-09-25
    • 2018-06-28
    相关资源
    最近更新 更多