You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SpringBoot应用初始化后即停止,添加等待函数后API无响应求助

问题排查:SpringBoot应用初始化后立即停止,添加阻塞逻辑后API无响应

问题现象

  • SpringBoot应用初始化完成后直接退出,无任何错误日志
  • 手动实现waitForTermination()阻塞主线程后,应用不再退出,但所有API请求均无响应
  • 进程列表中存在本地TCP连接:

TCP 127.0.0.1:53768 127.0.0.1:53767 ESTABLISHED 11584

核心原因分析

问题本质是缺少Web容器依赖:

  • 当前依赖仅引入spring-boot-starter(基础启动器,不包含嵌入式Web服务器)和spring-web(Web核心API,但无服务器实现)
  • 没有spring-boot-starter-web时,SpringBoot默认不会启动Tomcat/Jetty等嵌入式服务器,容器初始化完成后就会终止进程
  • 手动添加的waitForTermination()只是阻塞了主线程,但Web容器根本没启动,自然无法处理API请求

解决方案

1. 修正build.gradle依赖配置

取消注释spring-boot-starter-web,并统一Spring Boot版本,避免版本冲突:

plugins {
    id 'java'
    id "org.springframework.boot" version "3.1.0"
}

group = 'org.example'
version = '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    testImplementation platform('org.junit:junit-bom:5.9.1')
    testImplementation 'org.junit.jupiter:junit-jupiter'
    implementation files('src/NOVASDK/SDK/NovaSdk.jar')
    implementation files('src/Libs/gson-2.2.2.jar/gson-2.2.2.jar');

    // 移除单独的spring-web,由spring-boot-starter-web自动引入适配版本
    // implementation('org.springframework:spring-web:6.0.9')

    // 使用与插件一致的3.1.0依赖平台,避免版本混乱
    implementation platform('org.springframework.boot:spring-boot-dependencies:3.1.0')

    implementation 'org.springframework.boot:spring-boot-starter'
    // 启用Web启动器,自动包含嵌入式Tomcat及完整Web依赖
    implementation 'org.springframework.boot:spring-boot-starter-web'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
}

test {
    useJUnitPlatform()
}

2. 移除手动阻塞代码

删除waitForTermination()方法及main方法中的调用,SpringBoot的run()方法会自动维护Web容器的生命周期:

public class Main {
    public static void main(String[] args) {
        NovaOpt novaOpt = NovaOpt.GetInstance();

        SpringApplication springApplication = new SpringApplication(Main.class);
        springApplication.setDefaultProperties(Collections.singletonMap("server.port", "8090"));
        springApplication.run(args);
    }
}

3. 修正路由注解不匹配问题

路由/GetScreenBrightness/{screenIp}使用了路径变量语法,但参数标注为@RequestParam,两者不兼容,需二选一调整:

  • 改为路径变量模式:
@GetMapping("/GetScreenBrightness/{screenIp}")
public String getScreenBrightness(@PathVariable("screenIp") String screenIp) {
    String username = System.getenv("USERNAME");
    String password = System.getenv("PASSWORD");
    System.out.printf("Username:" + username);
    return "Hello";
}
  • 或保持请求参数模式:
@GetMapping("/GetScreenBrightness")
public String getScreenBrightness(@RequestParam("screenIp") String screenIp) {
    String username = System.getenv("USERNAME");
    String password = System.getenv("PASSWORD");
    System.out.printf("Username:" + username);
    return "Hello";
}

验证步骤

  1. 执行./gradlew clean build重新构建项目
  2. 启动应用,查看控制台是否输出Tomcat启动日志(如Tomcat started on port 8090 (http) with context path '')
  3. 访问http://localhost:8090/api/hello,确认返回Hello, World!
  4. 访问调整后的亮度接口,验证功能正常

内容的提问来源于stack exchange,提问作者Idan Zehavi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 23:23:18