无法在Tomcat上访问已部署的WAR文件

时间:2016-11-21 03:45:15

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

我有一个Spring启动休息服务项目,可以在我的本地机器上运行。当我将应用程序作为" Spring Boot App"

运行时

我可以通过转到

来访问其余服务

http://127.0.0.1:8080/persons/all

它返回JSON,因为它应该。

我将pom.xml包装更改为war,然后我通过Run as - >创建了一个war; Maven内置Spring工具套装。它会创建一个war文件。当我在http://myserverip:8080/manager/上传war文件时,我没有收到错误,它显示在Applications

战争文件名是" 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.

my 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);
    }
}

3 个答案:

答案 0 :(得分:2)

参考:Packaging executable jar and war files

如评论中所述,您可以通过将war部署到独立的tomcat实例来传统部署Spring启动应用程序。您需要对具有MyappApplication.java方法的main()类进行一些更改。

  • 让您的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);
        }
    
    }
    
  • 下一步是将包装作为战争而不是您已经拥有的默认jar。

  • 最后在pom.xml中将嵌入式tomcat容器的范围更改为provided,以便在部署到单独的实例时不会发生冲突。

答案 1 :(得分:1)

如果您要将jar更改为spring boot of war,请添加以下依赖项

 <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

答案 2 :(得分:0)

将弹簧启动可执行jar更改为可部署战争的核对表。

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

  2. 正如kuhajeyan所提到的,请标记pom.xml中提供的嵌入式tomcat依赖项。

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

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

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