使用spring,jackson格式化LocalDateTime

在春季休息应用程序中,我发送一个包含日期的对象

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

在我的dto方面

对于我的约会日期我有

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

在我的依赖中,我有
杰克逊的数据类型,jsr310-2.6.3

我收到这个错误

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”])

仅使用DateTimeFormat尝试,只使用JsonDeserialize和两者,但得到相同的错误.

编辑

@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来自https://github.com/A1rPun/transForm.js/blob/master/src/transForm.js
bootstrap datetimepicker来自https://github.com/Eonasdan/bootstrap-datetimepicker

瞬间使用2015-12-09T08:00:00-05:00(ISO 8601)
 DateTimeFormatter.ISO_LOCAL_DATE_TIME:2015-12-09T08:00:00(ISO 8601)

两者似乎都没有使用相同的格式

最佳答案
我认为你的问题在这里描述:

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

在使用LocalDateTime和REST API时遇到了同样的错误.问题是你可以将LocalDateTime序列化为这样的东西:

2015-12-27T16:59:29.959

您可以从该字符串在JavaScript中创建有效的Date对象.

另一方面,如果您尝试将POST / PUT JavaScript日期发送到服务器,那么:

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

将创建这样的字符串(带有额外的Z – 代表祖鲁时间/ UTC时区):

2015-12-27T16:59:29.959Z

并且在反序列化期间,额外的时区信息会导致您的情况出错,因为LocalDateTime没有时区.

你可以尝试在服务器上使用ZonedDateTime,或者在发送之前自己格式化客户端上的日期字符串(没有Z suffiks).

转载注明原文:使用spring,jackson格式化LocalDateTime - 代码日志