Your challenge is to create a Person repository interface and controller class to access the person entities in the database. Then, you will check your work by testing the application and finding a person node.
PersonRepository
In the src/main/java/com/example/appspringdata
directory, create an interface called PersonRepository.java
that extends the Neo4jRepository
interface with the appropriate domain and identifier types.
Completed code is available below for verification.
Click to reveal the completed PersonRepository
interface code
import org.springframework.data.neo4j.repository.Neo4jRepository;
public interface PersonRepository extends Neo4jRepository<Person, String> {
}
PersonController
In the same directory, add a new class called PersonController.java
with proper annotations (/people
main endpoint) and create methods to implement findAll()
and findById()
for Person
entities.
Completed code is available below.
Click to reveal the completed PersonController
class code
import java.util.Optional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.PathVariable;
@RestController
@RequestMapping("/people")
public class PersonController {
private final PersonRepository personRepo;
public PersonController(PersonRepository personRepo) {
this.personRepo = personRepo;
}
@GetMapping()
Iterable<Person> findAllPeople() {
return personRepo.findAll();
}
@GetMapping("/{imdbId}")
Optional<Person> findPersonById(@PathVariable String imdbId) {
return personRepo.findById(imdbId);
}
}
Test the application
With those pieces in place, you can now test the application. Run the application and execute the following commands in the terminal to see the results.
curl 'localhost:8080/people'
curl 'localhost:8080/people/0000245'
Summary
In this lesson, you created repository and controller classes to access Person
entities in the database. You also ran and tested the application, using an API to call repository methods, query the database, and return the results.
Next, you will map the relationships between Movie
and Person
entities.