使用war将spring-boot和angular 7应用程序部署到tomcat 8.5中

Posted

技术标签:

【中文标题】使用war将spring-boot和angular 7应用程序部署到tomcat 8.5中【英文标题】:Deploy spring-boot and angular 7 application using war into tomcat 8.5 【发布时间】:2019-08-16 11:47:33 【问题描述】:

我正在尝试使用 war 将 Angular 7 + Spring Boot 应用程序部署到 tomcat 中。我已经按照一些步骤使用角度代码创建战争。使用 UI 代码成功创建了 War。但我无法调用 UI 代码。 当我试图打电话时

localhost:8080/war-name localhost:8080/war-name/index.html localhost:8080/index.html

它显示404

Web 服务工作正常。我可以打电话给他们。

如何调用我的应用程序的index.html

注意:- 我没有使用spring-boot-starter-web

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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.hackthon</groupId>
    <artifactId>pmoconnect</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>pmoconnect</name>
    <description>PMO Connect</description>
    <packaging>war</packaging>
    <properties>
        <java.version>1.8</java.version>
        <start-class>com.hackthon.HackthonApplication</start-class>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jersey</artifactId>
        </dependency>

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

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.0.1</version>
        </dependency>
        <!-- excel 2007 over -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.0.1</version>
        </dependency>

        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-multipart</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
        <dependency>
            <groupId>com.sun.mail</groupId>
            <artifactId>javax.mail</artifactId>
            <version>1.5.5</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-envers</artifactId>
            <version>$hibernate.version</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>com.github.eirslett</groupId>
                <artifactId>frontend-maven-plugin</artifactId>
                <version>1.3</version>

                <configuration>
                    <nodeVersion>v8.11.3</nodeVersion>
                    <npmVersion>6.3.0</npmVersion>
                    <workingDirectory>src/main/ngapp/</workingDirectory>
                </configuration>

                <executions>
                    <execution>
                        <id>install node and npm</id>
                        <goals>
                            <goal>install-node-and-npm</goal>
                        </goals>
                    </execution>

                    <execution>
                        <id>npm install</id>
                        <goals>
                            <goal>npm</goal>
                        </goals>
                    </execution>

                    <execution>
                        <id>npm run build</id>
                        <goals>
                            <goal>npm</goal>
                        </goals>

                        <configuration>
                            <arguments>run build</arguments>
                        </configuration>
                    </execution>
                    <execution>
                        <id>prod</id>
                        <goals>
                            <goal>npm</goal>
                        </goals>
                        <configuration>
                            <arguments>run-script build</arguments>
                        </configuration>
                        <phase>generate-resources</phase>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-resources</id>
                        <phase>validate</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>$project.build.directory/classes/static/</outputDirectory>
                            <!-- <outputDirectory>$project.build.directory/../src/main/resources/static/</outputDirectory> -->
                            <resources>
                                <resource>
                                    <directory>/src/main/ngapp/dist/</directory>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
            </plugin>
            <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>
                <configuration>
                    <packagingExcludes>WEB-INF/lib/tomcat-*.jar</packagingExcludes>
                    <warName>pmoconnect</warName>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

application.properties

spring.datasource.url = jdbc:mysql://localhost:3306/elite_db?allowPublicKeyRetrieval=true&useSSL=false
spring.datasource.username = <username>
spring.datasource.password = <password>


## Hibernate Properties
# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true

# Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto = update
org.hibernate.envers.audit_table_suffix = _AUDIT_LOG

HackthonApplication.java

@SpringBootApplication
@ComponentScan
public class HackthonApplication 
    public static void main(String[] args) 
        SpringApplication.run(HackthonApplication.class, args);
    

index.html

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>PMO Connect</title>
  <base href="./">

  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
  <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
