Spring Boot + SPring REST + Swagger + JPA +内存中的 H2 DB
Spring Boot + SPring REST + Swagger + JPA +inmemory H2 DB
我正在使用 Spring Boot 开发 Spring REST。我首先将值硬编码为 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;
}
但后来我尝试集成 JPA 并将 bean 转换为实体,如下所示,起初我在创建名称为 'documentationpluginsbootstrapper' 的 bean 时出错,但通过从 SwaggerConfig class.But 中删除 @Configuration 注释解决了后来它以另一个异常结束 Error creating bean with name 'repositorySearchController' ..Log 显示在最后
@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
}
日志:
org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为 'repositorySearchController' 的 bean 时出错...... spring-data-rest-webmvc-3.0.8.RELEASE.jar
需要输入来解决这个问题。
要连接到 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
并且您可以从 URL 访问 BD:http://localhost:8080/h2-console/
我正在使用 Spring Boot 开发 Spring REST。我首先将值硬编码为 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;
}
但后来我尝试集成 JPA 并将 bean 转换为实体,如下所示,起初我在创建名称为 'documentationpluginsbootstrapper' 的 bean 时出错,但通过从 SwaggerConfig class.But 中删除 @Configuration 注释解决了后来它以另一个异常结束 Error creating bean with name 'repositorySearchController' ..Log 显示在最后
@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
}
日志:
org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为 'repositorySearchController' 的 bean 时出错...... spring-data-rest-webmvc-3.0.8.RELEASE.jar
需要输入来解决这个问题。
要连接到 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
并且您可以从 URL 访问 BD:http://localhost:8080/h2-console/