Spring REST API 没有显示 /players 尽管遵循了教程?
Spring REST API not showing /players despite following tutorial?
我正在开发 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", "magnuscarlsen@gmail.com", "I am world champion."));
}
}
Spring应用程序:
@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"
}
}
}
检查所有 classes 的包。它们应该是您的主要 Application
class 所在包的子包。
我正在开发 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", "magnuscarlsen@gmail.com", "I am world champion."));
}
}
Spring应用程序:
@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"
}
}
}
检查所有 classes 的包。它们应该是您的主要 Application
class 所在包的子包。