如何创建spring启动应用程序并在tomcat中作为.war运行?

q5iwbnjs  于 2021-08-20  发布在  Java
关注(0)|答案(1)|浏览(384)

这个问题在这里已经有了答案

在Tomcat10上部署SpringMVC5…部署问题(1个答案)
19小时前关门。

总结

我试图从头开始创建一个基本的spring启动应用程序,构建一个.war文件并在tomcat中运行它。我实现了在intellij中运行它,但它不是在tomcat中运行的。

复制

我在下面解释如何重现我的问题。如果我遗漏了任何相关内容,请写一条评论。

如何创建项目?

在上生成项目https://start.spring.io/ 使用以下选项

点击按钮 Generate . 档案 demo.zip 已下载。将其解压缩为目录 demo . 它应包含以下文件:

├── HELP.md
├── demo.iml
├── mvnw
├── mvnw.cmd
├── pom.xml
└── src
    ├── main
    │   ├── java
    │   │   └── com
    │   │       └── example
    │   │           └── demo
    │   │               ├── DemoApplication.java
    │   │               └── ServletInitializer.java
    │   └── resources
    │       ├── application.properties
    │       ├── static
    │       └── templates
    └── test
        └── java
            └── com
                └── example
                    └── demo
                        └── DemoApplicationTests.java

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 https://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.5.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <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-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

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

</project>

src/main/java/com/example/demo/demoapplication.java

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

src/main/java/com/example/demo/servletilizer.java

package com.example.demo;

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

public class ServletInitializer extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(DemoApplication.class);
    }

}
``` `src/main/resources/application.properties` 是空的
添加文件 `src/main/java/com/example/demo/HelloWorldController.java` 心满意足

package com.example.demo;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloWorldController {
@RequestMapping("/")
public String index() {
return "Greetings from Spring Boot!";
}
}


#### 如何在intellij中运行项目?

在intellij中打开并加载maven项目。在菜单中选择 `Run` 及 `Run 'DemoApplication'` . 您应该在控制台中看到如下内容:

. ____ _ __ _ _
/\ / ' __ _ () __ __ _ \ \ \
( ( )_
_ | '_ | '| | ' / ` | \ \ \
\/ __)| |)| | | | | || (
| | ) ) ) )
' || .__|| ||| |_, | / / / /
=========|
|==============|
/=////
:: Spring Boot :: (v2.5.2)

2021-07-09 17:31:07.447 INFO 7245 --- [ main] com.example.demo.DemoApplication : Starting DemoApplication using Java 1.8.0_292 on MY_HOST with PID 7245 (/path/to/demo/target/classes started by USER in /path/to/demo)
2021-07-09 17:31:07.451 INFO 7245 --- [ main] com.example.demo.DemoApplication : No active profile set, falling back to default profiles: default
2021-07-09 17:31:08.636 INFO 7245 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2021-07-09 17:31:08.647 INFO 7245 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2021-07-09 17:31:08.648 INFO 7245 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.48]
2021-07-09 17:31:08.720 INFO 7245 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2021-07-09 17:31:08.720 INFO 7245 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1205 ms
2021-07-09 17:31:09.214 INFO 7245 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2021-07-09 17:31:09.229 INFO 7245 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 2.89 seconds (JVM running for 3.783)

打开http://localhost:8080/ 在浏览器中。你应该看到

Greetings from Spring Boot!


#### 如何在intellij中构建.war?

