Spring Boot中保存Polygon到Neo4j及空间插件依赖导入问题
Hey there! Let’s tackle this Neo4j Spatial plugin import issue you’re facing with the embedded driver. I’ve dealt with similar version mismatch headaches before, so here’s how to fix it step by step:
This is the #1 culprit for import errors. Neo4j’s spatial plugin is tightly coupled to the core database version—they must be identical. For example:
- If you’re using Neo4j Embedded 4.4.23, you need
neo4j-spatial4.4.23 - For Neo4j 5.12.0, use
neo4j-spatial5.12.0
Mixing versions (e.g., Neo4j 4.4 with spatial 5.x) will break class imports and runtime functionality.
Replace your existing Neo4j-related dependencies with this compatible set (adjust versions to match your target Neo4j release):
<dependencies> <!-- Neo4j Embedded Core --> <dependency> <groupId>org.neo4j</groupId> <artifactId>neo4j</artifactId> <version>4.4.23</version> <!-- Match this to your spatial plugin version --> </dependency> <!-- Neo4j Spatial Plugin --> <dependency> <groupId>org.neo4j</groupId> <artifactId>neo4j-spatial</artifactId> <version>4.4.23</version> <!-- Must exactly match Neo4j core version --> <type>jar</type> </dependency> <!-- Optional: JTS for WKT parsing (required for Polygon handling) --> <dependency> <groupId>org.locationtech.jts</groupId> <artifactId>jts-core</artifactId> <version>1.18.2</version> <!-- Compatible with Neo4j 4.x spatial --> </dependency> </dependencies>
Pro Tip to Detect Conflicts
Run this Maven command to spot accidental mismatched dependencies:
mvn dependency:tree -Dverbose | grep neo4j
If you see multiple Neo4j versions listed, exclude the older ones from conflicting dependencies.
Double-check your imports—package structures can change between Neo4j versions:
// For embedded database access import org.neo4j.graphdb.GraphDatabaseService; // Spatial repository and WKT utilities import org.neo4j.spatial.SpatialRepository; import org.neo4j.spatial.WKTReader;
Note: For Neo4j 5.x, the spatial package structure shifted slightly—you may need org.neo4j.spatial.core.SpatialRepository instead.
Unlike server-based Neo4j, the embedded driver requires manual registration of the spatial plugin to make SpatialRepository available:
import org.neo4j.graphdb.factory.GraphDatabaseFactory; import org.neo4j.spatial.SpatialRepository; import java.io.File; public class Neo4jSpatialSetup { public static void main(String[] args) { GraphDatabaseService db = new GraphDatabaseFactory() .newEmbeddedDatabaseBuilder(new File("./neo4j-db")) .registerExtension(SpatialRepository.class) // Critical: Register spatial extension .newGraphDatabase(); // Now you can use SpatialRepository to add WKT Polygons WKTReader reader = new WKTReader(); // Example: Parse a WKT Polygon var polygon = reader.read("POLYGON((0 0, 0 10, 10 10, 10 0, 0 0))"); // Add to spatial repository... } }
- Clean Maven Cache: Run
mvn clean install -Uto force refresh dependencies and eliminate cached old versions. - IDE Dependency Check: In your IDE (IntelliJ/Eclipse), go to the dependency viewer and remove any duplicate or mismatched Neo4j JARs.
- Check Neo4j Spatial Docs: Confirm the API matches your version—some deprecated classes (like older
GraphRepositoryvariants) may have been replaced with session-based APIs in newer releases.
内容的提问来源于stack exchange,提问作者Hema

