'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
@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
@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.
But when I try to request Post using Postman
{
"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.
How can I fix this? Thanks
ps: I know there's a similar question to this, but it doesn't solve my issue
Solution 1:[1]
You said: in the Postgres database the data type for jamMulai, jamSelesai, and tglTayang is time without time zone and date.
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;
?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | SeanH |
