我創建了Spring Boot Maven項目,但是我的RequestMapping以及localhost:8080返回了404錯誤頁面。我認爲問題在於我的軟件包是如何設置的,但我已經嘗試了多個問題的解決方案,而且我仍然無法解決錯誤頁面。你們能否指出我如何解決這個問題的正確方向?也許我需要在我的Main類上面添加Component註釋?但我試過這個解決方案,錯誤仍然存在。Spring Boot | localhost:8080顯示404錯誤頁面
這裏是我的包結構:
/src/main/java
ControllerLayer
UsersController.java
DataAccessLayer
UsersDAL.java
ServiceLayer
UsersService.java
Main
Main.java
Main.java:
@SpringBootApplication(scanBasePackages = {
"/src/main/java/ControllerLayer", "/src/main/java/DataAccessLayer",
"/src/main/java/ServiceLayer" })
public class Main {
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}
}
UsersController.java:
import Entities.Users;
import ServiceLayer.UsersService;
@RestController
@RequestMapping("/users")
public class UsersController {
@Autowired
private UsersService usersService;
@RequestMapping(value =
"/create/{userId}/{userPassword}/{userAge}/{userEmail}"
+ "/{userFirstName}/{userlastName}", method =
RequestMethod.POST)
public void createUser(@PathVariable("userId")String userId,
@PathVariable("userPassword")String userPassword,
@PathVariable("userAge")int userAge,
@PathVariable("userEmail")String userEmail,
@PathVariable("userFirstName")String userFirstName,
@PathVariable("userLastName")String userLastName) {
usersService.createUser(new Users(userId, userPassword,
userAge, userEmail, userFirstName, userLastName));
}
}
UserService.java
import DataAccessLayer.UsersDAL;
import Entities.Users;
@Service
public class UsersService {
@Autowired
private UsersDAL usersDAL;
public void createUser(Users user) {
usersDAL.createUser(user);
}
}
級
的pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>xProjectAlpha</groupId>
<artifactId>org.htech.xProjectAlpha</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.9.Final</version><!--$NO-MVN-MAN-VER$-->
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
您需要從'scanBasePackages'刪除'/ src目錄/主/ JAVA /'然後是什麼你想的網址是什麼? – developer
如果我刪除/ src/main/java,Spring會拋出一個UnsatisfiedDependencyException:創建名爲'usersController'的bean時出錯:通過字段'usersService'表示的不滿意的依賴關係。我使用的URL只是localhost:8080/users –
你可以在上面的類中添加你的包名嗎? – developer