3

I have the below error when trying to create arraylist of an entity - 'timelog'.

{
    "timestamp": 1519372691473,
    "status": 400,
    "error": "Bad Request",
    "exception": "org.springframework.http.converter.
                  HttpMessageNotReadableException",
    "message": "JSON parse error: Can not deserialize value of type 
                java.time.LocalDate from String \"02-17-2018\": Text '02-17-
                2018' could not be parsed at index 0; nested exception is 
                com.fasterxml.jackson.databind.exc.InvalidFormatException: 
                Can not deserialize value of type java.time.LocalDate from 
                String \"02-17-2018\": Text '02-17-2018' could not be parsed 
                at index 0\n at [Source: 
                java.io.PushbackInputStream@83d3114; line: 3, column: 14] 
                (through reference chain: java.util.ArrayList[0] 
                >com.timesheet.model.Timelog[\"logDate\"])",
    "path": "/employee/101/timelog/tabdata"
}

I am sending a set of entity records through'post'method.I am getting error on executing the request body.

@RequestMapping(path = "employee/{id}/timelog/tabdata", method = 
RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
public void timeLogging(@RequestBody ArrayList<Timelog> tabData, 
@PathVariable("id") Integer id) {
    //Employee e = employeeRepository.findOne(id);
    //tabData.forEach(log -> {
    //log.setEmployee(e);
    //timeLogRepository.save(log);
    //});

    }

In my entity I am using a localdatedeserializer: Timelog entity code:

@Entity
@Table(name = "timelog", catalog = "timesheet")
public class Timelog implements java.io.Serializable {

    private static final long serialVersionUID = 1871977717947082854L;
    private Integer id;
    @JsonIgnore
    private Employee employee;

    @JsonDeserialize(using = LocalDateDeserializer.class)
    private LocalDate logDate;
    private String project;
    private Float timetaken;

//code for LocalDateDeserializer

public class LocalDateDeserializer extends JsonDeserializer<LocalDate> {
    public static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("MM-dd-yyyy");

    @Override
    public LocalDate deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
        return LocalDate.parse(p.getValueAsString(), FORMATTER);
    }
}

Can anyone help to identify why the localdatedeserializer is not been effective.Thanks in advance.

user9400503
  • 31
  • 1
  • 1
  • 3

1 Answers1

1

When passing dates and times over the network as dates and times you'll have to solve issues such as formatting it. If you have control over the front-end of the app I'd rather suggest you use milliseconds(which is just a 'long') and take care of presentation(formatting) when you really need it(usually a few places)

Sp3c7r00M
  • 79
  • 8