我正在嘗試將 JSON 文件反序列化為 POJO 對(duì)象,但出現(xiàn)錯(cuò)誤。JSON 文件看起來(lái)像 (NotificationPOST.json) { "endDate":"2018-12-27", "malfunctionStartDate":"2018-11-20T22:22:22", "malfunctionEndDate":"2018-11-21T12:34:46" }POJO類(lèi)有 @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 類(lèi)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)); }}我試圖運(yùn)行的測(cè)試類(lèi)@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貢獻(xiàn)2016條經(jīng)驗(yàn) 獲得超9個(gè)贊
您可以添加jackson-datatype-joda
到您的依賴(lài)項(xiàng),然后JodaModule
在您的ObjectMapper
:
ObjectMapper mapper = new ObjectMapper(); mapper.registerModule(new JodaModule());
添加回答
舉報(bào)
0/150
提交
取消