Problem with variables Date in Spring Boot - Angular 6

0

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() { }
}
    
asked by Carlos Devesa 23.11.2018 в 21:13
source

1 answer

0

With respect to typeof the documentation says:

  

The typeof operator returns a string indicating the type of the operand   without evaluating it.

Of which you can return:

  • function
  • string
  • number
  • object
  • undefined
  • boolean
  • So it's normal for you to return as output 'object' .

    On the other hand, if you want to receive the formatted field as a date, you can add the following to your application.properties file:

    spring.jackson.serialization.write-dates-as-timestamps=false
    

    Or in its respective format if you use a application.yml .

    With this you achieve that the result is of the following form:

    {
        "id": 1,
        "gender": "X",
        "birthdate": "2018-11-23"
    }
    

    In version 2.1.0 of Spring Boot this parameter already comes by default to true in which case the result is shown in the following way:

    {
        "id": 1,
        "gender": "X",
        "birthdate": "2018-11-24T02:32:09.673+0000"
    }
    

    Whose formats you can now work with javascript , for example:

    new Date("2018-11-24T02:32:09.673+0000")
    Fri Nov 23 2018 20:32:09 GMT-0600 (hora estándar central)
    
    new Date("2018-11-23")
    Thu Nov 22 2018 18:00:00 GMT-0600 (hora estándar central)
    

    References

  • typeof
  • WRITE_DATES_AS_TIMESTAMPS
  • answered by 24.11.2018 в 03:38