实施MongoRepository时Springboot应用程序无法运行

时间:2018-07-10 14:36:01

标签: java spring mongodb

我有一个管理ID的Springboot应用程序。当前,它读取ID并将其保存到文件中。我正在尝试使用Spring内置的Mongo存储库将该功能转移到MongoDB。

ServiceProviderRepository

    package repositories;

import org.springframework.data.mongodb.repository.MongoRepository;

import models.ServiceProvider;

public interface ServiceProviderRepository extends MongoRepository<ServiceProvider, String> {

    public ServiceProvider findByID(int id);
    public ServiceProvider findByUsername(String username);

}

服务提供商

package models;

import org.springframework.data.annotation.Id;

public class ServiceProvider {

    @Id
    private String id;

    private int providerId;
    private String username;

    public ServiceProvider() {}

    public ServiceProvider(int id, String username) {
        this.providerId = id;
        this.username = username;
    }

    public ServiceProvider(int id) {
        this.providerId = id;
    }

    public int getId() {
        return providerId;
    }

    @Override
    public String toString() {
        return String.format("ServiceProvider[id=%s, providerId='%s', username='%s']", id, providerId, username);
    }
}

应用

package app;

import java.io.IOException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
import org.springframework.web.client.RestTemplate;


import controllers.ServiceProviderController;
import models.ServiceProvider;
import repositories.ServiceProviderRepository;

@SpringBootApplication
@ComponentScan(basePackageClasses = ServiceProviderController.class9)
@EnableMongoRepositories(basePackageClasses = ServiceProviderRepository.class)
public class Application implements CommandLineRunner { 

    @Autowired
    private ServiceProviderRepository repository;

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

    @Override
    public void run(String... args) {
        repository.deleteAll();
        repository.save(new ServiceProvider(1234, "Provider1"));
    }

    @Bean
    public RestTemplate restTemplate(RestTemplateBuilder builder) {
        return builder.build(); 
    }
}

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>com.rquinlan</groupId>
    <artifactId>sti-pa-POC</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>sti-pa-POC Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.2.RELEASE</version>
    </parent>
    <repositories>
        <repository>
            <id>nexus</id>
            <url>.....</url>
        </repository>
    </repositories>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.0</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.0</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.auth0</groupId>
            <artifactId>java-jwt</artifactId>
            <version>3.3.0</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </dependency>
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20090211</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-jersey-jaxrs</artifactId>
            <version>1.5.0</version>
        </dependency>

    </dependencies>
    <build>
        <finalName>sti-pa-POC</finalName>
    </build>
</project>

目标是通过控制器通过端点访问存储库,但现在我只是在App.java中进行操作以实现功能。我已经在spring指南网站上完成了Customer教程,并且可以运行它,并且我在这里使用的格式基本相同。当我尝试通过Maven运行它时,我得到了堆栈跟踪

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'application': Post-processing of merged bean definition failed; nested exception is java.lang.IllegalStateException: Failed to introspect Class [app.Application] from ClassLoader [org.springframework.boot.devtools.restart.classloader.RestartClassLoader@1aa76ad7]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:556) ~[spring-beans-5.0.6.RELEASE.jar:5.0.6.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:501) ~[spring-beans-5.0.6.RELEASE.jar:5.0.6.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317) ~[spring-beans-5.0.6.RELEASE.jar:5.0.6.RELEASE]
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) ~[spring-beans-5.0.6.RELEASE.jar:5.0.6.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315) ~[spring-beans-5.0.6.RELEASE.jar:5.0.6.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.0.6.RELEASE.jar:5.0.6.RELEASE]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:760) ~[spring-beans-5.0.6.RELEASE.jar:5.0.6.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:869) ~[spring-context-5.0.6.RELEASE.jar:5.0.6.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.0.6.RELEASE.jar:5.0.6.RELEASE]
        at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE]
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759) [spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE]
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:395) [spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:327) [spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1255) [spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1243) [spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE]
        at app.Application.main(Application.java:40) [classes/:na]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_171]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_171]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_171]
        at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_171]
        at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-2.0.2.RELEASE.jar:2.0.2.RELEASE]
