BAEL-2495 fixing InvalidDefinitionException

This commit is contained in:
patkorek
2020-05-11 13:48:07 +02:00
parent 67cefb4342
commit 20e1b768e6
2 changed files with 63 additions and 8 deletions

View File

@@ -0,0 +1,36 @@
package com.baeldung.jackson.date;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import java.time.LocalDate;
public class EventWithLocalDate {
public String name;
@JsonDeserialize(using = LocalDateDeserializer.class)
@JsonSerialize(using = LocalDateSerializer.class)
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy")
public LocalDate eventDate;
public EventWithLocalDate() {
super();
}
public EventWithLocalDate(final String name, final LocalDate eventDate) {
this.name = name;
this.eventDate = eventDate;
}
public LocalDate getEventDate() {
return eventDate;
}
public String getName() {
return name;
}
}

View File

@@ -7,6 +7,7 @@ import static org.junit.Assert.assertThat;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.TimeZone;
@@ -15,11 +16,6 @@ import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.junit.Test;
import com.baeldung.jackson.date.Event;
import com.baeldung.jackson.date.EventWithFormat;
import com.baeldung.jackson.date.EventWithJodaTime;
import com.baeldung.jackson.date.EventWithLocalDateTime;
import com.baeldung.jackson.date.EventWithSerializer;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
@@ -54,7 +50,7 @@ public class JacksonDateUnitTest {
final ObjectMapper mapper = new ObjectMapper();
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
// StdDateFormat is ISO8601 since jackson 2.9
mapper.setDateFormat(new StdDateFormat().withColonInTimeZone(true));
@@ -125,7 +121,7 @@ public class JacksonDateUnitTest {
}
@Test
public void whenDeserializingDateWithJackson_thenCorrect() throws JsonProcessingException, IOException {
public void whenDeserializingDateWithJackson_thenCorrect() throws IOException {
final String json = "{\"name\":\"party\",\"eventDate\":\"20-12-2014 02:30:00\"}";
final SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
@@ -138,7 +134,7 @@ public class JacksonDateUnitTest {
}
@Test
public void whenDeserializingDateUsingCustomDeserializer_thenCorrect() throws JsonProcessingException, IOException {
public void whenDeserializingDateUsingCustomDeserializer_thenCorrect() throws IOException {
final String json = "{\"name\":\"party\",\"eventDate\":\"20-12-2014 02:30:00\"}";
final SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
@@ -161,6 +157,29 @@ public class JacksonDateUnitTest {
assertThat(result, containsString("2014-12-20T02:30"));
}
@Test
public void whenSerializingJava8DateAndReadingValue_thenCorrect() throws IOException {
final String stringDate = "\"2014-12-20\"";
final ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
final String result2 = mapper.readValue(stringDate, LocalDate.class)
.toString();
assertThat(result2, containsString("2014-12-20"));
}
@Test
public void whenSerializingJava8DateAndReadingFromEntity_thenCorrect() throws IOException {
final String json = "{\"name\":\"party\",\"eventDate\":\"20-12-2014\"}";
final ObjectMapper mapper = new ObjectMapper();
final EventWithLocalDate result = mapper.readValue(json, EventWithLocalDate.class);
assertThat(result.getEventDate().toString(), containsString("2014-12-20"));
}
@Test
public void whenSerializingJodaTime_thenCorrect() throws JsonProcessingException {
final DateTime date = new DateTime(2014, 12, 20, 2, 30, DateTimeZone.forID("Europe/London"));