Spring Boot应用无法在外部Tomcat中启动(在Tomcat 8.5和10中均尝试过)。

huangapple go评论83阅读模式
英文:

Spring Boot application is not starting in external Tomcat (tried both in Tomcat 8.5 and 10)

问题

我正在尝试将一个REST示例部署到外部的Tomcat中。然而,它没有显示任何错误,并且Spring也没有启动。根据Spring文档,我遵循了三个步骤:

1. 在pom.xml中将包打包为war。
2. 在pom.xml中为spring-boot-starter-tomcat添加`<scope>provided</scope>`。
3. 将主类扩展为SpringBootServletInitializer。

当我将这些内容放在Tomcat的webapp文件夹中,然后运行"catalina.bat run"时,它会显示应用程序已成功部署,但无论我尝试访问哪个URL,都不会执行REST方法。

```java
@SpringBootApplication
@RestController
public class HelloWorldApplication extends SpringBootServletInitializer
{
	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder application)
	{
		return application.sources(HelloWorldApplication.class);
	}
	public static void main(String[] args)
	{
		SpringApplication.run(HelloWorldApplication.class, args);
	}
	@RequestMapping(value = "/hello")
	public String helloWorld()
	{
		return "Hello World, Peter";
	}
}

在pom.xml中:

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

<groupId>com.example</groupId>
<artifactId>HelloWorld</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>HelloWorld</name>
<description>Demo project for Spring Boot</description>
<packaging>war</packaging>

以及application.properties文件中的内容为:

server.servlet.context-path=/sample

Tomcat日志:

15-Mar-2020 23:06:46.219 INFO [main] org.apache.catalina.startup.HostConfig.deployWAR Deploying web application archive [C:\softwares\apache-tomcat-10.0.0-M1\webapps\HelloWorld-0.0.1-SNAPSHOT.war]
15-Mar-2020 23:06:48.102 INFO [main] org.apache.jasper.servlet.TldScanner.scanJars 至少有一个JAR被扫描以查找TLD文件,但是其中不包含TLD文件。启用该记录器的调试日志可以获取扫描过的所有JAR文件的完整列表,但是在扫描它们时没有找到TLD文件。在扫描时跳过不需要的JAR文件可以改善启动时间和JSP编译时间。
15-Mar-2020 23:06:48.409 WARNING [main] org.apache.catalina.util.SessionIdGeneratorBase.createSecureRandom 为会话ID生成创建SecureRandom实例时使用[SHA1PRNG]花费了[270]毫秒。
15-Mar-2020 23:06:48.440 INFO [main] org.apache.catalina.startup.HostConfig.deployWAR 已在[2,221]毫秒内部署完Web应用程序归档文件[C:\softwares\apache-tomcat-10.0.0-M1\webapps\HelloWorld-0.0.1-SNAPSHOT.war]。

我尝试了以下几种访问方式,但都没有成功。能否有人指点我正确的方向?

localhost:8080/HelloWorld-0.0.1-SNAPSHOT
localhost:8080/HelloWorld-0.0.1-SNAPSHOT/hello
localhost:8080/HelloWorld-0.0.1-SNAPSHOT/HelloWorldApplication/helloWorld
英文:

I am trying to deploy a rest sample in external tomcat. However, it does not give any error and Spring does not start also. According to Spring documentation I followed three steps

  1. Packages as war inside pom.xml
  2. &lt;scope&gt;provided&lt;/scope&gt; to spring-boot-starter-tomcat in pom.xml
  3. Extending main class with SpringBootServletInitializer

When I place this in Tomcat's webapp folder and run "catalina.bat run" it says application deployed successfully, but whatever URL I try to access, it does not execute the rest method

@SpringBootApplication
@RestController
public class HelloWorldApplication extends SpringBootServletInitializer
{
	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder application)
	{
		return application.sources(HelloWorldApplication.class);
	}
	public static void main(String[] args)
	{
		SpringApplication.run(HelloWorldApplication.class, args);
	}
	@RequestMapping(value = &quot;/hello&quot;)
	public String helloWorld()
	{
		return &quot;Hello World, Peter&quot;;
	}
}

and in pom.xml

&lt;dependency&gt;
    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
    &lt;artifactId&gt;spring-boot-starter-tomcat&lt;/artifactId&gt;
    &lt;scope&gt;provided&lt;/scope&gt;
&lt;/dependency&gt;

        
&lt;groupId&gt;com.example&lt;/groupId&gt;
	&lt;artifactId&gt;HelloWorld&lt;/artifactId&gt;
	&lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
	&lt;name&gt;HelloWorld&lt;/name&gt;
	&lt;description&gt;Demo project for Spring Boot&lt;/description&gt;
	&lt;packaging&gt;war&lt;/packaging&gt;

and application.properties is like

server.servlet.context-path=/sample

Tomcat logs:

    15-Mar-2020 23:06:46.219 INFO [main] org.apache.catalina.startup.HostConfig.deployWAR Deploying web application archive [C:\softwares\apache-tomcat-10.0.0-M1\webapps\HelloWorld-0.0.1-SNAPSHOT.war]
    15-Mar-2020 23:06:48.102 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.
    15-Mar-2020 23:06:48.409 WARNING [main] org.apache.catalina.util.SessionIdGeneratorBase.createSecureRandom Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [270] milliseconds.
    15-Mar-2020 23:06:48.440 INFO [main] org.apache.catalina.startup.HostConfig.deployWAR Deployment of web application archive [C:\softwares\apache-tomcat-10.0.0-M1\webapps\HelloWorld-0.0.1-SNAPSHOT.war] has finished in [2,221] ms

I tried accessing in below ways.but none of them worked.Could any one points me in right direction?

localhost:8080/HelloWorld-0.0.1-SNAPSHOT
localhost:8080/HelloWorld-0.0.1-SNAPSHOT/hello
localhost:8080/HelloWorld-0.0.1-SNAPSHOT/HelloWorldApplication/helloWorld

答案1

得分: 2

确保您在您的pom.xml中设置的Java版本<java.version>1.8</java.version>与Tomcat正在运行的Java版本(即JAVA_HOME环境变量指向的Java版本)相同。

英文:

Make sure that the java version you set in your pom.xml &lt;java.version&gt;1.8&lt;/java.version&gt; and the java version using which the tomcat is running (version of java to which JAVA_HOME env variable is pointing) are same.

答案2

得分: 2

follow this steps it may helps you

  1. Build the war file : go to spring project directory there you do mvn clean install (make sure you have installed maven and set to environmental variable).
  2. Copy war :copy your war file into /webapps folder inside your tomcat folder.
  3. Start server: to start the server go to /bin folder then execute this commands startup.sh for Linux startup.bat for windows.
  4. Hit Url: Once the server started, open your browser the type localhost:{"portNumber"}/{warFileName}/{yourPath} in your case its look like this http://localhost:8080/{warfilename}/hello
英文:

follow this steps it may helps you

  1. Build the war file : go to spring project directory there you do mvn clean install (make sure you have installed maven and set to environmental variable).
  2. Copy war :copy your war file into /webapps folder inside your tomcat folder.
  3. Start server: to start the server go to /bin folder then execute this commands startup.sh for Linux startup.bat for windows.
  4. Hit Url: Once the server started, open your browser the type localhost:{"portNumber"}/{warFileName}/{yourPath} in your case its look like this http://localhost:8080/{warfilename}/hello

答案3

得分: 1

你的路径已在这些函数中设置:

@RequestMapping(value = "/hello")
public String helloWorld()
{
    return "Hello World, Peter";
}

这意味着你只能访问 "/hello"
访问 localhost:8080/HelloWorld-0.0.1-SNAPSHOT 不会起作用,你必须访问 localhost:8080/${context-path}/${your-path},在这种情况下是 localhost:8080/sample/hello

英文:

Your path is set up in this functions

@RequestMapping(value = &quot;/hello&quot;)
public String helloWorld()
{
    return &quot;Hello World, Peter&quot;;
}

Which means the only thing you can access is &quot;/hello&quot;.
Accessing to localhost:8080/HelloWorld-0.0.1-SNAPSHOT will not working, you have to access for localhost:8080/${context-path}/${your-path} in this case localhost:8080/sample/hello

答案4

得分: 0

尝试以下方法:

localhost:8080/app-name/hello

在您的 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>it.group.id</groupId>
    <artifactId>artifactId</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.5.RELEASE</version>
        <relativePath/> <!-- lookup parent fdaotory -->
    </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>

        <!--For Build War File-->
        <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-tomcat</artifactId>
             <scope>provided</scope>
        </dependency>

    </dependencies>
    <build>
        <!--DEV-->
        <finalName>app-name</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

            <!--For Build War File-->
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.2.2</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>

        </plugins>

    </build>

</project>
英文:

try this way

> localhost:8080/app-name/hello

In your pom.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot;
         xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
         xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;

    &lt;groupId&gt;it.group.id&lt;/groupId&gt;
    &lt;artifactId&gt;artifactId&lt;/artifactId&gt;
    &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
    &lt;packaging&gt;war&lt;/packaging&gt;
    &lt;parent&gt;
        &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
        &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
        &lt;version&gt;2.1.5.RELEASE&lt;/version&gt;
        &lt;relativePath/&gt; &lt;!-- lookup parent fdaotory --&gt;
    &lt;/parent&gt;
    &lt;properties&gt;
        &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
        &lt;project.reporting.outputEncoding&gt;UTF-8&lt;/project.reporting.outputEncoding&gt;
        &lt;java.version&gt;1.8&lt;/java.version&gt;
    &lt;/properties&gt;
    &lt;dependencies&gt;

        &lt;!--For Build War File--&gt;
        &lt;dependency&gt;
             &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
             &lt;artifactId&gt;spring-boot-starter-tomcat&lt;/artifactId&gt;
             &lt;scope&gt;provided&lt;/scope&gt;
        &lt;/dependency&gt;



    &lt;/dependencies&gt;
    &lt;build&gt;
        &lt;!--DEV--&gt;
        &lt;finalName&gt;app-name&lt;/finalName&gt;
        &lt;plugins&gt;
            &lt;plugin&gt;
                &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
                &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
            &lt;/plugin&gt;

            &lt;!--For Build War File--&gt;
            &lt;plugin&gt;
                &lt;artifactId&gt;maven-war-plugin&lt;/artifactId&gt;
                &lt;version&gt;3.2.2&lt;/version&gt;
                &lt;configuration&gt;
                    &lt;failOnMissingWebXml&gt;false&lt;/failOnMissingWebXml&gt;
                &lt;/configuration&gt;
            &lt;/plugin&gt;

        &lt;/plugins&gt;

    &lt;/build&gt;

&lt;/project&gt;

huangapple
  • 本文由 发表于 2020年3月16日 11:29:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/60699994.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定