Spring Boot应用无法映射HTML文件,访问路径返回白标错误求助
Hey there, let's walk through why you're seeing that frustrating Whitelabel Error Page when accessing http://localhost:8080/info/aa, and how to fix it. The issue almost always boils down to mismatched view locations, incorrect controller setup, or missing dependencies—let's break it down step by step.
1. Verify Your HTML File Location
Spring Boot uses template engines (like Thymeleaf, the default) to resolve view files, and it expects them in a specific directory:
- Make sure your
info.htmlis placed insrc/main/resources/templates/(not thestaticfolder—staticis for direct-access assets like CSS/JS, not controller-rendered views).
If you want to organize views into subfolders (e.g., templates/info/info.html), your controller's return value should match that path (like return "info/info";).
2. Check the Controller's Return Value
Your controller method getServerInfo returns a string—this string must exactly match the name of your HTML file (without the .html suffix). For example:
- If your file is
templates/info.html, your method should return"info" - If you named the file
templates/server-info.html, return"server-info"
Here's a corrected snippet of your controller to clarify:
@Controller @RequestMapping("/info") public class InfoController { @RequestMapping("/aa") public String getServerInfo(Map<String, Object> model) { // Add any model data you need here, e.g.: model.put("serverName", "Local Dev Server"); // Return the exact view name (matches your HTML file in templates/) return "info"; } }
3. Ensure You Have the Template Engine Dependency
If you're using Thymeleaf (Spring Boot's default for HTML views), make sure you've included the starter dependency in your build file:
For Maven (pom.xml):
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
For Gradle (build.gradle):
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
Without this dependency, Spring can't resolve your HTML view and will throw the Whitelabel error.
4. Confirm You're Using the Right Controller Annotation
Double-check that you're using @Controller (not @RestController). The @RestController annotation combines @Controller and @ResponseBody, which means it will return your string as raw text/JSON instead of trying to find a view file. That's a super common mistake!
5. Check for Custom View Resolver Configurations
If you've added custom view resolver beans to your application, make sure their prefix and suffix settings match your file structure. For Thymeleaf, the default prefix is classpath:/templates/ and suffix is .html—if you've changed these, your file path needs to align.
Quick Test to Debug
If you're still stuck, try returning a hardcoded string with a full path temporarily (e.g., return "classpath:/templates/info.html";) to see if Spring can find it. If that works, you know the issue is with your view name or path setup.
内容的提问来源于stack exchange,提问作者hellzone

