Java Spring - 使用MongoDB执行错误访问数据

时间:2015-07-26 18:32:10

标签: java spring mongodb maven

我想设置一个简单的 Java Spring和MongoDB 示例,但我还没有。在这种情况下,我正在关注Spring web中的示例。

我已经使用JRE8安装了Maven,Mongo DB和Java JDK 1.8。此外,在 pom.xml 文件中,由于编译错误,我不得不包含此依赖项:

    <dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-rest-core</artifactId>
    <version>2.3.1.RELEASE</version>
</dependency>

最后,按照上面提到的网页说明,我输入:

mvn spring-boot:run 要么 java -jar target / gs-access-data-mongodb-0.1.0.jar

在第一种情况下,spring-boot,我得到以下错误:

[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.2.5.RELEASE:run (default-cli) on project gs-accessing-data-mongodb: An exception occured while running. null: InvocationTargetException: Error creating bean with name 'messageConverters' defined in class path resource [org/springframework/boot/autoconfigure/web/HttpMessageConvertersAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.autoconfigure.web.HttpMessageConverters]: Factory method 'messageConverters' threw exception; nested exception is java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/JsonProcessingException: com.fasterxml.jackson.core.JsonProcessingException -> [Help 1]

有什么想法怎么办?提前谢谢。

1 个答案:

答案 0 :(得分:0)

不行,我的依赖关系看起来像这样:

   <dependencies>
    <dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-rest-core</artifactId>
    <version>2.3.1.RELEASE</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.6.0</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.6.0</version>
</dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>
    </dependencies>

结果在控制台中:

Customers found with findAll():
-------------------------------
Customer[id=55b52943dac11e2ab8262f16, firstName='Alice', lastName='Smith']
Customer[id=55b52944dac11e2ab8262f17, firstName='Bob', lastName='Smith']

Customer found with findByFirstName('Alice'):
--------------------------------
Customer[id=55b52943dac11e2ab8262f16, firstName='Alice', lastName='Smith']
Customers found with findByLastName('Smith'):
--------------------------------
Customer[id=55b52943dac11e2ab8262f16, firstName='Alice', lastName='Smith']
Customer[id=55b52944dac11e2ab8262f17, firstName='Bob', lastName='Smith']
2015-07-26 20:39:00.438  INFO 1884 --- [lication.main()] hello.Application                        : Started Application in 4.726 seconds (JVM running for 7.57)