简体   繁体   English

Spring Boot + SPRING REST + Swagger + JPA +内存H2 DB

[英]Spring Boot + SPring REST + Swagger + JPA +inmemory H2 DB

I am working on Spring REST using Spring Boot. 我正在使用Spring Boot进行Spring REST。 I started by hardcoding values to DAOService as below and everything working fine 我开始将值硬编码到DAOService ,如下所示,一切正常

@Component
public class UserDAOService {

    static List<User> users = new ArrayList<>();
    static int userCount = 3;

    static {
        users.add(new User("adam", new Date(), 1));
        users.add(new User("eve", new Date(), 2));
        users.add(new User("joe", new Date(), 3));
    }

    public User saveUser(User user) {
        if (user.getId() == null) {
            user.setId(++userCount);
        }
        users.add(user);
        return user;
    }

    public List<User> findAll() {
        System.out.println("finding all users");
        return users;
    }

    public User findOne(int id) {

        for (User user : users) {
            if (user.getId() == id) {
                return user;
            }
        }
        return null;
    }

But later i tried to integrate JPA and convert bean to entity as below , at first i got error creating bean with name 'documentationpluginsbootstrapper',but was resolved by removing @Configuration annotation from SwaggerConfig class.But later it ended up with another exception Error creating bean with name 'repositorySearchController' .. Log is shown at the end 但是后来我尝试集成JPA并将bean转换为实体,如下所示,起初我在创建名称为'documentationpluginsbootstrapper'的bean时遇到了错误,但是通过从SwaggerConfig类中删除@Configuration注释解决了,但是后来它又产生了另一个异常错误创建名称为'repositorySearchController'的bean末尾显示日志

    @ApiModel(description="all details about user")
    @Entity
    public class User {

        @Id
        @GeneratedValue
        private Integer id ;

        @Size(min=2,max=12,message="username should be atleast 2 characters")
        private String name;
        @Past
        private Date birthDate; 

        public User(String name, Date birthDate, Integer id) {
            super();
            this.name = name;
            this.birthDate = birthDate;
            this.id = id;
        }
        public User() {
            // TODO Auto-generated constructor stub
        } 
//setters and getters    
    }

log: 日志:

org.springframework.beans.factory.UnsatisfiedDependencyException : Error creating bean with name 'repositorySearchController' defined in ...... spring- data-rest-webmvc-3.0.8.RELEASE.jar org.springframework.beans.factory.UnsatisfiedDependencyException :创建名称为'repositorySearchController'的bean时出错,在...... spring- data-rest-webmvc-3.0.8.RELEASE.jar中定义

Need inputs to resolve this. 需要输入来解决此问题。

To connect to H2 database you should add the following properties to application.properties file: 要连接到H2数据库 ,应将以下属性添加到application.properties文件:

spring.h2.console.enabled=true
spring.h2.console.path=/h2-console

spring.datasource.url=jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.platform=h2
spring.datasource.initialize=true
spring.datasource.continue-on-error=true

and you can access to BD from URL: http://localhost:8080/h2-console/ 您可以从以下网址访问BD: http://localhost:8080/h2-console/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM