spring cloud 访问controller报(type=Not Found, status=404)错误

31次阅读

共计 591 个字符,预计需要花费 2 分钟才能阅读完成。

最近在学习微服务架构过程中,项目启动成功,但在浏览器访问 controller 对应的 requestMapping 的时候一直报 There was an unexpected error (type=Not Found, status=404). 错误。解决过程如下:
1. 检查了访问地址路径 “`http://localhost:8089/client1/hello?name=w“`,对应的 application.yml 里面配置的端口号和上下文路径没有问题。
controller 里面的 mapping 也没有问题。
2. 百度一下,网上说 springboot+thymeleaf 的项目需要添加 thymeleaf 依赖,而此项目只是
springboot。

3. 检查包路径。springboot 扫描文件 “`@ComponentScan(basePackages = {“com.spring.*”})“`,
由此 springboot 启动类的包必须是项目下的父路径,其他类的包路径必须是其子路径。

这里 ApplicationClient.java 是项目启动类,路径正确,而 controller 的路径不在 ApplicationClient.java
的包路径下,扫描不到。问题就在这里啦,修改 controller 的路径为
com.xxx.helloworld.eureka.client.controllers,最后访问成功啦。

正文完
 0