Spring Boot+Gradle项目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
@Controllerif 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@ResponseBodyto 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 incom.example.firstapporcom.example.firstapp.controller. - If it’s outside that scope, add
@ComponentScanto 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 accesshttp://localhost:8080/myapp/firstappinstead of just/firstapp. - If you changed
server.portto 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

