You have learned in previous lessons how to read nodes and relationships from the database. Now you will learn how to write them to the database by using the save() method to create a new node in Neo4j.
Save() method
If you remember from an earlier lesson, Spring Data has some derived methods that are provided out-of-the-box. One of those is the save() method for saving data to a database. You will use this method to create new nodes in Neo4j.
Most of the pieces for adding this functionality are already in place, so you will just need to add the method in the MovieController class.
-
Open the
MovieController.javafile in thesrc/main/java/com/example/appspringdatadirectory. -
Import the
springframeworkPostMappingandRequestBodyclasses:javaimport org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; -
Add the
savemethod to theMovieController:java@PostMapping("/save") Movie save(@RequestBody Movie movie) { return movieRepo.save(movie); }
Full code for the MovieController class is available in the dropdown below.
Click to reveal the completed MovieController class code
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Optional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
@RestController
@RequestMapping("/movies")
public class MovieController {
private final MovieRepository movieRepo;
public MovieController(MovieRepository movieRepo) {
this.movieRepo = movieRepo;
}
@GetMapping()
Iterable<Movie> findAllMovies() {
return movieRepo.findMoviesSubset();
}
@GetMapping("/{movieId}")
Optional<Movie> findMovieById(@PathVariable String movieId) {
return movieRepo.findById(movieId);
}
@PostMapping("/save")
Movie save(@RequestBody Movie movie) {
return movieRepo.save(movie);
}
}This method is annotated with @PostMapping and will handle requests to the /movies/save endpoint. It calls the save() method on the repository and returns the results.
Testing the save() method
You will need to pass values for the new movie object you want to create. While the Movie domain class contains several fields, most are not required, so you can pick and choose what to send. For this example, you will send the movieId and title fields, which are outlined in a sample movie.json file (src/main/resources folder). The contents of the movie.json file are shown below.
{
"movieId": "9876",
"title": "MyMovie"
}Test the save() method on the command line by running the application and running the following command in the terminal tab:
curl -X POST 'localhost:8080/movies/save' \
-H 'Content-Type: application/json; charset=utf-8' \
-d @src/main/resources/movie.jsonYou should see something like the following output:
{"movieId":"9876","title":"MyMovie","plot":null,"poster":null,"url":null,"imdbId":null,"tmdbId":null,"released":null,"year":null,"runtime":null,"budget":null,"revenue":null,"imdbVotes":null,"imdbRating":null,"languages":null,"countries":null,"actors":null}You can also execute the provided query in the right-hand tab (also copied below) to see the new node in the database.
MATCH (m:Movie {title: "MyMovie"})
RETURN m;Feel free to play around with including different fields and values in the movie.json file.
Summary
In this lesson, you learned how to create a Movie node in Neo4j using the save() method.
In the next lesson, you will build upon that skill to create a pattern (node with relationships).