Al tratar datos de tipo Date
estoy teniendo problemas. En el servidor (Spring Boot
) estoy recogiendo de MySQL
un campo Date, y esta información al Debuguear es correcta en el Modelo de Spring Boot. Pero si veo esa llamada por fuera, el JSON generado tiene este campo como TimeStamp. Supongo que el error principal es este, pero si más tarde en Angular intento parsear esta variable a Date y hago un typeof, me devuelve que la variable es un Object
.
¿Alguna idea? Dejo el código a continuación:
[SPRING BOOT]:
Modelo:
@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;
}
}
Código Controlador:
/**
* 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);
}
Código Service: (método getOne de JpaRepository)
public Patient getPatient(Long id) {
return patientDao.getOne(id);
}
[JSON]:
{"id":1,"gender":"F","birthdate":846194400000}
[ANGULAR 6]:
Servicio:
public getAllPatients() {
return this.http.get<Patient[]>(this._baseURL + '/all');
}
Modelo:
export class Patient {
id: number;
gender: string;
birthdate: Date;
constructor() { }
}