Caused by: java.lang.IllegalStateException: Failed to introspect Class [app.Application] from ClassLoader [org.springframework.boot.devtools.restart.classloader.RestartClassLoader@1aa76ad7]
        at org.springframework.util.ReflectionUtils.getDeclaredFields(ReflectionUtils.java:758) ~[spring-core-5.0.6.RELEASE.jar:5.0.6.RELEASE]
        at org.springframework.util.ReflectionUtils.doWithLocalFields(ReflectionUtils.java:690) ~[spring-core-5.0.6.RELEASE.jar:5.0.6.RELEASE]
        at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.buildResourceMetadata(CommonAnnotationBeanPostProcessor.java:355) ~[spring-context-5.0.6.RELEASE.jar:5.0.6.RELEASE]
        at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.findResourceMetadata(CommonAnnotationBeanPostProcessor.java:339) ~[spring-context-5.0.6.RELEASE.jar:5.0.6.RELEASE]

        at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessMergedBeanDefinition(CommonAnnotationBeanPostProcessor.java:298) ~[spring-context-5.0.6.RELEASE.jar:5.0.6.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyMergedBeanDefinitionPostProcessors(AbstractAutowireCapableBeanFactory.java:1020) ~[spring-beans-5.0.6.RELEASE.jar:5.0.6.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553) ~[spring-beans-5.0.6.RELEASE.jar:5.0.6.RELEASE]
        ... 20 common frames omitted
Caused by: java.lang.NoClassDefFoundError: org/springframework/data/mongodb/repository/MongoRepository
        at java.lang.ClassLoader.defineClass1(Native Method) ~[na:1.8.0_171]
        at java.lang.ClassLoader.defineClass(ClassLoader.java:763) ~[na:1.8.0_171]
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142) ~[na:1.8.0_171]
        at java.net.URLClassLoader.defineClass(URLClassLoader.java:467) ~[na:1.8.0_171]
        at java.net.URLClassLoader.access$100(URLClassLoader.java:73) ~[na:1.8.0_171]
        at java.net.URLClassLoader$1.run(URLClassLoader.java:368) ~[na:1.8.0_171]
        at java.net.URLClassLoader$1.run(URLClassLoader.java:362) ~[na:1.8.0_171]
        at java.security.AccessController.doPrivileged(Native Method) ~[na:1.8.0_171]
        at java.net.URLClassLoader.findClass(URLClassLoader.java:361) ~[na:1.8.0_171]
        at org.springframework.boot.devtools.restart.classloader.RestartClassLoader.findClass(RestartClassLoader.java:163) ~[spring-boot-devtools-2.0.2.RELEASE.jar:2.0.2.RELEASE]
        at org.springframework.boot.devtools.restart.classloader.RestartClassLoader.loadClass(RestartClassLoader.java:145) ~[spring-boot-devtools-2.0.2.RELEASE.jar:2.0.2.RELEASE]
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_171]
        at java.lang.Class.getDeclaredFields0(Native Method) ~[na:1.8.0_171]
        at java.lang.Class.privateGetDeclaredFields(Class.java:2583) ~[na:1.8.0_171]
        at java.lang.Class.getDeclaredFields(Class.java:1916) ~[na:1.8.0_171]
        at org.springframework.util.ReflectionUtils.getDeclaredFields(ReflectionUtils.java:753) ~[spring-core-5.0.6.RELEASE.jar:5.0.6.RELEASE]
        ... 26 common frames omitted
Caused by: java.lang.ClassNotFoundException: org.springframework.data.mongodb.repository.MongoRepository
        at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[na:1.8.0_171]
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_171]
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349) ~[na:1.8.0_171]
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_171]
        at org.springframework.boot.devtools.restart.classloader.RestartClassLoader.loadClass(RestartClassLoader.java:148) ~[spring-boot-devtools-2.0.2.RELEASE.jar:2.0.2.RELEASE]
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_171]
        ... 42 common frames omitted

独立应用程序和尝试将其合并到实际应用程序中时的格式基本相同,因此我无法弄清楚是什么导致了错误。任何帮助表示赞赏。谢谢。

1 个答案:

答案 0 :(得分:0)

这里的事物:

  1. 您需要将您的模型标记为@Document

    @文档 公共类ServiceProvider {

    @Id
    private String id;
    
    private int providerId;
    private String username;
    //...
    

    }

  2. 由于@ComponentScan启用了@EnableMongoRepositories,因此您不需要@SpringBootApplication@ComponentScan。查找文档here

    @SpringBootApplication
    // @ComponentScan(basePackageClasses = ServiceProviderController.class9)
    // @EnableMongoRepositories(basePackageClasses = ServiceProviderRepository.class)
    public class Application implements CommandLineRunner { 
    
        @Autowired
        private ServiceProviderRepository repository;
        // ...
    }
    

最后,您需要告知spring为MongoDB创建上下文:

application.yml

spring:
  data:
    mongodb:
      database: test
      host: localhost
      port: 27017

您可以在GitHub上找到最简单的完全可用的项目启动程序版本