2

发现日期时间反序列化存在问题(nomad-sdk 版本 0.11.3.0)。

服务器(代理)版本:Nomad v1.0.1 (c9c68aa55a7275f22d2338f2df53e67ebfcb9238)

当我尝试从 nomad 代理(通过 API)获取分配列表时,出现以下错误:

完整的堆栈跟踪可以在这里找到:https ://pastebin.pl/view/9bf82a78

Caused by: com.fasterxml.jackson.databind.exc.InvalidFormatException: Can not deserialize value of type java.util.Date from String "2020-12-17T11:58:59.346780177+01:00": not a valid representation (error
: Failed to parse Date value '2020-12-17T11:58:59.346780177+01:00': Can not parse date "2020-12-17T11:58:59.346780177+0100": while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSSZ', parsing fails (leni
ency? null))

建议/测试的解决方法:

package com.hashicorp.nomad.apimodel;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.ISODateTimeFormat;

public class CustomDateDeserializer extends StdDeserializer<Date> {
    
    public CustomDateDeserializer() {
        super(Date.class);
    }

    @Override
    public Date deserialize(com.fasterxml.jackson.core.JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
        
        final String date = p.getText();
        if (date.equals("0001-01-01T00:00:00Z")) {
            return new Date();
        }
        DateTimeFormatter isoDateTimeFormat = ISODateTimeFormat.dateTime();
        return isoDateTimeFormat.parseDateTime(date).toDate();
    }
}


public abstract class NomadJson {

    static {
        OBJECT_MAPPER.setConfig(
                OBJECT_MAPPER.getSerializationConfig()
                        .with(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"))
        );

        SimpleModule simpleModule = new SimpleModule();
        simpleModule.addDeserializer(Date.class, new CustomDateDeserializer());
        OBJECT_MAPPER.registerModule(simpleModule);

    }

}

//Added  "CustomDateDeserializer"  to AllocDeploymentStatus.java


public final class AllocDeploymentStatus extends ApiObject {

    @JsonProperty("Timestamp")
    @JsonDeserialize(using = CustomDateDeserializer.class)
    public Date getTimestamp() {
        return timestamp;
    }
  
}


4

0 回答 0