当我尝试访问以下 REST 服务时,它返回所有数据以及“SupplierPayment”实体类“customerId”中不存在的字段。
@GET
@Produces(MediaType.APPLICATION_JSON)
public List<SupplierPayment> getAllSupplierPaymentsService() {
return (ArrayList<SupplierPayment>) supplierPaymentDao.getAllSupplierPayments();
}
这是 getAllSupplierPayments() 方法:
public List<SupplierPayment> getAllSupplierPayments() {
String query = "SELECT * FROM supplierpayment";
return (ArrayList<SupplierPayment>) getJdbcTemplate().query(query,
new BeanPropertyRowMapper<SupplierPayment>(SupplierPayment.class));
}
以下是“SupplierPayment”类中的所有字段:
private Integer supplierPaymentId;
private BigDecimal amount;
private Integer purchaseInvoiceId;
private Integer supplierId;
private Integer paymentMethodId;
private String description;
private Integer checkId;
private Integer fromBankAccountId;
private Integer toBankAccountId;
private String creditCardNo;
private Timestamp created;
private Integer createdBy;
当我调试时,我发现Web服务的ArrayList没有返回该字段,然后不知何故我在响应中找到了该字段。
我尝试截断并删除表格并逐个添加列。我发现只有当我将“supplierId”列添加到表中并且其值与“supplierId”相同时,该字段才会在响应中返回。我认为“customerId”列以前就存在,但我删除了它。
最佳答案
由于表中先前存在列,我发现SupplierPayment实体类中存在以下getter和setter:
public Integer getCustomerId() {
return supplierId;
}
public void setCustomerId(Integer customerId) {
this.supplierId = customerId;
}
删除它们后,问题就解决了。
关于java - REST Web 服务返回基础 SQL 查询中不存在的字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40938654/