SoFunction
Updated on 2025-04-07

Detailed tutorial on SpringBoot integration with Neo4j

1. Environmental preparation

1. Create a Spring Boot project

You can use Spring Initializr to create a new Spring Boot project, select the following dependencies:

  • Spring Web
  • Spring Data Neo4j

2. Add Maven dependencies

Add Neo4j's related dependencies in

<dependency>
    <groupId></groupId>
    <artifactId>spring-boot-starter-data-neo4j</artifactId>
</dependency>
<dependency>
    <groupId>org.</groupId>
    <artifactId>neo4j-java-driver</artifactId>
    <version>4.4.4</version> <!-- Adjusted according to the latest version -->
</dependency>

2. Configure Neo4j

existorConnection information for Neo4j configured in:

.=bolt://localhost:7687
.=your_username
.=your_password

3. Define entity classes

use@NodeAnnotation defines the Neo4j node model. Here is a simple onePersonEntity class example:

import ;
import .;

@Node
public class Person {
    @Id
    private Long id;
    private String name;
    private int age;

    // Constructor, getter and setter    public Person() {}

    public Person(Long id, String name, int age) {
         = id;
         = name;
         = age;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
         = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
         = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
         = age;
    }
}

4. Create a data access layer

Using Spring Data Neo4jNeo4jRepositoryInterface to create data access layer. The following isPersonRepositoryExample:

import ..Neo4jRepository;

public interface PersonRepository extends Neo4jRepository<Person, Long> {
    Person findByName(String name);
}

V. Service layer

In the service layer, you can use@ServiceAnnotations to manage business logic:

import ;
import ;

import ;

@Service
public class PersonService {
    private final PersonRepository personRepository;

    @Autowired
    public PersonService(PersonRepository personRepository) {
         = personRepository;
    }

    public Person savePerson(Person person) {
        return (person);
    }

    public List<Person> findAllPersons() {
        return ();
    }

    public Person findByName(String name) {
        return (name);
    }
}

6. Control layer

Create a controller to handle HTTP requests:

import ;
import .*;

import ;

@RestController
@RequestMapping("/api/persons")
public class PersonController {
    private final PersonService personService;

    @Autowired
    public PersonController(PersonService personService) {
         = personService;
    }

    @PostMapping
    public Person createPerson(@RequestBody Person person) {
        return (person);
    }

    @GetMapping
    public List<Person> getAllPersons() {
        return ();
    }

    @GetMapping("/{name}")
    public Person getPersonByName(@PathVariable String name) {
        return (name);
    }
}

7. Run the application

Make sure the Neo4j database is running and then start your Spring Boot application. You can test the API by sending requests using Postman or other HTTP clients.

Sample Request

Create a node

POST /api/persons
Content-Type: application/json

{
    "id": 1,
    "name": "Alice",
    "age": 30
}

Query all nodes

GET /api/persons

Query nodes by name

GET /api/persons/Alice

8. Summary

With the above steps, you can easily integrate Neo4j in your Spring Boot application. Using Spring Data Neo4j not only simplifies the implementation of the data access layer, but also provides strong query capabilities and transaction management. Hopefully this article will help you get started quickly and take advantage of Neo4j to build your application.

The above is the detailed content of the detailed tutorial on SpringBoot Integration Neo4j. For more information about SpringBoot Integration Neo4j, please follow my other related articles!