Spring Boot HelloWorld 启动失败:端口占用问题排查与解决(排查.端口.占用.失败.启动...)

wufei123 发布于 2025-09-24 阅读(13)

spring boot helloworld 启动失败:端口占用问题排查与解决

第一段引用上面的摘要:

本文旨在帮助开发者解决在 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 命令可能无法找到占用该端口的进程。这可能是因为:

  • 应用程序已经在 IntelliJ IDEA 中运行: 检查 IntelliJ IDEA 的 "Services" 标签页。如果您的 Spring Boot 应用已经在运行,即使控制台没有显示任何输出,它也可能正在占用 8080 端口。停止该应用即可。
  • 其他应用程序占用了端口: 尽管 lsof 没有显示,但其他应用程序可能在后台运行并占用了端口。尝试重启计算机,这通常可以释放被占用的端口。
解决方案:修改端口配置

如果无法确定哪个进程占用了 8080 端口,或者您希望避免端口冲突,最简单的解决方案是修改 Spring Boot 应用的端口配置。

  1. 打开 application.properties 或 application.yml 文件: 该文件位于 src/main/resources 目录下。如果文件不存在,请手动创建。

  2. 添加或修改 server.port 属性: 在文件中添加以下行,将端口修改为未被占用的端口,例如 8089:

    HyperWrite HyperWrite

    AI写作助手帮助你创作内容更自信

    HyperWrite54 查看详情 HyperWrite
    server.port=8089

    或者,如果使用 YAML 格式:

    server:
      port: 8089
  3. 重新启动应用程序: 保存文件后,重新启动 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

注意事项:

  • 确保选择一个未被其他应用程序占用的端口。通常,8081、8082、8089 等端口都可以使用。
  • 如果您正在使用防火墙,请确保允许 Spring Boot 应用使用的端口通过防火墙。
总结

端口占用是 Spring Boot 开发中常见的问题。通过排查端口占用进程或修改端口配置,您可以轻松解决此问题,确保您的应用程序能够顺利启动并运行。建议在开发过程中养成良好的习惯,避免同时运行多个占用相同端口的应用程序,并及时关闭不再需要的应用程序。

以上就是Spring Boot HelloWorld 启动失败:端口占用问题排查与解决的详细内容,更多请关注知识资源分享宝库其它相关文章!

相关标签: idea 计算机 防火墙 app 端口 ai springboot intellij idea spring spring boot idea intellij idea 大家都在看: 在IDEA中复制粘贴包后为什么会找不到主类?有什么解决方法? 在IDEA中复制粘贴包后为什么会找不到主类? IDEA控制台日志打印空格问题如何解决? 在Idea中如何设置SpringBoot项目默认运行配置列表以便团队成员共享? 如何在Idea中配置SpringBoot项目运行配置,使其在克隆项目时自动显示?

标签:  排查 端口 占用 

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。