
第一段引用上面的摘要:
本文旨在帮助开发者解决在 IntelliJ IDEA 中运行 Spring Boot HelloWorld 应用时遇到的端口占用问题。我们将分析错误信息,提供排查端口占用进程的方法,并给出修改端口配置的解决方案,确保应用能够成功启动。
当您尝试在 IntelliJ IDEA 中运行 Spring Boot HelloWorld 应用时,可能会遇到类似以下错误信息:
*************************** APPLICATION FAILED TO START *************************** Description: Web server failed to start. Port 8080 was already in use. Action: Identify and stop the process that's listening on port 8080 or configure this application to listen on another port. Process finished with exit code 1
这表明默认的 8080 端口已被其他进程占用,导致 Spring Boot 应用无法正常启动。以下提供几种排查和解决此问题的方法。
虽然错误信息提示端口 8080 被占用,但有时使用 lsof -i :8080 命令可能无法找到占用该端口的进程。这可能是因为:
如果无法确定哪个进程占用了 8080 端口,或者您希望避免端口冲突,最简单的解决方案是修改 Spring Boot 应用的端口配置。
打开 application.properties 或 application.yml 文件: 该文件位于 src/main/resources 目录下。如果文件不存在,请手动创建。
添加或修改 server.port 属性: 在文件中添加以下行,将端口修改为未被占用的端口,例如 8089:
server.port=8089
或者,如果使用 YAML 格式:
server: port: 8089
重新启动应用程序: 保存文件后,重新启动 Spring Boot 应用。此时,应用将尝试在 8089 端口上启动。
以下是一个简单的 Spring Boot HelloWorld 应用示例,其中包含修改端口配置的步骤:
package com.example.springbootexample;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
public class SpringBootExampleApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootExampleApplication.class, args);
}
}
@RestController
class HelloController {
@GetMapping("/hello")
public String hello() {
return "Hello, World!";
}
}application.properties (修改端口)
server.port=8089
注意事项:
端口占用是 Spring Boot 开发中常见的问题。通过排查端口占用进程或修改端口配置,您可以轻松解决此问题,确保您的应用程序能够顺利启动并运行。建议在开发过程中养成良好的习惯,避免同时运行多个占用相同端口的应用程序,并及时关闭不再需要的应用程序。
以上就是Spring Boot HelloWorld 启动失败:端口占用问题排查与解决的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号