gpt4 book ai didi

java - 如何从我的代码程序中修复此错误

转载 作者:行者123 更新时间:2023-12-02 03:03:54 27 4
gpt4 key购买 nike

如何修复这个使用 Java Spring MVC 的程序?我想在数据库中输入包含日期的数据,但它显示错误。我该如何修复它?

这是我在实体中的代码

@Entity
@Table(name ="tb_transaksi_penjualan")
public class TransaksiPenjualan {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String customer;
@DateTimeFormat(pattern ="MM/dd/yyyy")
private Date tanggal;
private String barang;
private int jumlah;
private int harga;
private int total;
private Long customer_id;
private Long barang_id;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getCustomer() {
return customer;
}
public void setCustomer(String customer) {
this.customer = customer;
}
public Date getTanggal() {
return tanggal;
}
public void setTanggal(Date tanggal) {
this.tanggal = tanggal;
}
public String getBarang() {
return barang;
}
public void setBarang(String barang) {
this.barang = barang;
}
public int getJumlah() {
return jumlah;
}
public void setJumlah(int jumlah) {
this.jumlah = jumlah;
}
public int getHarga() {
return harga;
}
public void setHarga(int harga) {
this.harga = harga;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public Long getCustomer_id() {
return customer_id;
}
public void setCustomer_id(Long customer_id) {
this.customer_id = customer_id;
}
public Long getBarang_id() {
return barang_id;
}
public void setBarang_id(Long barang_id) {
this.barang_id = barang_id;
}



}

这是我来自 Controller 的代码

@Controller
@RequestMapping("/transaksipembelian/save")
public class TransaksiPembelianSaveController {


@Autowired
private TransaksiPembelianRepository transaksiPembelianRepository;

@ModelAttribute("transaksipembelian")
private TransaksiPembelian getTransaksiPembelian(@RequestParam(value="id", required = false) Long id)
{
if(id==null)
return new TransaksiPembelian();
else
return transaksiPembelianRepository.findById(id).orElse(null);
}

@RequestMapping(method = RequestMethod.GET)
public String form() {
return "tambahtransaksipembelian";
}

@RequestMapping(method = RequestMethod.POST)
public String submit(@ModelAttribute("transaksipembelian") TransaksiPembelian transaksipembelian ) {

System.out.println(transaksipembelian.toString());
transaksiPembelianRepository.save(transaksipembelian);

return "redirect:/transaksipembelian";
}

}

这是来自错误的消息:

Field error in object 'transaksipenjualan' on field 'tanggal': rejected value [2019-07-15]; codes [typeMismatch.transaksipenjualan.tanggal,typeMismatch.tanggal,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [transaksipenjualan.tanggal,tanggal]; arguments []; default message [tanggal]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'tanggal'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.format.annotation.DateTimeFormat java.util.Date] for value '2019-07-15'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [2019-07-15]]]

最佳答案

您看到的错误消息似乎表明您尝试使用 tanggal2019- 的文字日期字符串来保存/创建 TransaksiPenjualan 实体07-15 。根据 @DateTimeFormat 注释,您应该使用 MM/dd/yyyy 格式。

您可以通过传入文字 07/15/2019 来解决此问题。

关于java - 如何从我的代码程序中修复此错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57033521/

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