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

使用Gradle搭建Spring Boot REST服务时路径访问异常求助

问题排查与解决

Hey there! Let's break down why you're seeing that Whitelabel Error Page even though your build and startup look totally normal.

First, take a close look at your startup logs—you’ll notice there are only mappings for the default /error endpoints and static resources, no entries for any custom controllers you’ve written. That’s the core issue here.

Why is this happening?

Spring Boot automatically scans for components (like @Controller or @RestController classes) in the package where your main application class lives, plus all its sub-packages. From your logs, your main class is com.initiator.Application—so if your controller is in a package outside of com.initiator (or its sub-packages), Spring can’t find it at all.

Your build.gradle configuration checks out: you’ve got the spring-boot-starter-web dependency, all plugins are set up correctly, and both gradle clean build and gradle bootRun execute without errors. Dependencies aren’t the problem here.

How to fix it

Here are a few straightforward solutions:

  • Adjust your package structure
    This is the cleanest approach (following Spring Boot conventions). Move your controller classes into a sub-package of com.initiator—for example, com.initiator.controller.

  • Manually specify scan packages
    If you don’t want to rearrange your code, add the scanBasePackages attribute to your main class’s @SpringBootApplication annotation to tell Spring exactly where to look for components:

    @SpringBootApplication(scanBasePackages = "your.controller.package.path")
    public class Application {
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    }
    

    Replace your.controller.package.path with the actual package where your controller lives (like com.yourname.api).

  • Double-check your controller annotations
    Make sure your controller class uses @Controller (for view-based endpoints) or @RestController (for JSON/REST endpoints), and that your methods have valid mapping annotations. For example:

    @RestController
    @RequestMapping("/api")
    public class GreetingController {
        @GetMapping("/greeting")
        public String getGreeting() {
            return "Hello from your Spring Boot app!";
        }
    }
    

How to verify it’s fixed

After making these changes, restart your app with gradle bootRun and watch the logs. You should see a line like this:

s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/api/greeting],methods=[GET]}" onto public java.lang.String com.initiator.controller.GreetingController.getGreeting()

If you spot that line, your mapping is working! Now try accessing http://localhost:8080/api/greeting—it should return your expected response instead of the Whitelabel error.

One quick side note: Your build.gradle has both bootJar and war configurations, but since you’re using bootRun, you’re running the jar version. The war settings don’t affect your current setup, so no need to worry about that right now.


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 07:32:36