Spring Boot特定Controller的GetMapping映射失效问题求助
我是Spring Boot新手,当前遇到一个问题:QuestionController中的GetMapping路径未被应用识别。访问URL
http://localhost:8080/app/survey/question/questionlist时出现404错误。questionlist.html与其他正常工作的HTML文件一样位于templates目录下,但该文件在资源管理器中显示红色菱形标识。
相关代码
控制器代码
package com.pablop.survey.web.app.controllers; import java.util.List; //import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; //import com.pablop.survey.web.app.models.entity.Question; //import com.pablop.survey.web.app.services.IQuestionService; @Controller @RequestMapping ("app/survey/question") public class QuestionController { // @Autowired // private IQuestionService iQuestionService; @GetMapping("/questionlist") public String questionlist(Model model) { // List<Question> questionList= iQuestionService.questionList(); return "questionlist"; } }
跳转HTML代码
<tbody class="card-body"> <tr th:each="survey: ${surveyList}"> <td class="h5"> <h4 th:text="${survey.surveyName}"></h4> <div> <a th:href="@{'/app/survey/question/questionlist'}" th:text="${'List of questions'}" class="text-primary"></a> </div> </td> <td><a th:href="@{'/app/survey/edit/'+ ${survey.id}}" th:text="${edit}" class="btn btn-warning"></a></td> <td><a th:href="@{'/app/survey/delete/'+ ${survey.id}}" th:text="${delete}" class="btn btn-danger" onclick="return confirm ('Are you sure to delete this item?')"></a></td> <td><a th:href="@{''}" th:text="${startTest}" class="btn btn-success" onclick="return confirm ('Are you sure to delete this item?')"> </a></td> </tr> </tbody>
排查步骤
恢复核心注解的导入语句
控制器代码中@Controller、@GetMapping等注解的导入被注释,这会导致Spring无法识别该类为控制器。必须取消注释这些导入:import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping;确认控制器被Spring扫描
检查项目主启动类所在包是否是com.pablop.survey.web.app或其父包(比如com.pablop)。如果主启动类在其他包,需在主启动类上添加@ComponentScan(basePackages = "com.pablop.survey.web.app.controllers")指定扫描路径。修复questionlist.html的IDE识别问题
红色菱形标识说明IDE未正确识别该文件:- 确认文件位于
src/main/resources/templates目录下,路径无拼写错误 - 右键点击
templates目录,选择「Mark Directory as」→「Resources Root」 - 清理IDE缓存(File → Invalidate Caches... → Invalidate and Restart)后重新构建项目
- 确认文件位于
验证模板引擎配置
若使用Thymeleaf,确保依赖已正确引入:<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>检查
application.properties中的默认配置是否有效:spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.suffix=.html排查路径冲突与上下文路径
- 检查是否有其他控制器的路径与
/app/survey/question/questionlist冲突 - 查看
application.properties中是否配置了server.servlet.context-path,若有,访问URL需加上该前缀
- 检查是否有其他控制器的路径与
简化测试定位问题
暂时修改控制器为最简形式,测试基础路径是否生效:@Controller public class TestController { @GetMapping("/test") public String test() { return "questionlist"; } }访问
http://localhost:8080/test,若仍404,说明问题出在控制器识别或模板文件上;若正常,再逐步恢复原路径配置排查。
内容的提问来源于stack exchange,提问作者Pablo PARODI

