1
votes

I am trying to config embedded neo4j with spring boot. But after getting so much trouble with version of different package I came up with this

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-neo4j</artifactId>
            <version>1.5.8.RELEASE</version>
        </dependency>


        <dependency>
            <groupId>org.neo4j</groupId>
            <artifactId>neo4j-ogm-embedded-driver</artifactId>
            <version>2.1.6</version>
            <scope>compile</scope> <!--compatible with spring-boot-starter-data-neo4j 1.5.8.RELEASE-->
        </dependency>


        <dependency>
            <groupId>org.neo4j</groupId>
            <artifactId>neo4j</artifactId>
            <version>3.1.0</version>
            <scope>runtime</scope>
        </dependency>

This are the dependency of a graph module which is a dependency of rest module.

When I start the server everything go right but when I try to access neo4j server with this code.

@Service
public class FileBasedImportServiceImpl implements ImportService {

private Session session;

@Autowired
public FileBasedImportServiceImpl(Session session) {
    this.session = session;
}

@Transactional
public void clearDatabase() {
    session.purgeDatabase();
}

@Transactional
public void load() {
    StringBuilder sb = new StringBuilder();
    BufferedReader reader = new BufferedReader(new InputStreamReader(Thread.currentThread().getContextClassLoader().getResourceAsStream("school.cql")));
    String line;
    try {
        while ((line = reader.readLine()) != null) {
            sb.append(line);
            sb.append(" ");
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    String cqlFile = sb.toString();
    session.query(cqlFile, Collections.EMPTY_MAP);
}
}

by calling load() method in a controller, I get connection refused.

Caused by: org.apache.http.conn.HttpHostConnectException: Connect to localhost:7474 [localhost/127.0.0.1] failed: Connection refused (Connection refused)

I think that the embedded neo4j didn't start that's why. So how to make the embedded server start. I was thinking it will start automatically if I use spring boot but it wasn't the case.

Note: the file school.cql contains cypher query this is the project that I followed https://github.com/neo4j-examples/sdn-university

I have already add the annotation to my main class @EntityScan("com")

Thank you.

Update

This is the configuration annotation of spring application

@ComponentScan(basePackages={"com"})
@Configuration
@EnableJpaRepositories("com.repository")
@EntityScan(basePackages = {"com.domain", "org.springframework.data.jpa.convert.threeten"})
@EnableConfigurationProperties
@EnableAutoConfiguration
@EnableScheduling
@SpringBootApplication(exclude = org.activiti.spring.boot.SecurityAutoConfiguration.class)
@EnableNeo4jRepositories("com.graph.repository")
public class Application extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);

    }

Like I said I have followed this tutorial https://github.com/neo4j-examples/sdn-university and the only thing that I have done different is making two maven module instead of one for the graph that contains the repository/ domain entities/service related to neo4j. The other module is related to spring boot and to rest controller

1
Can you provide a sample somewhere or at least post your configuration? Your application connects to the REST API instead of an embedded instance.meistermeier
Hello, What configuration should I post ? and what do you mean by " Your application connects to the REST API instead of an embedded instance"Mohamed Amine Ouali
I mean the value of the Neo4j url. Looking at the exception you get SDN/Neo4j OGM looks for an instance that is available via HTTP.meistermeier
I am using embedded neo4j so normally I don't have to specify the url. it will run it and detect the url it self. For example in the github link that I have specified they didn't specify an url. But I am not sure if my thoughts are correct. And to answer your question i don't have a config where I specify the urlMohamed Amine Ouali
should I specify a configuration bean because I am using an old versionMohamed Amine Ouali

1 Answers

1
votes

SO I have found the problem

Spring boot doesn't detect neo4j dependency

<dependency>
    <groupId>org.neo4j</groupId>
    <artifactId>neo4j</artifactId>
    <version>3.1.0</version>
    <scope>runtime</scope>
</dependency>

If it is not in the same pom.xml as the spring-boot maven plugin.

So in other word transitive dependency doesn't work.