[英]How to send Time and Date value from Postman to my REST API
I have a Schedules class as a Table in postgres with jamMulai, jamSelesai variable with a LocalTime data type, and tglTayang variable with a LocalDate variable我在 postgres 中有一个带有 jamMulai 的表、带有 LocalTime 数据类型的 jamSelesai 变量和带有 LocalDate 变量的 tglTayang 变量的 Schedules 类
@Entity
@Getter
@Setter
@Table(name = "schedules")
public class Schedules implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(columnDefinition = "serial")
private Long scheduleId;
@Column(name = "tgl_tayang",columnDefinition = "DATE")
private LocalDate tglTayang;
@Column(name = "jam_mulai",columnDefinition = "TIME")
private LocalTime jamMulai;
@Column(name = "jam_selesai" ,columnDefinition = "TIME")
private LocalTime jamSelesai;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "film_id")
private Films filmId;
This is my service method for the Schedules class这是我的 Schedules 类的服务方法
@Override
public void saveSchedule(String jamMulai, String jamSelesai, String tglTayang, Long filmId) {
Schedules schedules = new Schedules();
schedules.setJamMulai(LocalTime.parse(jamMulai));
schedules.setJamSelesai(LocalTime.parse(jamSelesai));
schedules.setTglTayang(LocalDate.parse(tglTayang));
Films films = filmsService.findFilmById(filmId);
schedules.setFilmId(films);
schedulesRepository.save(schedules);
}
and this is my controller method这是我的控制器方法
@PostMapping("/admin/add-schedule")
public String addSchedule(@RequestBody Map<String, Object> schedule) {
schedulesService.saveSchedule((String) schedule.get("jamMulai"), (String) schedule.get("jamSelesai"), (String) schedule.get("tglTayang"), (Long) schedule.get("filmId"));
return "Add Films Success!";
}
in the Postgres database the data type for jamMulai, jamSelesai, and tglTayang is time without time zone and date.在 Postgres 数据库中,jamMulai、jamSelesai 和 tglTayang 的数据类型是没有时区和日期的时间。
But when I try to request Post using Postman但是当我尝试使用 Postman 请求 Post
{
"jamMulai":"20:00:00",
"jamSelesai":"23:00:00",
"tglTayang":"2020-05-12",
"filmId":2
}
I get我明白了
{
"timestamp": "2022-05-17T13:41:52.771+00:00",
"status": 500,
"error": "Internal Server Error",
"path": "/cinema/schedules/admin/add-schedule"
}
I've tried changing the data type in Schedules class to a different time and date but it still doesn't work.我尝试将 Schedules 类中的数据类型更改为不同的时间和日期,但它仍然不起作用。
How can I fix this?我怎样才能解决这个问题? Thanks
谢谢
ps: I know there's a similar question to this, but it doesn't solve my issue ps:我知道有一个类似的问题,但这并不能解决我的问题
You said: in the Postgres database the data type for jamMulai, jamSelesai, and tglTayang is time without time zone and date.您说:在 Postgres 数据库中,jamMulai、jamSelesai 和tglTayang的数据类型是没有时区和日期的时间。
Why do you define:为什么要定义:
@Column(name = "tgl_tayang",columnDefinition = "DATE")
private LocalDate tglTayang;
Shouldn't it be:不应该是:
@Column(name = "tgl_tayang",columnDefinition = "TIME")
private LocalTime tglTayang;
? ?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.