我在 Eclipse 中创建了一个简单的 Spring Boot 应用程序,其中包含 User 类,并且我编写了 Web 服务来对 User 执行各种操作,例如获取用户、创建用户、删除用户。
当我从浏览器测试应用程序时,出现以下错误: 此应用程序没有 /error 的显式映射,因此您将其视为后备。
我使用的路径:http://localhost:8080/user/get-user/Mike 我已经注释了用户资源类@RestController和应用程序类@SpringBootApplication。
代码片段:
@RequestMapping(method = RequestMethod.GET, path = "/user/get-user/{name}" )
public User getUserByName(@PathVariable String name) {
System.out.println("Request for name: "+name);
return service.getUser(name); // this return the first user found with given name
}
这是我的项目结构: 项目结构
刚刚查看了您的项目结构。在您的情况下,您需要确保您的主类位于其他类之上的根包中。
每当启动或运行 Spring Boot 应用程序(例如:
Application.java
或用 @SpringBootApplication
注释的类)时,它都会扫描主类包下面的类。
一个例子是:
com
+- test
+- Application.java
|
+- controller
| +- UserRestController.java
+- domains
+- User.java
尝试一下并检查。 另外,请确保您已在 Spring boot 应用程序正常工作所需的所有类上添加了所有必需的注释。
就我而言(我最终来到这里,寻找解决方案,Stackoverflow 很棒!),我忘记添加 thymeleaf 依赖项。
你能告诉我如何将你的 application.java 移动到 intelliJ 中的其他包之上吗?