无法让Neo4j和Spring Data在最新版本上运行

时间:2015-09-04 04:25:17

标签: spring junit neo4j spring-data-neo4j

我在使用空数据测试Neo4j本地服务器(未嵌入)时遇到问题。 我基本上修改了这个版本是完全注释的弹簧配置:

http://krams915.blogspot.sg/2012/03/spring-mvc-31-implement-crud-with.html

我担心这是关于版本的,所以如果有人可以帮助那将是伟大的。 这是我的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
  <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.krams</groupId>
<artifactId>spring-neo4j-tutorial</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>spring-neo4j-tutorial Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties> 
    <spring.core.version>4.2.0.RELEASE</spring.core.version>
    <spring.data.neo4j.version>3.4.0.RELEASE</spring.data.neo4j.version>
    <spring.boot.bersion>1.2.5.RELEASE</spring.boot.bersion>

    <neo4j.kernel.version>2.2.3</neo4j.kernel.version>
    <neo4j.cypher.version>2.2.3</neo4j.cypher.version>
    <neo4j.cypher.dsl.version>2.0.1</neo4j.cypher.dsl.version>

    <hibernate.validator.version>4.1.0.Final</hibernate.validator.version>
    <scala.library.version>2.11.7</scala.library.version>
    <cglib.version>3.1</cglib.version>
    <aspectj.version>1.8.6</aspectj.version>

    <javax.servlet-api.version>3.1.0</javax.servlet-api.version>
    <javax.servlet.jsp-api.version>2.2</javax.servlet.jsp-api.version>
    <javax.servlet.jstl.version>1.1.2</javax.servlet.jstl.version>
    <javax.taglibs.standard.version>1.1.2</javax.taglibs.standard.version>

    <slf4j.version>1.7.10</slf4j.version>
    <log4j.version>1.2.14</log4j.version>
    <jackson.version>1.9.3</jackson.version>

    <!-- Testing -->
    <mockito.version>1.10.19</mockito.version>
    <junit.version>4.12</junit.version>

    <!-- Plugins -->
    <maven.compiler.plugin.version>2.5.1</maven.compiler.plugin.version>
    <maven.plugin.version>2.6</maven.plugin.version>
