4

In a spring rest application, i send a objet who contain a date

{ appointmentId: "", appointmentTypeId: "1", appointmentDate: "2015-12-08T08:00:00-05:00" }

In my dto side

for my appointmentDate i have

@DateTimeFormat(iso=DateTimeFormat.ISO.DATE_TIME)
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
private LocalDateTime appointmentDate;

In my dependencies i have jackson-datatype-jsr310-2.6.3

I get this error

rg.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Text '2015-12-08T13:00:00.000Z' could not be parsed, unparsed text found at index 23 (through reference chain: server.dto.AppointmentDto["appointmentDate"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Text '2015-12-08T13:00:00.000Z' could not be parsed, unparsed text found at index 23 (through reference chain: server.dto.AppointmentDto["appointmentDate"])

tried only with DateTimeFormat, only with JsonDeserialize and both, but get the same error.

Edit

@RequestMapping(value = "/rest")
@RestController
public class LodgerController {

    @RequestMapping(value = "/lodgers/{lodgerId}/appointments", method = RequestMethod.POST)
    public Long createAppointmentsByLodgerId(@PathVariable("lodgerId") Long lodgerId, @RequestBody AppointmentDto appointmentDto) {
        return appointmentService.save(appointmentDto);
    }
}

public class AppointmentDto {

    private Long appointmentId;
    private Long appointmentTypeId;     
    private Long lodgerId;

    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    private LocalDateTime appointmentDate;

    public AppointmentDto() {

    }
}

<form id="lodgerAppointmentForm" class="form-horizontal" role="form">
    <input type="hidden" id="lodgerId" name="lodgerId">
    <input type="hidden" id="lodgerAppointmentId" name="appointmentId">
    <div class="form-group">
        <label for="lodgerAppointmentDate" class="col-sm-2 control-label">Date</label>
        <div class="col-sm-10">
            <div class="input-group date" id="appointmentDatepicker" >
                <input type="text" class="form-control" id="lodgerAppointmentDate" name="appointmentDate">
                <span class="input-group-addon">
                    <span class="glyphicon glyphicon-calendar">
                    </span>
                </span>
            </div>
        </div>
    </div>
</form>

var locale = navigator.languages ? navigator.languages[0] : (navigator.language || navigator.userLanguage);
moment().locale(locale);

$('#appointmentDatepicker').datetimepicker({
    format: 'DD/MM/YYYY H:mm',
    allowInputToggle: true
});

var lodgerId = $('#lodgerId').val();

var type = "post";
var url = "http://localhost:8080/rest/lodgers/" + lodgerId + "/appointments";

var data = transForm.serialize('#lodgerAppointmentForm');
data.appointmentDate = $('#appointmentDatepicker').data('DateTimePicker').date().format();
data.lodgerId = lodgerId;
data = JSON.stringify(data);

jQuery.ajax({
    type: type,
    url: url,
    contentType: "application/json",
    data: data,
    success: function (data, status, jqXHR) {
    },
    error: function (jqXHR, status) {
    }
});

transform.js come from https://github.com/A1rPun/transForm.js/blob/master/src/transForm.js bootstrap datetimepicker come from https://github.com/Eonasdan/bootstrap-datetimepicker

Moment use 2015-12-09T08:00:00-05:00 (ISO 8601) DateTimeFormatter.ISO_LOCAL_DATE_TIME who is: 2015-12-09T08:00:00 (ISO 8601)

both don't seem to use the same format

robert trudel
  • 5,283
  • 17
  • 72
  • 124

1 Answers1

4

I think that your problem is described here:

https://github.com/FasterXML/jackson-datatype-jsr310/issues/14

I encounter the same error when was playing around with LocalDateTime and REST API. The problem is that you can serialize LocalDateTime to something like this:

2015-12-27T16:59:29.959

And you can create valid Date object in JavaScript from that string.

On the other hand if you try to POST/PUT JavaScript date to server then this:

var myDate = new Date();
JSON.stringify(myDate);

will create string like this (with extra Z - which stands for zulu time/UTC time zone):

2015-12-27T16:59:29.959Z

And that extra time zone info causes error in your case during deserialization because LocalDateTime don`t have time zone.

You can try to use ZonedDateTime on ther server or format by yourself date string on client side before sending (without Z suffiks).

bary
  • 1,699
  • 2
  • 15
  • 24