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

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

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

问题

  1. 我正在尝试将一个REST示例部署到外部的Tomcat中。然而,它没有显示任何错误,并且Spring也没有启动。根据Spring文档,我遵循了三个步骤:
  2. 1. pom.xml中将包打包为war
  3. 2. pom.xml中为spring-boot-starter-tomcat添加`<scope>provided</scope>`
  4. 3. 将主类扩展为SpringBootServletInitializer
  5. 当我将这些内容放在Tomcatwebapp文件夹中,然后运行"catalina.bat run"时,它会显示应用程序已成功部署,但无论我尝试访问哪个URL,都不会执行REST方法。
  6. ```java
  7. @SpringBootApplication
  8. @RestController
  9. public class HelloWorldApplication extends SpringBootServletInitializer
  10. {
  11. @Override
  12. protected SpringApplicationBuilder configure(SpringApplicationBuilder application)
  13. {
  14. return application.sources(HelloWorldApplication.class);
  15. }
  16. public static void main(String[] args)
  17. {
  18. SpringApplication.run(HelloWorldApplication.class, args);
  19. }
  20. @RequestMapping(value = "/hello")
  21. public String helloWorld()
  22. {
  23. return "Hello World, Peter";
  24. }
  25. }

在pom.xml中:

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-tomcat</artifactId>
  4. <scope>provided</scope>
  5. </dependency>
  6. <groupId>com.example</groupId>
  7. <artifactId>HelloWorld</artifactId>
  8. <version>0.0.1-SNAPSHOT</version>
  9. <name>HelloWorld</name>
  10. <description>Demo project for Spring Boot</description>
  11. <packaging>war</packaging>

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

  1. server.servlet.context-path=/sample

Tomcat日志:

  1. 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]
  2. 15-Mar-2020 23:06:48.102 INFO [main] org.apache.jasper.servlet.TldScanner.scanJars 至少有一个JAR被扫描以查找TLD文件,但是其中不包含TLD文件。启用该记录器的调试日志可以获取扫描过的所有JAR文件的完整列表,但是在扫描它们时没有找到TLD文件。在扫描时跳过不需要的JAR文件可以改善启动时间和JSP编译时间。
  3. 15-Mar-2020 23:06:48.409 WARNING [main] org.apache.catalina.util.SessionIdGeneratorBase.createSecureRandom 为会话ID生成创建SecureRandom实例时使用[SHA1PRNG]花费了[270]毫秒。
  4. 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]。

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

  1. localhost:8080/HelloWorld-0.0.1-SNAPSHOT
  2. localhost:8080/HelloWorld-0.0.1-SNAPSHOT/hello
  3. 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

  1. @SpringBootApplication
  2. @RestController
  3. public class HelloWorldApplication extends SpringBootServletInitializer
  4. {
  5. @Override
  6. protected SpringApplicationBuilder configure(SpringApplicationBuilder application)
  7. {
  8. return application.sources(HelloWorldApplication.class);
  9. }
  10. public static void main(String[] args)
  11. {
  12. SpringApplication.run(HelloWorldApplication.class, args);
  13. }
  14. @RequestMapping(value = &quot;/hello&quot;)
  15. public String helloWorld()
  16. {
  17. return &quot;Hello World, Peter&quot;;
  18. }
  19. }

and in pom.xml

  1. &lt;dependency&gt;
  2. &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
  3. &lt;artifactId&gt;spring-boot-starter-tomcat&lt;/artifactId&gt;
  4. &lt;scope&gt;provided&lt;/scope&gt;
  5. &lt;/dependency&gt;
  6. &lt;groupId&gt;com.example&lt;/groupId&gt;
  7. &lt;artifactId&gt;HelloWorld&lt;/artifactId&gt;
  8. &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
  9. &lt;name&gt;HelloWorld&lt;/name&gt;
  10. &lt;description&gt;Demo project for Spring Boot&lt;/description&gt;
  11. &lt;packaging&gt;war&lt;/packaging&gt;

and application.properties is like

  1. server.servlet.context-path=/sample

Tomcat logs:

  1. 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]
  2. 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.
  3. 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.
  4. 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?

  1. localhost:8080/HelloWorld-0.0.1-SNAPSHOT
  2. localhost:8080/HelloWorld-0.0.1-SNAPSHOT/hello
  3. 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

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

  1. @RequestMapping(value = "/hello")
  2. public String helloWorld()
  3. {
  4. return "Hello World, Peter";
  5. }

这意味着你只能访问 "/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

  1. @RequestMapping(value = &quot;/hello&quot;)
  2. public String helloWorld()
  3. {
  4. return &quot;Hello World, Peter&quot;;
  5. }

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 文件中:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>it.group.id</groupId>
  7. <artifactId>artifactId</artifactId>
  8. <version>1.0-SNAPSHOT</version>
  9. <packaging>war</packaging>
  10. <parent>
  11. <groupId>org.springframework.boot</groupId>
  12. <artifactId>spring-boot-starter-parent</artifactId>
  13. <version>2.1.5.RELEASE</version>
  14. <relativePath/> <!-- lookup parent fdaotory -->
  15. </parent>
  16. <properties>
  17. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  18. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  19. <java.version>1.8</java.version>
  20. </properties>
  21. <dependencies>
  22. <!--For Build War File-->
  23. <dependency>
  24. <groupId>org.springframework.boot</groupId>
  25. <artifactId>spring-boot-starter-tomcat</artifactId>
  26. <scope>provided</scope>
  27. </dependency>
  28. </dependencies>
  29. <build>
  30. <!--DEV-->
  31. <finalName>app-name</finalName>
  32. <plugins>
  33. <plugin>
  34. <groupId>org.springframework.boot</groupId>
  35. <artifactId>spring-boot-maven-plugin</artifactId>
  36. </plugin>
  37. <!--For Build War File-->
  38. <plugin>
  39. <artifactId>maven-war-plugin</artifactId>
  40. <version>3.2.2</version>
  41. <configuration>
  42. <failOnMissingWebXml>false</failOnMissingWebXml>
  43. </configuration>
  44. </plugin>
  45. </plugins>
  46. </build>
  47. </project>
英文:

try this way

> localhost:8080/app-name/hello

In your pom.xml

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  2. &lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot;
  3. xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
  4. xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
  5. &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  6. &lt;groupId&gt;it.group.id&lt;/groupId&gt;
  7. &lt;artifactId&gt;artifactId&lt;/artifactId&gt;
  8. &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
  9. &lt;packaging&gt;war&lt;/packaging&gt;
  10. &lt;parent&gt;
  11. &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
  12. &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
  13. &lt;version&gt;2.1.5.RELEASE&lt;/version&gt;
  14. &lt;relativePath/&gt; &lt;!-- lookup parent fdaotory --&gt;
  15. &lt;/parent&gt;
  16. &lt;properties&gt;
  17. &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
  18. &lt;project.reporting.outputEncoding&gt;UTF-8&lt;/project.reporting.outputEncoding&gt;
  19. &lt;java.version&gt;1.8&lt;/java.version&gt;
  20. &lt;/properties&gt;
  21. &lt;dependencies&gt;
  22. &lt;!--For Build War File--&gt;
  23. &lt;dependency&gt;
  24. &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
  25. &lt;artifactId&gt;spring-boot-starter-tomcat&lt;/artifactId&gt;
  26. &lt;scope&gt;provided&lt;/scope&gt;
  27. &lt;/dependency&gt;
  28. &lt;/dependencies&gt;
  29. &lt;build&gt;
  30. &lt;!--DEV--&gt;
  31. &lt;finalName&gt;app-name&lt;/finalName&gt;
  32. &lt;plugins&gt;
  33. &lt;plugin&gt;
  34. &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
  35. &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
  36. &lt;/plugin&gt;
  37. &lt;!--For Build War File--&gt;
  38. &lt;plugin&gt;
  39. &lt;artifactId&gt;maven-war-plugin&lt;/artifactId&gt;
  40. &lt;version&gt;3.2.2&lt;/version&gt;
  41. &lt;configuration&gt;
  42. &lt;failOnMissingWebXml&gt;false&lt;/failOnMissingWebXml&gt;
  43. &lt;/configuration&gt;
  44. &lt;/plugin&gt;
  45. &lt;/plugins&gt;
  46. &lt;/build&gt;
  47. &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:

确定