我正在開發 SpringBoot REST API,并且一直在關注教程。我之前遵循了本教程,一切正常,但是現在當我打電話時:
http://localhost:8080/api/players
我收到 404 錯誤:
{"timestamp":"2021-11-05T15:05:07.850 00:00","status":404,"error":"Not Found","path":"/api/players"}
領域:
@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Player {
private @Id
@GeneratedValue
Long id;
private String firstName;
private String lastName;
private String email;
private String bio;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Player player = (Player) o;
return Objects.equals(id, player.id) &&
Objects.equals(firstName, player.firstName) &&
Objects.equals(lastName, player.lastName) &&
Objects.equals(email, player.email) &&
Objects.equals(bio, player.bio);
}
@Override
public int hashCode() {
return Objects.hash(id, firstName, lastName, email, bio);
}
}
回購:
public interface PlayerRepository extends CrudRepository<Player, Long> {
}
資料庫加載器:
@Component
public class DatabaseLoader implements CommandLineRunner {
private final PlayerRepository repository;
@Autowired
public DatabaseLoader(PlayerRepository repository) {
this.repository = repository;
}
@Override
public void run(String... strings) throws Exception {
this.repository.save(new Player(1L, "Magnus", "Carlsen", "[email protected]", "I am world champion."));
}
}
彈簧應用:
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
應用屬性:
spring.data.rest.base-path=/api
正如我所說,我真的不明白為什么它不起作用,因為我已經檢查了兩次并且它應該起作用,就像我第一次學習本教程時一樣:
https://spring.io/guides/tutorials/react-and-spring-data-rest/
謝謝你的幫助!
curl http://localhost:8080
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8"/>
<title>ReactJS Spring Data REST</title>
<link rel="stylesheet" href="/main.css" />
</head>
<body>
<div id="react"></div>
<script src="built/bundle.js"></script>
</body>
</html>
curl http://localhost:8080/api
{
"_links" : {
"profile" : {
"href" : "http://localhost:8080/api/profile"
}
}
}
uj5u.com熱心網友回復:
檢查所有類的包。它們應該是您的主Application類所在的包的子包。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/351870.html
