Angular2 Spring启动日期序列化 [英] Angular2 Spring boot date serialization

查看:122
本文介绍了Angular2 Spring启动日期序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Angular2前端制作REST API.在我的杰克逊的Spring配置中,我已经设置了spring.jackson.date-format=EEE MMM dd yyyy HH:mm:ss zzz (zzzz),因为我使用了bootstrap-datepicker插件,它输出的日期如下:Wed May 31 2017 00:00:00 GMT+0200 (W. Europe Daylight Time). 当我尝试将日期发布到具有这样的变量的DTO时private Date defaultDatetime; REST API返回400错误的请求错误.

I'm making a REST API with an Angular2 frontend. In my Spring configuration for jackson I have set this spring.jackson.date-format=EEE MMM dd yyyy HH:mm:ss zzz (zzzz) because I use the bootstrap-datepicker plugin which outputs dates like this: Wed May 31 2017 00:00:00 GMT+0200 (W. Europe Daylight Time). When I try to post a date to a DTO which has variables like this one private Date defaultDatetime; The REST API returns a 400 Bad request error.

{"timestamp":"mer. mai 03 2017 14:16:47",
"status":400,
"error":"Bad Request",
"exception":"org.springframework.http.converter.HttpMessageNotReadableException",
"message":"Could not read document: Can not construct instance of java.util.Date from String value '2017-05-01T22:00:00.000Z': not a valid representation (error: Failed to parse Date value '2017-05-01T22:00:00.000Z': Unparseable date: \"2017-05-01T22:00:00.000Z\")\n at [Source: java.io.PushbackInputStream@77b19daf; line: 1, column: 68] (through reference chain: ch.heigvd.form.api.dto.FormDTO[\"fields\"]->java.util.ArrayList[0]->ch.heigvd.form.api.dto.field.DateFieldDTO[\"defaultDate\"]); nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Can not construct instance of java.util.Date from String value '2017-05-01T22:00:00.000Z': not a valid representation (error: Failed to parse Date value '2017-05-01T22:00:00.000Z': Unparseable date: \"2017-05-01T22:00:00.000Z\")\n at [Source: java.io.PushbackInputStream@77b19daf; line: 1, column: 68] (through reference chain: ch.heigvd.form.api.dto.FormDTO[\"fields\"]->java.util.ArrayList[0]->ch.heigvd.form.api.dto.field.DateFieldDTO[\"defaultDate\"])",
"path":"/api/forms"}

任何想法我应该为杰克逊反序列化使用哪种日期格式?还是应该直接在前端更改格式?

Any idea what kind of date-format I should put for jackson deserializatrion? Or should I change the format directly in the frontend?

更新

我让它与自定义序列化程序一起工作.这是属性文件中的配置.

I got it working with a custom serializer. This is the configuration in the properties file.

spring.jackson.date-format = ch.heigvd.form.configuration.CustomJsonDateDeserializer spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS = false

spring.jackson.date-format=ch.heigvd.form.configuration.CustomJsonDateDeserializer spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS=false

这是序列化器:

public class CustomJsonDateDeserializer extends ISO8601DateFormat {

    @Override
    public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition fieldPosition) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
        toAppendTo.append(format.format(date));
        return toAppendTo;
    }

    @Override
    public Date parse(String source, ParsePosition pos) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
        try {
            return format.parse(source);
        } catch (ParseException var4) {
            return null;
        }
    }

}

推荐答案

您可以执行以下两个选项之一, 选项1: 由于它返回ISOFormat,因此请编写自己的解串器.

You can do either of the two options, Option 1: since it returns ISOFormat, write your own deserializer.

@JsonDeserialize(using=CustomerDateAndTimeDeserialize .class)
public class CustomJsonDateDeserializer extends JsonDeserializer<Date>
{
    @Override
    public Date deserialize(JsonParser jsonparser,
            DeserializationContext deserializationcontext) throws IOException, JsonProcessingException {

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        String date = jsonparser.getText();
        try {
            return format.parse(date);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }

    }
}

@JsonDeserialize(using = CustomJsonDateDeserializer.class)

选项2: 更改格式以匹配ISO字符串格式.

Option 2: Change your format to match ISO String format.

spring.jackson.date-format=YYYY-MM-dd'T'HH:mm:ss.SSS'Z'

这篇关于Angular2 Spring启动日期序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