I am developing a SpringBoot REST API, and have been following a tutorial. I have followed this tutorial before and everything worked fine, yet now when I call:
http://localhost:8080/api/players
I get a 404 error:
{"timestamp":"2021-11-05T15:05:07.850 00:00","status":404,"error":"Not Found","path":"/api/players"}
Domain:
@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);
}
}
Repo:
public interface PlayerRepository extends CrudRepository<Player, Long> {
}
Database Loader:
@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."));
}
}
SpringApplication:
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Application Properties:
spring.data.rest.base-path=/api
As I say, I really don't understand why it's not working as I have checked everything twice and it should work, just like it did the first time I followed this tutorial:
https://spring.io/guides/tutorials/react-and-spring-data-rest/
Thanks for any help!
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"
}
}
}
CodePudding user response:
Check the packages of all your classes. They should be sub packages of the package where your main Application
class is.