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

Spring Boot+Gradle项目ModelAndView为空,访问控制器出现白标错误页

Troubleshooting Your Spring Boot + Gradle White Label Error & Empty ModelAndView

Hey there! Let’s break down why you’re hitting that white label error and seeing an empty ModelAndView when accessing /firstapp—I’ve been in this spot before, so let’s work through it systematically.

First, Check Your Core Dependencies

The most common gotcha here is missing the web starter dependency in your build.gradle. Without it, Spring Boot won’t set up the web infrastructure needed to handle controller requests. Double-check that your build.gradle has this line:

implementation 'org.springframework.boot:spring-boot-starter-web'

If you’re trying to render views (like using ModelAndView to load an HTML page), you’ll also need a template engine starter. For example, if you’re using Thymeleaf:

implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'

Verify Your Controller Annotations

Mixing up @Controller and @RestController is a super common mistake:

  • Use @Controller if you want to return a view (like an HTML page via ModelAndView). This requires a template engine to resolve the view name.
  • Use @RestController (or add @ResponseBody to your method) if you want to return raw text/JSON directly as the response body.

Here’s a correct example for a view-based controller:

@Controller
public class FirstAppController {
    @GetMapping("/firstapp")
    public ModelAndView loadFirstAppPage() {
        ModelAndView mav = new ModelAndView("firstapp"); // Matches src/main/resources/templates/firstapp.html (for Thymeleaf)
        mav.addObject("greeting", "Welcome to your first app!");
        return mav;
    }
}

And for a raw string response:

@RestController
public class FirstAppController {
    @GetMapping("/firstapp")
    public String getFirstAppResponse() {
        return "Hello from your first Spring Boot app!";
    }
}

Ensure Your Controller Is Being Scanned

Spring Boot automatically scans components (like controllers) in the same package as your main application class, and all its sub-packages. If your controller is in a completely separate package, Spring won’t find it.

For example:

  • If your main class is in com.example.firstapp, your controller should be in com.example.firstapp or com.example.firstapp.controller.
  • If it’s outside that scope, add @ComponentScan to your main class to specify the extra packages:
@SpringBootApplication
@ComponentScan(basePackages = {"com.example.firstapp", "com.other.controller.package"})
public class FirstAppApplication {
    public static void main(String[] args) {
        SpringApplication.run(FirstAppApplication.class, args);
    }
}

Check Your Application Properties

Double-check application.properties for any settings that might change your request path:

  • If you set server.servlet.context-path=/myapp, you’ll need to access http://localhost:8080/myapp/firstapp instead of just /firstapp.
  • If you changed server.port to something other than 8080, make sure you’re using the correct port in your browser.

Validate the Controller Mapping in Logs

When you start your application, look for a log line like this:

Mapped "{[/firstapp]}" onto public org.springframework.web.servlet.ModelAndView com.example.firstapp.controller.FirstAppController.loadFirstAppPage()

If you don’t see this line, your controller isn’t being registered—go back and check the annotations or package scanning.

Quick Test to Rule Out Issues

To isolate the problem, try creating a super simple @RestController with a basic mapping and see if it works. If it does, the issue is likely with your view setup or ModelAndView configuration. If not, it’s a core web setup or scanning problem.


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 07:07:30