简体   繁体   中英

Spring Boot + SPring REST + Swagger + JPA +inmemory H2 DB

I am working on Spring REST using Spring Boot. I started by hardcoding values to DAOService as below and everything working fine

@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

    @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

Need inputs to resolve this.

To connect to H2 database you should add the following properties to application.properties file:

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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