工具:
我只是使用 Spring Assistant 创建了一个演示项目,并打开了 Spring Web
依赖项,pom.xml
也有
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
当我运行 DemoApplication.java
时,应用程序在启动后立即关闭,不打印任何有关服务器或端口的信息:
2021-03-18 09:17:38.398 INFO 8553 --- [ main] com.example.demo.DemoApplication : Starting DemoApplication using Java 15.0.2 on Summoring-MBP.local with PID 8553 (/Users/Smeegol/Git/demo2/target/classes started by Smeegol in /Users/Smeegol/Git/demo2)
2021-03-18 09:17:38.403 INFO 8553 --- [ main] com.example.demo.DemoApplication : No active profile set, falling back to default profiles: default
2021-03-18 09:17:40.286 INFO 8553 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 3.424 seconds (JVM running for 4.374)
Process finished with exit code 0
如何在应用程序启动后让Web服务器运行并监听8080之类的端口?谢谢。
源代码可以从这里下载:https://github.com/Smeegol/SpringBootDemo/blob/main/demo.zip
答案 0 :(得分:1)
您的代码有效。我没有 Java 15,我已将其更改为使用 11 并运行。
这是我得到的:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.4.3)
2021-03-17 23:15:01.495 INFO 34616 --- [ main] com.example.demo.DemoApplication : Starting DemoApplication using Java 11.0.3 on machine1.domain.com with PID 34616 (/Users/test/Downloads/demo 5/out/production/demo started by test in /Users/test/Downloads/demo 5)
2021-03-17 23:15:01.497 INFO 34616 --- [ main] com.example.demo.DemoApplication : No active profile set, falling back to default profiles: default
2021-03-17 23:15:02.192 INFO 34616 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2021-03-17 23:15:02.199 INFO 34616 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2021-03-17 23:15:02.199 INFO 34616 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.43]
2021-03-17 23:15:02.251 INFO 34616 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2021-03-17 23:15:02.251 INFO 34616 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 711 ms
2021-03-17 23:15:02.362 INFO 34616 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2021-03-17 23:15:02.495 INFO 34616 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2021-03-17 23:15:02.508 INFO 34616 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 1.249 seconds (JVM running for 1.977)
JDK 15 可能“太新”了。如果您只是在试验,请将 JDK 降级到较低的版本。 Java 11 有长期支持。
答案 1 :(得分:0)
这发生在你的源代码中没有任何 API 被监听时,这意味着你的源代码中没有任何标有 @RestController
或 @Controller
注释的类,此时 SpringBoot 只是只需构建源代码运行并退出而不返回任何错误消息