无法使用Spring Data Couchbase将JSON对象从Couchbase映射到DTO实体

问题描述 投票:0回答:1

我创建了一些数据模型对象以插入和读取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;
}

因此,我使用创建1万个随机客户对象的逻辑进行端点处理,然后执行了repository.saveAll(customers);

我可以在Couchbase UI中看到此数据

但是然后我想从Couchbase获取所有这些客户对象。这是我的代码

    @GetMapping("/findAllCustomers")
    public Iterable<Customer> getAll() {
        return repository.findAll();
    }

非常简单,没有自定义转换,没有其他复杂的东西。我期望的类型恰好是我用来生成和保存此数据的类型。

我收到以下错误:

无法使用实例化com.bachelor.boostr.model.Customer构造函数public com.bachelor.boostr.model.Customer

原因:java.lang.ClassCastException:java.lang.String不能为强制转换为java.lang.Integer \ r \ n \ tatcom.bachelor.boostr.model.Customer_Instantiator_z47nsm.newInstance(未知来源)\ r \ n \ tatorg.springframework.data.convert.ClassGeneratingEntityInstantiator $ EntityInstantiatorAdapter.createInstance(ClassGeneratingEntityInstantiator.java:226)\ r \ n \ t ...

请帮助

java spring couchbase spring-data-couchbase
1个回答
0
投票

我删除了@AllArgsConstructor Lombok注释,并创建了没有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;
    }
}

之后,它就很好了。读写操作。

© www.soinside.com 2019 - 2024. All rights reserved.