</head>
 <body>
  <app-root></app-root>
  <script type="text/javascript" src="runtime.js"></script>
  <script type="text/javascript" src="es2015-polyfills.js" nomodule></script>
  <script type="text/javascript" src="polyfills.js"></script>
  <script type="text/javascript" src="styles.js"></script>
  <script type="text/javascript" src="vendor.js"></script>
  <script type="text/javascript" src="main.js"></script>
  </body>
</html>

战争文件

tomcat 日志

2019-03-27 10:34:19.134  INFO 18464 --- [ost-startStop-2] com.hackthon.HackthonApplication         : No active profile set, falling back to default profiles: default
    2019-03-27 10:34:19.291  INFO 18464 --- [ost-startStop-2] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
    2019-03-27 10:34:22.558  INFO 18464 --- [ost-startStop-2] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode.
    2019-03-27 10:34:22.977  INFO 18464 --- [ost-startStop-2] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 372ms. Found 6 repository interfaces.
    2019-03-27 10:34:24.386  INFO 18464 --- [ost-startStop-2] o.a.c.c.C.[.[localhost].[/pmoconnect]    : Initializing Spring embedded WebApplicationContext
    2019-03-27 10:34:24.386  INFO 18464 --- [ost-startStop-2] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 5094 ms
    2019-03-27 10:34:24.809  INFO 18464 --- [ost-startStop-2] o.s.b.a.jersey.JerseyAutoConfiguration   : Configuring existing registration for Jersey servlet 'com.hackthon.config.JerseyConfiguration'
    2019-03-27 10:34:25.046  INFO 18464 --- [ost-startStop-2] o.s.boot.web.servlet.RegistrationBean    : Servlet com.hackthon.config.JerseyConfiguration was not registered (possibly already registered?)
    2019-03-27 10:34:25.628  INFO 18464 --- [ost-startStop-2] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
    2019-03-27 10:34:26.473  INFO 18464 --- [ost-startStop-2] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
    2019-03-27 10:34:26.815  INFO 18464 --- [ost-startStop-2] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [
        name: default
        ...]
    2019-03-27 10:34:27.465  INFO 18464 --- [ost-startStop-2] org.hibernate.Version                    : HHH000412: Hibernate Core 5.3.7.Final
    2019-03-27 10:34:27.472  INFO 18464 --- [ost-startStop-2] org.hibernate.cfg.Environment            : HHH000206: hibernate.properties not found
    2019-03-27 10:34:28.996  INFO 18464 --- [ost-startStop-2] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations 5.0.4.Final
    2019-03-27 10:34:30.597  INFO 18464 --- [ost-startStop-2] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.MySQL5InnoDBDialect
    2019-03-27 10:34:30.951  INFO 18464 --- [ost-startStop-2] o.h.e.boot.internal.EnversServiceImpl    : Envers integration enabled? : true
    2019-03-27 10:34:41.272  INFO 18464 --- [ost-startStop-2] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
    2019-03-27 10:34:43.158  INFO 18464 --- [ost-startStop-2] o.h.h.i.QueryTranslatorFactoryInitiator  : HHH000397: Using ASTQueryTranslatorFactory
    2019-03-27 10:34:46.554  INFO 18464 --- [ost-startStop-2] com.hackthon.HackthonApplication         : Started HackthonApplication in 29.239 seconds (JVM running for 143.14)
    2019-03-27 10:34:46.944  INFO 18464 --- [ost-startStop-2] org.apache.jasper.servlet.TldScanner     : At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
    2019-03-27 10:34:50.219  WARN 18464 --- [ost-startStop-2] org.glassfish.jersey.internal.Errors     : The following warnings have been detected: WARNING: A HTTP GET method, public javax.ws.rs.core.Response com.hackthon.service.WSFGMapping.getMappings(java.lang.String), should not consume any entity.

    2019-03-27 10:34:50.348  INFO 18464 --- [ost-startStop-2] org.apache.catalina.startup.HostConfig   : Deployment of web application archive [C:\Users\kaukhare\myWorkspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\webapps\pmoconnect.war] has finished in [68,229] ms

【问题讨论】:

请发布您的 index.html @GaneshGudghe 已更新。 请查看我的回答,它将解决您的问题 您可以尝试在属性“server.servlet.context-path=/>”中添加上下文路径吗? @AkhilSK 我试过这个,但同样的 404 错误。 【参考方案1】:

我也遇到了同样的问题,我通过在 index.html

中添加以下脚本来解决
<base href="/" id="baseHref">
<script>
  (function() 
    document.getElementById('baseHref').href = '/' + window.location.pathname.split('/')[1] + "/";
  )();
</script>

我想它会解决你的问题。

【讨论】:

【参考方案2】:

考斯图布,

您似乎忘记从SpringBootServletInitializer 扩展您的应用程序。您可以在official documentation 阅读更多相关信息。 长话短说,请为您的主要课程尝试以下代码。

@SpringBootApplication
@ComponentScan
public class HackthonApplication extends SpringBootServletInitializer 
    public static void main(String[] args) 
        SpringApplication.run(HackthonApplication.class, args);
    

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) 
        return builder.sources(HackthonApplication.class);
    