挑选 `View > Tool Windows > Maven` 在菜单中,单击“运行生命周期” `clean` 及 `package` ![](https://i.stack.imgur.com/mHvuC.png)
在其他文件中,有一个 `target/demo-0.0.1-SNAPSHOT.war` 是创建的。

#### 如何在tomcat中运行项目?

注意:我在下面使用docker,以使环境尽可能重现。然而,我也尝试过用brew将tomcat安装在另一台mac上,但也遇到了同样的问题。
在终端中运行(在项目根目录中)

docker run --rm -p 8081:8080
-v ${PWD}/target/demo-0.0.1-SNAPSHOT.war:/usr/local/tomcat/webapps/demo.war
tomcat:10-jdk8

您应该获得类似以下内容的输出:

09-Jul-2021 16:44:19.312 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version name: Apache Tomcat/10.0.8
09-Jul-2021 16:44:19.315 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built: Jun 25 2021 23:05:41 UTC
09-Jul-2021 16:44:19.315 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version number: 10.0.8.0
09-Jul-2021 16:44:19.315 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name: Linux
09-Jul-2021 16:44:19.315 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Version: 5.10.25-linuxkit
09-Jul-2021 16:44:19.315 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Architecture: amd64
09-Jul-2021 16:44:19.316 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Java Home: /usr/local/openjdk-8/jre
09-Jul-2021 16:44:19.316 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Version: 1.8.0_292-b10
09-Jul-2021 16:44:19.316 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Vendor: Oracle Corporation
09-Jul-2021 16:44:19.316 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_BASE: /usr/local/tomcat
09-Jul-2021 16:44:19.316 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME: /usr/local/tomcat
09-Jul-2021 16:44:19.320 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties
09-Jul-2021 16:44:19.320 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
09-Jul-2021 16:44:19.320 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djdk.tls.ephemeralDHKeySize=2048
09-Jul-2021 16:44:19.320 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.protocol.handler.pkgs=org.apache.catalina.webresources
09-Jul-2021 16:44:19.321 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dorg.apache.catalina.security.SecurityListener.UMASK=0027
09-Jul-2021 16:44:19.321 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dignore.endorsed.dirs=
09-Jul-2021 16:44:19.321 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.base=/usr/local/tomcat
09-Jul-2021 16:44:19.321 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.home=/usr/local/tomcat
09-Jul-2021 16:44:19.321 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.io.tmpdir=/usr/local/tomcat/temp
09-Jul-2021 16:44:19.335 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent Loaded Apache Tomcat Native library [1.2.30] using APR version [1.6.5].
09-Jul-2021 16:44:19.335 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true], UDS [true].
09-Jul-2021 16:44:19.339 INFO [main] org.apache.catalina.core.AprLifecycleListener.initializeSSL OpenSSL successfully initialized [OpenSSL 1.1.1d 10 Sep 2019]
09-Jul-2021 16:44:19.821 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-nio-8080"]
09-Jul-2021 16:44:19.844 INFO [main] org.apache.catalina.startup.Catalina.load Server initialization in [769] milliseconds
09-Jul-2021 16:44:19.898 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service [Catalina]
09-Jul-2021 16:44:19.898 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet engine: [Apache Tomcat/10.0.8]
09-Jul-2021 16:44:19.945 INFO [main] org.apache.catalina.startup.HostConfig.deployWAR Deploying web application archive [/usr/local/tomcat/webapps/demo.war]
09-Jul-2021 16:44:21.248 INFO [main] org.apache.jasper.servlet.TldScanner.scanJars 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.
09-Jul-2021 16:44:21.315 INFO [main] org.apache.catalina.startup.HostConfig.deployWAR Deployment of web application archive [/usr/local/tomcat/webapps/demo.war] has finished in [1,369] ms
09-Jul-2021 16:44:21.321 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"]
09-Jul-2021 16:44:21.339 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in [1494] milliseconds

注意:我希望我的应用程序的日志,即。

. ____ _ __ _ _
/\ / ' __ _ () __ __ _ \ \ \
( ( )_
_ | '_ | '| | ' / ` | \ \ \
\/ __)| |)| | | | | || (
| | ) ) ) )
' || .__|| ||| |_, | / / / /
=========|
|==============|
/=////
:: Spring Boot :: (v2.5.2)

...

也出现在这里,但它不见了。
打开http://localhost:8081/demo/ 在浏览器中。我希望看到

Greetings from Spring Boot!

但我明白了
![](https://i.stack.imgur.com/qmFht.png)
我的项目或tomcat配置/代码中缺少什么?如何在tomcat中以.war的形式运行我的spring boot应用程序?
fnx2tebb

fnx2tebb1#

正如评论中所指出的,运行Tomcat9而不是10是可行的

docker run --rm -p 8081:8080 \
  -v ${PWD}/target/demo-0.0.1-SNAPSHOT.war:/usr/local/tomcat/webapps/demo.war \
  tomcat:9-jdk8

相关问题