</properties>
<dependencies>
    <!-- Spring Core -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.core.version}</version>
        <type>jar</type>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${spring.core.version}</version>
        <type>jar</type>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>${spring.core.version}</version>
        <type>jar</type>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring.core.version}</version>
        <type>jar</type>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>${spring.core.version}</version>
        <type>jar</type>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-oxm</artifactId>
        <version>${spring.core.version}</version>
        <type>jar</type>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aspects</artifactId>
        <version>${spring.core.version}</version>
        <type>jar</type>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-neo4j-rest</artifactId>
        <version>${spring.data.neo4j.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-autoconfigure</artifactId>
        <version>${spring.boot.bersion}</version>
    </dependency>
    <!-- A seamless aspect-oriented extension to the Java programming language -->
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>${aspectj.version}</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>${aspectj.version}</version>
    </dependency>
    <!-- Cglib is a powerful, high performance and quality Code Generation Library, It is used to extend JAVA classes and implements interfaces at runtime. -->
    <dependency>
        <groupId>cglib</groupId>
        <artifactId>cglib-nodep</artifactId>
        <version>${cglib.version}</version>
        <type>jar</type>
        <scope>compile</scope>
    </dependency>
    <!-- The JavaServer Pages Standard Tag Library (JSTL) encapsulates, as simple tags, core functionality common to many JSP applications. -->
    <dependency>
        <groupId>jstl</groupId>
        <artifactId>jstl</artifactId>
        <version>${javax.servlet.jstl.version}</version>
    </dependency>
    <dependency>
        <groupId>taglibs</groupId>
        <artifactId>standard</artifactId>
        <version>${javax.taglibs.standard.version}</version>
    </dependency>
    <!-- Data Mapper package is a high-performance data binding package built on Jackson JSON processor -->
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>${jackson.version}</version>
        <type>jar</type>
        <scope>compile</scope>
    </dependency>
    <!-- Jackson is a high-performance JSON processor (parser, generator) -->
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-core-asl</artifactId>
        <version>${jackson.version}</version>
        <type>jar</type>
        <scope>compile</scope>
    </dependency>
    <!-- Logger -->
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>${log4j.version}</version>
        <type>jar</type>
        <scope>compile</scope>
    </dependency>
    <!-- The Simple Logging Facade for Java or (SLF4J) serves as a simple facade or abstraction for various logging frameworks, e.g. java.util.logging, log4j and logback, allowing the end user to plug in the desired logging framework at deployment time. -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>${slf4j.version}</version>
        <type>jar</type>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>${slf4j.version}</version>
        <type>jar</type>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>jcl-over-slf4j</artifactId>
        <version>${slf4j.version}</version>
        <type>jar</type>
        <scope>compile</scope>
    </dependency>
    <!-- Spring Data Neo4j -->
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-neo4j</artifactId>
        <version>${spring.data.neo4j.version}</version>
        <exclusions>
            <exclusion>
                <artifactId>neo4j</artifactId>
                <groupId>org.neo4j</groupId>
            </exclusion>
            <exclusion>
                <artifactId>neo4j-cypher-dsl</artifactId>
                <groupId>org.neo4j</groupId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.neo4j</groupId>
        <artifactId>neo4j-kernel</artifactId>
        <version>${neo4j.kernel.version}</version>
    </dependency>
    <dependency>
        <groupId>org.neo4j</groupId>
        <artifactId>neo4j-cypher</artifactId>
        <version>${neo4j.cypher.version}</version>
    </dependency>
    <dependency>
        <groupId>org.neo4j</groupId>
        <artifactId>neo4j-cypher-dsl</artifactId>
        <version>${neo4j.cypher.dsl.version}</version>
    </dependency>
    <dependency>
        <groupId>org.scala-lang</groupId>
        <artifactId>scala-library</artifactId>
        <version>${scala.library.version}</version>
    </dependency> 
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>${hibernate.validator.version}</version>
    </dependency>
    <!-- Javax Servlet. This needs to be included for runtime only! -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>${javax.servlet-api.version}</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
    <groupId>javax.servlet.jsp</groupId>
    <artifactId>jsp-api</artifactId>
    <version>${javax.servlet.jsp-api.version}</version>
    <scope>provided</scope>
</dependency>
    <!-- Testing dependencies -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>${junit.version}</version>
        <type>jar</type>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-all</artifactId>
        <version>${mockito.version}</version>
        <type>jar</type>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>${spring.core.version}</version>
        <type>jar</type>
        <scope>test</scope>
    </dependency>
</dependencies>
<build>
    <resources>
    <resource>
      <filtering>true</filtering>
      <directory>src/test/resources</directory>
      <includes>
        <include>**/*.properties</include>
      </includes>
      <excludes>
        <exclude>**/*local.properties</exclude>
      </excludes>
    </resource>
    <resource>
      <directory>src/main/resources</directory>
      <includes>
        <include>**/*.properties</include>
        <include>**/*.xml</include>
      </includes>
    </resource>
</resources>
    <finalName>spring-neo4j-tutorial</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>${maven.compiler.plugin.version}</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
        <plugin>
    <artifactId>maven-resources-plugin</artifactId>
    <version>${maven.plugin.version}</version>
    <configuration>
      <encoding>UTF-8</encoding>
    </configuration>
  </plugin>
    </plugins>
</build>
<repositories>
    <repository>
        <id>org.springframework.maven.release</id>
        <name>Spring Maven Release Repository</name>
        <url>http://maven.springframework.org/release</url>
        <releases>
            <enabled>true</enabled>
        </releases>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
    <repository>
        <id>neo4j</id>
        <url>http://m2.neo4j.org/content/repositories/releases</url>
        <releases>
            <enabled>true</enabled>
        </releases>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
</repositories>

这是我的配置文件:

    package org.krams.config;

    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    //import org.springframework.context.annotation.ImportResource;
    import org.springframework.context.annotation.PropertySource;
    import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
    import org.springframework.web.servlet.ViewResolver;
    import org.springframework.web.servlet.config.annotation.EnableWebMvc;
    import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
    import org.springframework.web.servlet.view.InternalResourceViewResolver;

    @EnableWebMvc
    @ComponentScan("org.krams")
    @PropertySource(value={"spring.properties"})
    //@ImportResource({"trace-context.xml"})
    @Configuration
    public class AppContextConfiguration extends WebMvcConfigurerAdapter {

        /*
         * PropertySourcesPlaceHolderConfigurer Bean only required for @Value("{}") annotations.
         * Remove this bean if you are not using @Value annotations for injecting properties.
         */
        @Bean
        public static PropertySourcesPlaceholderConfigurer properties() {
            PropertySourcesPlaceholderConfigurer propertyConfigurer = new PropertySourcesPlaceholderConfigurer();
            return propertyConfigurer;
        }

        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/resources/**").addResourceLocations("classpath:../../META-INF/resources/");
        }

        @Bean
        public ViewResolver getViewResolver(){
            InternalResourceViewResolver resolver = new InternalResourceViewResolver();
            resolver.setPrefix("/WEB-INF/jsp/");
            resolver.setSuffix(".jsp");
            resolver.setOrder(1);
            return resolver;
        }

    }

    package org.krams.config;  

    import org.neo4j.graphdb.GraphDatabaseService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.Import;
    import org.springframework.data.neo4j.config.EnableNeo4jRepositories;
    import org.springframework.data.neo4j.config.Neo4jConfiguration;
    import org.springframework.data.neo4j.core.GraphDatabase;
    import org.springframework.data.neo4j.rest.SpringCypherRestGraphDatabase;
    import org.krams.repository.UserRepository;
    import org.springframework.boot.autoconfigure.EnableAutoConfiguration;

    @Configuration
    @Import(AppContextConfiguration.class)
    @EnableAutoConfiguration
    @EnableNeo4jRepositories(basePackages = "org.krams.repository")
    public class Neo4jConfigConfiguration  {
        @Value("${neo4j.server}")
        private String BASE_URI;

        @Value("${neo4j.user}")
        private String USER_NAME;

        @Value("${neo4j.password}")
        private String PASSWORD;

        @Autowired
        GraphDatabase graphDatabase;

        @Autowired
        UserRepository userRepository;

        @Bean
        GraphDatabaseService graphDatabaseService() {
            SpringCypherRestGraphDatabase grapdhDbService = new SpringCypherRestGraphDatabase(BASE_URI, USER_NAME, PASSWORD);
            return grapdhDbService;
        }

        @Configuration
        static class Neo4jMoreConfig extends Neo4jConfiguration {
            Neo4jMoreConfig() {
                setBasePackage("org.krams.domain");
            }
        }
    }

这是我的测试类:

            package org.krams.unit;

    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.krams.config.Neo4jConfigConfiguration;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.SpringApplicationConfiguration;
    import org.springframework.data.neo4j.support.Neo4jTemplate;
    import org.springframework.test.annotation.DirtiesContext;
    import org.springframework.test.annotation.Rollback;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    import org.springframework.test.context.transaction.BeforeTransaction;
    import org.springframework.test.context.web.WebAppConfiguration;

    @SpringApplicationConfiguration(classes = Neo4jConfigConfiguration.class)
    @RunWith(SpringJUnit4ClassRunner.class)
    @WebAppConfiguration
    @DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
    public class Neo4jConnectionTest {

        @Autowired
        private Neo4jTemplate template;

        @Rollback(true)
        @BeforeTransaction
        public void cleanDb() {
            System.out.println("Function Before");
        }

        @Test
        public void testConnection() {
            System.out.println("Connection build");
        }

    }

这会产生错误:

            package org.krams.unit;

            import org.junit.Test;
            import org.junit.runner.RunWith;
            import org.krams.config.Neo4jConfigConfiguration;
            import org.springframework.beans.factory.annotation.Autowired;
            import org.springframework.boot.test.SpringApplicationConfiguration;
            import org.springframework.data.neo4j.support.Neo4jTemplate;
            import org.springframework.test.annotation.DirtiesContext;
            import org.springframework.test.annotation.Rollback;
            import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
            import org.springframework.test.context.transaction.BeforeTransaction;

            @SpringApplicationConfiguration(classes = Neo4jConfigConfiguration.class)
            @RunWith(SpringJUnit4ClassRunner.class)
            @DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
            public class Neo4jConnectionTest {

                @Autowired
                private Neo4jTemplate template;

                @Rollback(true)
                @BeforeTransaction
                public void cleanDb() {
                    System.out.println("Function Before");
                }

                @Test
                public void testConnection() {
                    System.out.println("Connection build");
                }

            }

这会导致错误:

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.neo4j.support.typerepresentation.TypeRepresentationStrategyFactory]: Factory method 'ty
        peRepresentationStrategyFactory' threw exception; nested exception is java.lang.RuntimeException: Error reading as JSON ''
                        at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189)
                        at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588)
                        ... 160 more
        Caused by: java.lang.RuntimeException: Error reading as JSON ''
                        at org.neo4j.rest.graphdb.util.JsonHelper.readJson(JsonHelper.java:57)
                        at org.neo4j.rest.graphdb.util.JsonHelper.jsonToSingleValue(JsonHelper.java:62)
                        at org.neo4j.rest.graphdb.RequestResult.toEntity(RequestResult.java:104)
                        at org.neo4j.rest.graphdb.RequestResult.toMap(RequestResult.java:113)
                        at org.neo4j.rest.graphdb.index.RetrievedIndexInfo.<init>(RetrievedIndexInfo.java:42)
                        at org.neo4j.rest.graphdb.RestAPIIndexImpl.indexInfo(RestAPIIndexImpl.java:328)
                        at org.neo4j.rest.graphdb.RestAPICypherImpl.indexInfo(RestAPICypherImpl.java:748)
                        at org.neo4j.rest.graphdb.index.RestIndexManager.indexInfo(RestIndexManager.java:52)
                        at org.neo4j.rest.graphdb.index.RestIndexManager.existsForNodes(RestIndexManager.java:47)
                        at org.neo4j.rest.graphdb.RestAPICypherImpl.getIndex(RestAPICypherImpl.java:584)
                        at org.springframework.data.neo4j.rest.SpringCypherRestGraphDatabase.getIndex(SpringCypherRe
        stGraphDatabase.java:91)
                        at org.springframework.data.neo4j.support.typerepresentation.AbstractIndexBasedTypeRepresent
        ationStrategy.isStrategyAlreadyInUse(AbstractIndexBasedTypeRepresentationStrategy.java:56)
                        at org.springframework.data.neo4j.support.typerepresentation.TypeRepresentationStrategyFactory.chooseStrategy(TypeRepresentationStrategyFactory.java:51)
                        at org.springframework.data.neo4j.support.typerepresentation.TypeRepresentationStrategyFactory.<init>(TypeRepresentationStrategyFactory.java:35)
                        at org.springframework.data.neo4j.config.Neo4jConfiguration.typeRepresentationStrategyFactory(Neo4jConfiguration.java:151)
                        at org.krams.config.Neo4jConfigConfiguration$Neo4jMoreConfig$$EnhancerBySpringCGLIB$$ca3aba7a.CGLIB$typeRepresentationStrategyFactory$26(<generated>)
                        at org.krams.config.Neo4jConfigConfiguration$Neo4jMoreConfig$$EnhancerBySpringCGLIB$$ca3aba7a$$FastClassBySpringCGLIB$$d4ddeef3.invoke(<generated>)
                        at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
                        at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:318)
                        at org.krams.config.Neo4jConfigConfiguration$Neo4jMoreConfig$$EnhancerBySpringCGLIB$$ca3aba7a.typeRepresentationStrategyFactory(<generated>)
                        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
                        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
                        at java.lang.reflect.Method.invoke(Method.java:606)
                        at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleI
        nstantiationStrategy.java:162)
                        ... 161 more
        Caused by: java.io.EOFException: No content to map to Object due to end of input
                        at org.codehaus.jackson.map.ObjectMapper._initForReading(ObjectMapper.java:2768)
                        at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2711)
                        at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1856)
                        at org.neo4j.rest.graphdb.util.JsonHelper.readJson(JsonHelper.java:55)
                        ... 185 more

我不确定这是否与版本有关。 我当地的Neo4j版本操作系统2.2.3

感谢。

2 个答案:

答案 0 :(得分:0)

尝试使用此Java配置文件而不是XML配置。它应该有效,请查看this SO post以获得更好的解释。

import org.neo4j.ogm.session.Session;
import org.neo4j.ogm.session.SessionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;


@Configuration
@EnableNeo4jRepositories("com.app.repository")
@EnableTransactionManagement
@ComponentScan("com.app")

public class AppNeo4jConfiguration extends Neo4jConfiguration{


public SessionFactory getSessionFactory() {
    return new SessionFactory("com.app.bo");
}


@Bean
public Neo4jServer neo4jServer() {
    return new RemoteServer("http://localhost:7474");
}


@Bean
@Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS)
public Session getSession() throws Exception {
    return super.getSession();
}
}

答案 1 :(得分:0)

我已经更改了上面的代码。 我也没有提到DB REST API而犯了一个错误。 只要确保这两个就可以了。