希望这能解决您的问题。

【讨论】:

@KaustubhKhare 你能不能从 tomcat 控制台共享日志文件?看起来问题也存在于您的代码中。没有SpringBootServletInitializer,它无法工作,但即使有了它,由于代码中的其他问题,它也无法工作。我敢肯定,如果没有SpringBootServletInitializer,您的代码的第一版将无法运行。 我可以在没有SpringBootServletInitializer 代码的情况下调用网络服务。 我添加了tomcat日志。它没有给出任何错误。 让我看看。仅供参考,更新了文档的 URL - 使用这个:docs.spring.io/spring-boot/docs/1.5.6.RELEASE/reference/… 也许你是对的......如果不使用'spring-boot-starter-web'和控制器中的映射,它有点过于复杂。【参考方案3】:

我还没有找到使用spring-jersey 的任何解决方案。所以我决定使用spring-web

我对项目所做的必要更改,

pom.xml

删除spring-jersey并添加spring-web

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

package.json

"start": "ng serve --proxy-config proxy.conf.json",
"build": "ng build --base-href=\"./\"",

proxy.conf.json 文件添加到 Angular 应用的基本路径中,


  "/api": 
    "target": "http://localhost:8080",
    "secure": false
  

HackthonApplication.java

public class HackthonApplication extends SpringBootServletInitializer

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) 
        return application.sources(HackthonApplication.class);
    

还有一些针对 web 服务的注释更改。这是因为我已将spring-jersey 更改为spring-web

【讨论】:

【参考方案4】:

在您的 POM.xml 中,通过添加 Angular 应用程序的名称来更改您的目录路径。

<resource>
    <directory> /src/main/ngapp/dist/NAME_APP </directory>
</resource>

【讨论】:

【参考方案5】:
<execution>
                    <id>copy-standalone-index</id>
                    <phase>process-resources</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <overwrite>true</overwrite>
                        <outputDirectory>$project.build.directory/$project.artifactId-$project.version/</outputDirectory>
                        <resources>
                            <resource>
                                <directory>$project.basedir/src/main/webapp/src/</directory>
                                <includes>index.html</includes>
                            </resource>
                        </resources>
                    </configuration>
                </execution>

【讨论】:

以上是关于使用war将spring-boot和angular 7应用程序部署到tomcat 8.5中的主要内容,如果未能解决你的问题,请参考以下文章

没有父pom.xml的spring-boot无法生成war打包

如何将 Spring Boot Rest 服务和 Angular App 捆绑在一个 war 文件中

没有测试和 ng 服务的 angular-cli 简单配置

如何将 Spring Boot 和 Angular 应用程序部署为单个 war 文件?

单个 WAR 中的 Angular 4 和 Java 后端项目

如何将带有 angular 2 前端(angular-cli build)的 spring-boot webapp 部署到 Tomcat?