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

SpringBoot转Gradle后访问出现Whitelabel错误404求助

Troubleshooting Whitelabel 404 After Converting Spring Boot Maven Project to Gradle

Hey there, let's dig into this 404 issue you're facing after switching your Spring Boot project from Maven to Gradle. Even though the logs say the / path is mapped, that Whitelabel error is definitely frustrating—let's break down the most likely fixes step by step:

1. Check Package Scanning Scope

Spring Boot automatically scans for beans (like controllers) in the same package as your main application class (the one annotated with @SpringBootApplication) and all its sub-packages. If your controller lives outside this scope, it won't get registered.

  • Example: If your main class is in com.myapp, ensure your controller is in com.myapp.controller or a sub-package under it. If it's in a separate top-level package (e.g., com.other.controller), add a component scan annotation to your main class:
    @SpringBootApplication
    @ComponentScan(basePackages = {"com.myapp", "com.other.controller"})
    public class MyApplication {
        public static void main(String[] args) {
            SpringApplication.run(MyApplication.class, args);
        }
    }
    

2. Verify Gradle Build Configuration

Double-check that your build.gradle has the correct Spring Boot plugins and dependencies—this is a common pitfall when converting from Maven:

  • Your plugins block should include the Spring Boot and dependency management plugins:
    plugins {
        id 'org.springframework.boot' version '3.2.0' // Use your project's actual Spring Boot version
        id 'io.spring.dependency-management' version '1.1.4'
        id 'java'
    }
    
  • Make sure you have the spring-boot-starter-web dependency (required for web controllers):
    dependencies {
        implementation 'org.springframework.boot:spring-boot-starter-web'
        // Add this if you're using view templates like Thymeleaf
        // implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
        testImplementation 'org.springframework.boot:spring-boot-starter-test'
    }
    
  • Run ./gradlew clean build to resolve any hidden dependency issues, then try bootRun again.

3. Validate Controller Annotations

A small annotation mistake can lead to unexpected 404s:

  • If you're returning raw text/JSON, use @RestController (combines @Controller and @ResponseBody). If you're returning view templates, use @Controller.
  • Ensure your mapping annotation is correctly applied to the method. For the root path, it should look like this:
    @RestController
    public class HomeController {
        @GetMapping("/")
        public String home() {
            return "Hello from Gradle!";
        }
    }
    
  • If using @Controller, make sure you have a corresponding template file (e.g., src/main/resources/templates/home.html for Thymeleaf) and your method returns the template name:
    @Controller
    public class HomeController {
        @GetMapping("/")
        public String home() {
            return "home"; // Maps to src/main/resources/templates/home.html
        }
    }
    

4. Inspect the Actual Mapped Endpoint Logs

That log line saying "/" is mapped might not be from your controller. Look closely at the full log entry—it should list the fully qualified class name of the handler. For example:

Mapped "{[/],methods=[GET]}" onto public java.lang.String com.myapp.controller.HomeController.home()
If the log references a different class (like Spring Boot's internal error handlers), your controller isn't being registered—circle back to the package scanning or Gradle dependency checks.

5. Clean and Rebuild to Clear Caches

Sometimes leftover files from the Maven-to-Gradle conversion can cause weird issues. Run these commands to start fresh:

./gradlew clean
./gradlew build --refresh-dependencies
./gradlew bootRun

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.22 09:56:30