【问题标题】:Can not access deployed WAR file on Tomcat无法访问 Tomcat 上部署的 WAR 文件
【发布时间】:2017-04-04 09:23:31
【问题描述】:

我有一个在我的本地机器上运行的 Spring Boot 休息服务项目。当我将应用程序作为“Spring Boot App”运行时

我可以通过去访问休息服务

http://127.0.0.1:8080/persons/all

它会按预期返回 JSON。

我将 pom.xml 打包更改为 war,然后我通过转到 Run as -> Maven build in Spring tools suit 创建了一个战争。它创建一个战争文件。当我在http://myserverip:8080/manager/ 上上传战争文件时,我没有收到任何错误,它显示在应用程序下

上传后war文件名为“myapp-0.0.1-SNAPSHOT.war”,我尝试访问

http://myserverip:8080/myapp-0.0.1-SNAPSHOT/persons/all

但我收到 404 错误

我在这里做错了什么?

我的 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>com.mycompany</groupId>
    <artifactId>myapp</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <name>myapp</name>
    <description>myappapi</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.2.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
    </dependencies>

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


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

当我构建应用程序时,我得到以下日志

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building myapp 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ myapp ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ myapp ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ myapp ---
[INFO] Not copying test resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ myapp ---
[INFO] Not compiling test sources
[INFO] 
[INFO] --- maven-surefire-plugin:2.18.1:test (default-test) @ myapp ---
[INFO] Tests are skipped.
[INFO] 
[INFO] --- maven-war-plugin:2.6:war (default-war) @ myapp ---
[INFO] Packaging webapp
[INFO] Assembling webapp [myapp] in [C:\Users\me\Documents\workspace-sts-3.8.2.RELEASE\myapp\target\myapp-0.0.1-SNAPSHOT]
[INFO] Processing war project
[INFO] Webapp assembled in [586 msecs]
[INFO] Building war: C:\Users\me\Documents\workspace-sts-3.8.2.RELEASE\myapp\target\myapp-0.0.1-SNAPSHOT.war
[INFO] 
[INFO] --- spring-boot-maven-plugin:1.4.2.RELEASE:repackage (default) @ myapp ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.300 s
[INFO] Finished at: 2016-11-21T10:12:03-05:00
[INFO] Final Memory: 20M/243M
[INFO] ------------------------------------------------------------------------
[WARNING] The requested profile "pom.xml" could not be activated because it does not exist.

我的 MyappApplication.java

package com.mycompany;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

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

【问题讨论】:

  • 您可以在 pom.xml 中将您的 buid 名称更改为 myapp。在服务器启动时检查/persons/all 是否已注册。如果没有,它将显示 404 错误,因为您的控制器未正确注册。
  • @Lucky 但该应用程序在我的本地计算机上运行良好。是否有可能在 Spring boot 本地注册,但在 Tomcat 上使用 war 文件部署时不注册?
  • 是的,请检查您的 Tomcat 服务器日志。 Spring Boot 使用嵌入式 tomcat。您正在部署到一个单独的 tomcat 实例中。两者都不一样。
  • @Lucky 启动 tomcat 后我确实看到 myapp 正在部署,但没有关于 /persons/all pastebin.com/9iyPxuBs
  • 我希望您的组件、配置、服务在 company 包下。如果这个包里没有那些。另外,不要使用三个@Configuration、@EnableAutoConfiguration 和@ComponentScan 注释,而是使用这个@SpringBootApplication 注释。还将spring-webmvc 依赖项添加到您的webapp。这会将应用程序标记为 Web 应用程序并激活关键行为,例如设置 DispatcherServlet。(即类似于添加 @EnableWebMvc)

标签: spring tomcat spring-boot tomcat8 spring-web


【解决方案1】:

参考:Packaging executable jar and war files

正如 cmets 中提到的,您可以传统地通过将 war 部署到独立的 tomcat 实例来部署 spring boot 应用程序。您需要对具有 main() 方法的 MyappApplication.java 类进行一些更改。

  • 使您的MyappApplication 扩展SpringBootServletInitializer 并覆盖其configure() 方法,

    @SpringBootApplication
    public class MyappApplication extends SpringBootServletInitializer {
    
        @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
            return application.sources(Application.class);
        }
    
        public static void main(String[] args) throws Exception {
            SpringApplication.run(MyappApplication.class, args);
        }
    
    }
    
  • 下一步是将打包作为 war 而不是您已经拥有的默认 jar。

  • 最后在您的pom.xml 中将嵌入式tomcat 容器的作用域更改为provided,这样当您部署到单独的实例时它不会发生冲突。

【讨论】:

  • 谢谢它现在可以工作了 :) 但 SpringBootServletInitializer 显示为折旧。
  • @Arya 您可能正在使用org.springframework.boot.context.web.SpringBootServletInitializer,请改用org.springframework.boot.web.support.SpringBootServletInitializer。
【解决方案2】:

如果您将 jar 更改为 spring boot 之战,请添加以下依赖项

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

http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#build-tool-plugins-maven-packaging

【讨论】:

    【解决方案3】:

    将 Spring Boot 可执行 jar 更改为可部署战争的清单。

    1. 将 pom.xml 中的包装从“jar”更改为“war”

    2. 正如 kuhajeyan 提到的,将嵌入的 tomcat 依赖项标记为 pom.xml 中提供的内容。

      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
      

    3. 让你的主 Spring Boot 应用程序类像这样扩展 SpringBootServletInitializer

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

    From the documentations

    【讨论】:

      猜你喜欢
      • 2012-06-05
      • 1970-01-01
      • 2014-02-01
      • 2012-09-28
      • 2012-12-05
      • 1970-01-01
      • 2010-09-13
      • 2012-07-29
      • 2012-12-16
      相关资源
      最近更新 更多