亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

杰克遜日期從 JSON 文件反序列化錯誤

杰克遜日期從 JSON 文件反序列化錯誤

森林海 2022-12-15 16:39:59
我正在嘗試將 JSON 文件反序列化為 POJO 對象,但出現錯誤。JSON 文件看起來像 (NotificationPOST.json)  {   "endDate":"2018-12-27",   "malfunctionStartDate":"2018-11-20T22:22:22",   "malfunctionEndDate":"2018-11-21T12:34:46"   }POJO類有 @DateFormat(field = "Start Date", groups = Order.Level2.class)    @JsonSerialize(using = DateSerializer.class)    private DateTime startDate;    @DateFormat(field = "End Date", groups = Order.Level2.class)    @JsonSerialize(using = DateSerializer.class)    private DateTime endDate;自定義 DateSerializer 類import com.fasterxml.jackson.core.JsonGenerator;import com.fasterxml.jackson.core.JsonProcessingException;import com.fasterxml.jackson.databind.JsonSerializer;import com.fasterxml.jackson.databind.SerializerProvider;import java.io.IOException;import org.joda.time.DateTime;import org.joda.time.format.DateTimeFormat;import org.joda.time.format.DateTimeFormatter;public class DateSerializer extends JsonSerializer<DateTime> {    private static DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd");    public DateSerializer() {    }    public void serialize(DateTime value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException {        gen.writeString(formatter.print(value));    }}我試圖運行的測試類@BeforeClass    public static void init() {        notificationPOST = new File("src/test/resources/json/NotificationPOST.json");    }@Testpublic void notificationPayloadToEntityTest () throws IOException {private ObjectMapper objectMapper = new ObjectMapper();    NotificationPOST post = objectMapper.readValue(notificationPOST, NotificationPOST.class);}
查看完整描述

1 回答

?
慕沐林林

TA貢獻2016條經驗 獲得超9個贊

您可以添加jackson-datatype-joda到您的依賴項,然后JodaModule在您的ObjectMapper

ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JodaModule());


查看完整回答
反對 回復 2022-12-15
  • 1 回答
  • 0 關注
  • 97 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號