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

运行Spring Boot项目访问localhost:8080出现Whitelabel Error Page错误如何解决

问题原因

  • 路径映射不匹配:你当前Controller仅配置了/sum路径的访问规则,没有为根路径/配置映射,直接访问localhost:8080就会触发Spring Boot默认的Whitelabel错误页。
  • 多余无效导入:Controller类中导入的org.springframework.boot.autoconfigure.SpringBootApplication没有实际作用,可以直接删除。

解决方案

  1. 测试现有接口:直接访问http://localhost:8080/sum,即可正常得到返回结果100。
  2. 配置根路径访问:如果需要localhost:8080直接可访问,在Controller中新增根路径映射即可,参考修改代码:
package com.springFramework.helloGame.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class Controller {
    // 新增根路径映射
    @GetMapping("/")
    public String rootPage() {
        return "项目启动成功";
    }

    @GetMapping("/sum")
    public long displaySum(){
        return 100;
    }
}

注:你的启动类HelloGameApplication的位置是正确的,@SpringBootApplication默认会扫描所在包及其所有子包的组件,Controller所在的controller子包可以被正常扫描到,不需要额外添加@ComponentScan配置。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.04 02:24:03