When dealing with data of type Date
I am having problems. On the server ( Spring Boot
) I am collecting a Date field from MySQL
, and this information is correct in the Spring Boot Model when debugging. But if I see that call on the outside, the JSON generated has this field as TimeStamp . I guess the main error is this, but if later in Angular I try to parse this variable to Date and do a typeof, it returns that the variable is Object
.
Any ideas? I leave the code below:
[SPRING BOOT]:
Model:
@Entity
@Table(name="PATIENTS")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class Patient {
@Id
@GeneratedValue
private Long id;
@Column(name="gender")
private String gender;
@Column(name="birthdate")
@Temporal(TemporalType.DATE)
private Date birthdate;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public Date getBirthdate() {
return birthdate;
}
public void setBirthdate(Date birthdate) {
this.birthdate = birthdate;
}
}
Controller Code:
/**
* Obtenemos el modelo Patient a partir de su Id
* @param id
* @return
*/
@GetMapping("/patients/{id}")
public ResponseEntity<?> getPatient(@PathVariable("id") Long id) {
logger.debug("Llamada al método getPatient()");
Patient patient = patientService.getPatient(id);
if(patient == null) {
logger.error("No se ha encontrado al usuario " + id);
return new ResponseEntity(HttpStatus.NOT_FOUND);
}
return new ResponseEntity<Patient>(patient, HttpStatus.OK);
}
Service Code: (getOne method of JpaRepository)
public Patient getPatient(Long id) {
return patientDao.getOne(id);
}
[JSON]:
{"id":1,"gender":"F","birthdate":846194400000}
[ANGULAR 6]:
Service:
public getAllPatients() {
return this.http.get<Patient[]>(this._baseURL + '/all');
}
Model:
export class Patient {
id: number;
gender: string;
birthdate: Date;
constructor() { }
}