Java 将WAR部署到Tomcat(弹簧靴+;角度)

Java 将WAR部署到Tomcat(弹簧靴+;角度),java,angular,spring,spring-boot,tomcat,Java,Angular,Spring,Spring Boot,Tomcat,我正在尝试将带有WAR打包的Spring Boot应用程序部署到Tomcat 10。但是,当我尝试访问端点时,应用程序成功部署,结果是404找不到 WAR文件:application.WAR Tomcatwebapps/application文件夹由以下内容和index.html(角度)have 如前所述,我添加了一个类AppServletInitializer,它扩展了SpringBootServletInitializer,如下所示 @Configuration public class

我正在尝试将带有WAR打包的Spring Boot应用程序部署到Tomcat 10。但是,当我尝试访问端点时,应用程序成功部署,结果是404找不到

WAR文件:application.WAR

Tomcat
webapps/application
文件夹由以下内容和
index.html
(角度)have


如前所述,我添加了一个类
AppServletInitializer
,它扩展了
SpringBootServletInitializer
,如下所示

@Configuration
public class AppServletInitializer extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(AutomationwhizApp.class);
    }
} 
在pom.xml中,我为spring boot starter tomcat、tomcat embed jasper添加了依赖项,并将打包选项设置为war。我已经分享了简化版(删除了依赖项、插件。如果需要,将添加相同的内容)

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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.name</groupId>
    <artifactId>application</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>My Application</name>

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

    <properties>
        <!-- Build properties -->
        <maven.version>3.3.9</maven.version>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <project.testresult.directory>${project.build.directory}/test-results</project.testresult.directory>
        <maven.build.timestamp.format>yyyyMMddHHmmss</maven.build.timestamp.format>
        <maven.compiler.source>${java.version}</maven.compiler.source>
        <maven.compiler.target>${java.version}</maven.compiler.target>
        <argLine>-Djava.security.egd=file:/dev/./urandom -Xmx256m</argLine>
        <m2e.apt.activation>jdt_apt</m2e.apt.activation>
        <run.addResources>false</run.addResources>

        <jhipster-dependencies.version>3.0.5</jhipster-dependencies.version>
        <spring-boot.version>2.1.8.RELEASE</spring-boot.version>
        <spring.version>5.1.9.RELEASE</spring.version>
        <hibernate.version>5.3.11.Final</hibernate.version>
        <javassist.version>3.23.2-GA</javassist.version>
        <maven-clean-plugin.version>3.1.0</maven-clean-plugin.version>
        <maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
        <maven-javadoc-plugin.version>3.1.1</maven-javadoc-plugin.version>
        <maven-resources-plugin.version>3.1.0</maven-resources-plugin.version>
        <maven-war-plugin.version>3.2.3</maven-war-plugin.version>
        <properties-maven-plugin.version>1.0.0</properties-maven-plugin.version>
    </properties>


    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>junit</groupId>
                    <artifactId>junit</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <defaultGoal>spring-boot:run</defaultGoal>
        <finalName>application</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-enforcer-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <mainClass>${start-class}</mainClass>
                    <fork>true</fork>
                </configuration>
            </plugin>
        </plugins>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>${maven-compiler-plugin.version}</version>
                </plugin>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <version>${spring-boot.version}</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

    <profiles>
        <profile>
            <id>dev</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-devtools</artifactId>
                    <optional>true</optional>
                </dependency>
            </dependencies>
        </profile>
        <profile>
            <id>prod</id>
            <build>
                <plugins>
                    <plugin>
                        <artifactId>maven-clean-plugin</artifactId>
                        <configuration>
                            <filesets>
                                <fileset>
                                    <directory>target/classes/static/</directory>
                                </fileset>
                            </filesets>
                        </configuration>
                    </plugin>
                    <plugin>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-maven-plugin</artifactId>
                        <configuration>
                            <mainClass>${start-class}</mainClass>
                        </configuration>
                        <executions>
                            <execution>
                                <goals>
                                    <goal>build-info</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                    <!--                    <plugin>-->
                    <!--                        <groupId>org.apache.maven.plugins</groupId>-->
                    <!--                        <artifactId>maven-war-plugin</artifactId>-->
                    <!--                        <version>${maven-war-plugin.version}</version>-->
                    <!--                        <executions>-->
                    <!--                            <execution>-->
                    <!--                                <goals>-->
                    <!--                                    <goal>war</goal>-->
                    <!--                                </goals>-->
                    <!--                                <phase>package</phase>-->
                    <!--                            </execution>-->
                    <!--                        </executions>-->
                    <!--                        <configuration>-->
                    <!--                            &lt;!&ndash;<warSourceIncludes>WEB-INF/**,META-INF/**</warSourceIncludes>&ndash;&gt;-->
                    <!--                            <failOnMissingWebXml>false</failOnMissingWebXml>-->
                    <!--                        </configuration>-->
                    <!--                    </plugin>-->
                </plugins>
            </build>
        </profile>
    </profiles>
</project>
server:
  servlet:
    context-path: /application
我们在客户端使用Angular,资源位于静态文件夹中。

环境: 弹簧靴2.1.8, 角度8, 爪哇11

我已经提到了,不幸的是,没有一个是为我工作的。任何帮助都将不胜感激


  • 项目中的配置是正确的。正如评论中提到的,Tomcat10在alpha版本中可能存在一些问题。我使用了Tomcat 9,它工作得很好

    配置如下所示

    步骤1:在pom.xml中将packing选项更改为war

    war

    第2步:扩展SpringBootServletInitializer

    步骤3:创建WAR文件

    我们走了!我们可以在以下位置访问端点:

    http://localhost:8080/context-path/endpoint OR
    http://localhost:8080/war-filename/endpoint
    
    我们还可以为
    war
    创建配置文件,如下所示

    
    3.2.3
    org.springframework.boot
    springbootmaven插件
    org.apache.maven.plugins
    maven战争插件
    ${maven war plugin.version}
    战争
    包裹
    WEB-INF/**,META-INF/**
    假的
    目标/类/静态/
    src/main/webapp
    WEB-INF/**
    战争
    org.apache.maven.plugins
    maven战争插件
    
    这不是一个真正的答案,只是一个简单的想法:打开start.spring.io并创建一个“示例项目”,该项目使用打包WAR以及java和spring启动版本生成一个工件。编译它并尝试在tomcat上部署,确保它按预期工作(这样您就可以确保tomcat正常),然后尝试比较创建的工件并查看差异。打开environment.ts文件,检查后端项目的基本URL。您是否在主类中使用
    @SpringBootApplication
    注释?您的主类应该扩展
    SpringBootServletilizer
    ,并实现一个main来实际启动SpringApplication…您可以试试Mark所说的。另外,检查它是否在另一个版本的Tomcat中工作。9或8。10是最新版本,它仍处于alpha阶段。请确保您的
    服务器.contextPath
    配置正确,请参阅此
    server:
      servlet:
        context-path: /application
    
    import org.springframework.boot.builder.SpringApplicationBuilder;
    import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
    
    /**
     * This is a helper Java class that provides an alternative to creating a {@code web.xml}.
     * This will be invoked only when the application is deployed to a Servlet container like Tomcat, JBoss etc.
     */
    public class ApplicationWebXml extends SpringBootServletInitializer {
    
        @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {       
            return application.sources(ApplicationApp.class);
        }
    }
    
    mvn clean install 
    
    http://localhost:8080/context-path/endpoint OR
    http://localhost:8080/war-filename/endpoint