无法使用 Spring 数据 Couchbase 将 JSON 对象从 Couchbase 映射到 DTO 实体
Can't map the JSON object from Couchbase to DTO entity using Spring Data Couchbase
我已经创建了一些数据模型对象来插入 Couchbase 并从中读取。它有简单的类型和 2 个字段是其他 DTO 对象。
@Data
@AllArgsConstructor
@Document
public class Customer {
@Id
private int id;
private String fullName;
private String phoneNumber;
private String address;
private Date registrationDate;
private boolean isBusiness;
private String status;
private Tariff currentTariff;
private BillingAccount billingAccount;
}
因此,我使用逻辑创建了端点以创建 10000 个随机客户对象,然后 repository.saveAll(customers);
我可以在 Couchbase 中看到添加的数据 UI
但是 我想从 Couchbase 获取所有这些客户对象。这是我的代码
@GetMapping("/findAllCustomers")
public Iterable<Customer> getAll() {
return repository.findAll();
}
非常简单,没有自定义转换,没有其他复杂的东西。我期望的类型正是我生成和保存此数据所用的类型。
我收到以下错误:
Failed to instantiate com.bachelor.boostr.model.Customer using
constructor public com.bachelor.boostr.model.Customer
Caused by: java.lang.ClassCastException: java.lang.String cannot be
cast to java.lang.Integer\r\n\tat
com.bachelor.boostr.model.Customer_Instantiator_z47nsm.newInstance(Unknown
Source)\r\n\tat
org.springframework.data.convert.ClassGeneratingEntityInstantiator$EntityInstantiatorAdapter.createInstance(ClassGeneratingEntityInstantiator.java:226)\r\n\t...
请帮忙
我删除了@AllArgsConstructor Lombok Annotation,并创建了没有 Id 字段的构造函数
@Data
@Document
public class Customer {
@Id
@GeneratedValue(strategy = GenerationStrategy.UNIQUE)
private String id;
private String fullName;
private String phoneNumber;
private String address;
private Date registrationDate;
private boolean isBusiness;
private String status;
private Tariff currentTariff;
private BillingAccount billingAccount;
public Customer(String fullName, String phoneNumber, String address, Date registrationDate, boolean isBusiness, String status, Tariff currentTariff, BillingAccount billingAccount) {
this.fullName = fullName;
this.phoneNumber = phoneNumber;
this.address = address;
this.registrationDate = registrationDate;
this.isBusiness = isBusiness;
this.status = status;
this.currentTariff = currentTariff;
this.billingAccount = billingAccount;
}
}
之后一切正常。读取和写入操作。
我已经创建了一些数据模型对象来插入 Couchbase 并从中读取。它有简单的类型和 2 个字段是其他 DTO 对象。
@Data
@AllArgsConstructor
@Document
public class Customer {
@Id
private int id;
private String fullName;
private String phoneNumber;
private String address;
private Date registrationDate;
private boolean isBusiness;
private String status;
private Tariff currentTariff;
private BillingAccount billingAccount;
}
因此,我使用逻辑创建了端点以创建 10000 个随机客户对象,然后 repository.saveAll(customers);
我可以在 Couchbase 中看到添加的数据 UI
但是 我想从 Couchbase 获取所有这些客户对象。这是我的代码
@GetMapping("/findAllCustomers")
public Iterable<Customer> getAll() {
return repository.findAll();
}
非常简单,没有自定义转换,没有其他复杂的东西。我期望的类型正是我生成和保存此数据所用的类型。
我收到以下错误:
Failed to instantiate com.bachelor.boostr.model.Customer using constructor public com.bachelor.boostr.model.Customer
Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer\r\n\tat com.bachelor.boostr.model.Customer_Instantiator_z47nsm.newInstance(Unknown Source)\r\n\tat org.springframework.data.convert.ClassGeneratingEntityInstantiator$EntityInstantiatorAdapter.createInstance(ClassGeneratingEntityInstantiator.java:226)\r\n\t...
请帮忙
我删除了@AllArgsConstructor Lombok Annotation,并创建了没有 Id 字段的构造函数
@Data
@Document
public class Customer {
@Id
@GeneratedValue(strategy = GenerationStrategy.UNIQUE)
private String id;
private String fullName;
private String phoneNumber;
private String address;
private Date registrationDate;
private boolean isBusiness;
private String status;
private Tariff currentTariff;
private BillingAccount billingAccount;
public Customer(String fullName, String phoneNumber, String address, Date registrationDate, boolean isBusiness, String status, Tariff currentTariff, BillingAccount billingAccount) {
this.fullName = fullName;
this.phoneNumber = phoneNumber;
this.address = address;
this.registrationDate = registrationDate;
this.isBusiness = isBusiness;
this.status = status;
this.currentTariff = currentTariff;
this.billingAccount = billingAccount;
}
}
之后一切正常。读取和写入操作。