使用spring,jackson格式化LocalDateTime

时间:2015-12-09 02:22:22

标签: spring jackson momentjs spring-restcontroller

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

  

{appointmentId:"",appointmentTypeId:" 1",appointmentDate:   #&34; 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:   无法阅读文件:文字' 2015-12-08T13:00:00.000Z'不可能   在索引23处找到解析的,未解析的文本(通过参考链:   server.dto.AppointmentDto [" appointmentDate"]);嵌套异常是   com.fasterxml.jackson.databind.JsonMappingException:Text   ' 2015-12-08T13:00:00.000Z'无法解析,未解析的文本在   指数23(通过参考链:   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)

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

1 个答案:

答案 0 :(得分:4)

我认为你的问题在这里描述:

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)。