gpt4 book ai didi

java - Spring Boot + SPring REST + Swagger + JPA + 内存中 H2 DB

转载 作者:太空宇宙 更新时间:2023-11-04 10:20:22 25 4
gpt4 key购买 nike

我正在使用 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 类中删除 @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
}

日志:

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/

关于java - Spring Boot + SPring REST + Swagger + JPA + 内存中 H2 DB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51239538/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com