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

使用Spring Boot与Thymeleaf时如何调试404错误?

Debugging 404 Errors with Spring Boot + Thymeleaf

Hey there, let's work through fixing that frustrating 404 error you're hitting. Looking at your provided code, here are targeted steps to diagnose and resolve the issue:

  • Double-check your template file location
    Thymeleaf automatically looks for templates in src/main/resources/templates/ by default. Make sure your home.html file is exactly in this directory. If it's placed elsewhere (like src/main/resources directly, or an unconfigured subfolder), Spring won't be able to locate it, leading to a 404.

  • Confirm your package structure is correct
    Your ZooplusApplication (I assume that's the full class name) and HomeController are both in the me.ankit.zooplus package—great, because Spring Boot auto-scans the application class's package and all subpackages for components like @Controller. Just make sure there's no typo in the package names that's throwing off the scan.

  • Verify you have the Thymeleaf starter dependency
    Without the correct dependency, Spring won't handle Thymeleaf templates at all. Check 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'
    
  • Enable debug logging to see behind the scenes
    Add these lines to your application.properties (or equivalent in YAML) to get detailed logs about request handling and template lookup:

    logging.level.org.springframework.web=DEBUG
    logging.level.org.thymeleaf=DEBUG
    

    When you start your app and hit /home, look for log entries like "Looking for template with name 'home'"—this will tell you if Spring is even trying to find the template, and if there's an error in locating it.

  • Ensure you're using @Controller, not @RestController
    Your code uses @Controller which is correct, since @RestController would return the string "home" directly as a JSON response instead of looking for a template. Just a quick double-check to make sure you didn't mix up the annotations.

  • Validate your request URL
    Make sure you're accessing the correct URL: if your app uses the default port, it should be http://localhost:8080/home. If you've set a context path via server.servlet.context-path in your config, you'll need to add that to the URL (e.g., http://localhost:8080/your-app-name/home).

  • Check the template file extension
    Thymeleaf defaults to .html as the template suffix. If your file is named something like home.txt or home.thymeleaf, you'll either need to rename it to home.html or configure the suffix in application.properties with spring.thymeleaf.suffix=.your-extension.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 09:50:44