英文:
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
- Packages as war inside pom.xml
<scope>provided</scope>
to spring-boot-starter-tomcat in pom.xml- 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 = "/hello")
public String helloWorld()
{
return "Hello World, Peter";
}
}
and in 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>
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 <java.version>1.8</java.version>
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
- 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).
- Copy war :copy your war file into /webapps folder inside your tomcat folder.
- Start server: to start the server go to /bin folder then execute this commands startup.sh for Linux startup.bat for windows.
- 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
- 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).
- Copy war :copy your war file into /webapps folder inside your tomcat folder.
- Start server: to start the server go to /bin folder then execute this commands startup.sh for Linux startup.bat for windows.
- 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 = "/hello")
public String helloWorld()
{
return "Hello World, Peter";
}
Which means the only thing you can access is "/hello"
.
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
<?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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论