using relative paths for embedded neo4j in spring-boot application

spring boot hostname property
spring boot application.properties example
spring boot application.yml datasource
spring boot application.properties location
spring boot application.yml example
spring boot profiles
how to read external properties file in spring boot
spring boot configuration

I am trying to setup the sample spring-boot-neo4j application but with an embedded database. I added required dependencies and configurations as suggested here and here. While this works for absolute paths

spring.data.neo4j.uri=file://var/tmp/graph.db

I cant seem to find a way to specify relative paths. I tried file://graph.db and file://./graph.db and file://~/graph.db but get this

Caused by: java.lang.IllegalArgumentException: URI has an authority component
    at java.io.File.<init>(File.java:423) ~[na:1.8.0_112]
    at org.neo4j.ogm.drivers.embedded.driver.EmbeddedDriver.createPermanentFileStore(EmbeddedDriver.java:211) ~[neo4j-ogm-embedded-driver-2.0.5.jar:na]
    at org.neo4j.ogm.drivers.embedded.driver.EmbeddedDriver.configure(EmbeddedDriver.java:102) ~[neo4j-ogm-embedded-driver-2.0.5.jar:na]

tried dropping file:// and used graph.db or ./graph.db but get URI is not absolute

tried file:./graph.db but get URI is not hierarchical

I want to use path relative to project location (or home folder) can somebody point how to specify such paths. I am using neo4j-ogm 2.0.5, gradle 2.14, spring-boot 1.4.2 and jdk 1.8.

Thanks

Behold mighty cross-platform solution:

  @Value("${spring.data.neo4j.uri:default}")
  private String neoUri;

  @Bean
  public Configuration neoConfig() throws URISyntaxException, IOException {
    String path = neoUri;
    Configuration.Builder builder = new Configuration.Builder();
    if (!"default".equals(path)) {
      LOG.info("Configured database uri: " + path);
      path = path.replace('\\', '/');
      URI uri = new URI(path);
      if ("file".equals(uri.getScheme())) {
        File file = new File(uri.getSchemeSpecificPart());
        path = "file:///" + file.getCanonicalFile().getAbsolutePath().replace('\\', '/');
        LOG.info("Absolute database path: " + path);
      }
      builder.uri(path);
    }
    // TODO: process other neo4j properties
    return builder.build();
  }

Limitations: can't use backslash as part of directory name ;)

NullPointerException when building a configuration using a URI with , Only supported with the default logback setup. logging.file.max-size=10MB # Maximum Path to the SQL file to use to initialize the database schema. Can be absolute or relative to the Tomcat base dir. server.tomcat.accesslog.enabled=​false spring.data.neo4j.embedded.enabled=true # Whether to enable embedded  Using neo4j-harness brings in a dependency on Jetty container. If you use Spring Boot dependency management the version may be set to an incompatible version. This may be avoided by overriding jetty.version property to a version required by neo4j server.

Appendix A. Common application properties, This appendix provides a list of common Spring Boot properties and references to the Names can be an exact location or relative to the current directory. spring.application.admin.jmx-name Path to the SQL file to use to initialize the database schema. spring.data.neo4j.embedded.enabled. true. Spring Data Neo4j offers advanced features to map annotated entity classes to the Neo4j Graph Database. The template programming model is equivalent to other Spring templates and builds the basis for interaction with the graph and is also used for the Spring Data repository support.

You can use ${user.dir} to resolve a absolut path.

uri=file:${user.dir}/target/mock_data/neo4j_${random.uuid}/graph.db
uri=file:${user.dir}/../neo4j_${random.uuid}/graph.db

Common Application properties - Project Metadata API Guide, Using the @SpringBootApplication Annotation; 3.7. Running Connecting to a Neo4j Database; Using the Embedded Mode; Using Native Types Custom context path Names can be an exact location or relative to the current directory. By using our site, spring-boot. Score 1. Posts 1. 1 using relative paths for embedded neo4j in spring-boot application Oct 11 '19.

Spring Boot Reference Documentation, Neo4j. 12.3.1. Connecting to a Neo4j Database; 12.3.2. Using the Embedded Mode; 12.3.3. Using Using Spock to Test Spring Boot Applications. 26.4. Locations with wildcards are sorted alphabetically based on the absolute path of the file names. Names can be an exact location or relative to the current directory. The Neo4j Java Driver Spring Boot starter provides both a Spring Boot autoconfigure and a starter module. The autoconfigure module adds a single instance of org.neo4j.driver.Driver as a bean to the Spring Context if there is none and registers health- and metric endpoints for all Driver-beans if the necessary Spring infrastructure is in place.

Spring Boot Features, Defaults to "true" when using Spring Transaction Management, otherwise "false". Context path of the application. server.display-name=application # Display name of the Can be relative to the tomcat base dir or absolute. Compiler to use. spring.data.neo4j.embedded.enabled=true # Enable embedded mode if the​  You’ll build a Spring application that let’s you create and retrieve Employee and Address objects stored in a Neo4j NoSQL database (Neo4j Server) using Spring Data REST. You’ll use the non embedded Neo4j for achieving the exporter, and the Tomcat 8 for running the Spring Boot application.

Neo4j Java Driver Spring Boot Starter, As with any other Spring Boot starter, the only thing you have to do is to to get all the dependencies needed for creating a Spring based web application. { (2) this.driver = driver; } @GetMapping(path = "/movies", produces = MediaType. Check out our amazing learning paths: Learning Path 01 - Spring and Spring Boot Web Applications and API Developer, Learning Path 02 - Full Stack Developer with Spring Boot, React & Angular, Learning Path 03 - Cloud Microservices Developer with Docker and Kubernetes, Learning Path 04 - Learn Cloud with Spring Boot, AWS, Azure and PCF and

Comments
  • unfortunately this does not work (at least not in windows) -- java.net.URISyntaxException: Illegal character in opaque part at index 7: file:D:\src\neo4j-loader\graph.db . I tried file:/ and file://, different error message but no luck. The : char seems to be the issue. will try in linux
  • This works only on linux! Post a cross-platform solution.
  • replace ${user.dir} with #{T(java.lang.System).getProperty('user.dir').replace('\\','/')} I think you need to develop your own Windows solution with SpEL. Because Windows has its own way to tread paths and "fancy characters" like back slash, white spaces, ... See details. Also note the @Bean solution that digx1 mentions. You can do amazing things with SpEL but it can be easier to write plain Java code.