Skip to content

Commit e577df9

Browse files
authored
BAEL-2495 fixing InvalidDefinitionException (eugenp#9272)
* BAEL-2495 fixing InvalidDefinitionException * BAEL-2495 minor changes
1 parent 3755ad5 commit e577df9

File tree

2 files changed

+59
-8
lines changed

2 files changed

+59
-8
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.baeldung.jackson.date;
2+
3+
import com.fasterxml.jackson.annotation.JsonFormat;
4+
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
5+
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
6+
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
7+
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
8+
9+
import java.time.LocalDate;
10+
11+
public class EventWithLocalDate {
12+
public String name;
13+
14+
@JsonDeserialize(using = LocalDateDeserializer.class)
15+
@JsonSerialize(using = LocalDateSerializer.class)
16+
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy")
17+
public LocalDate eventDate;
18+
19+
public EventWithLocalDate() {}
20+
21+
public EventWithLocalDate(final String name, final LocalDate eventDate) {
22+
this.name = name;
23+
this.eventDate = eventDate;
24+
}
25+
26+
public LocalDate getEventDate() {
27+
return eventDate;
28+
}
29+
30+
public String getName() {
31+
return name;
32+
}
33+
}

jackson-modules/jackson-conversions/src/test/java/com/baeldung/jackson/date/JacksonDateUnitTest.java

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.io.IOException;
99
import java.text.ParseException;
1010
import java.text.SimpleDateFormat;
11+
import java.time.LocalDate;
1112
import java.time.LocalDateTime;
1213
import java.time.ZoneId;
1314
import java.time.ZonedDateTime;
@@ -18,11 +19,6 @@
1819
import org.joda.time.DateTimeZone;
1920
import org.junit.Test;
2021

21-
import com.baeldung.jackson.date.Event;
22-
import com.baeldung.jackson.date.EventWithFormat;
23-
import com.baeldung.jackson.date.EventWithJodaTime;
24-
import com.baeldung.jackson.date.EventWithLocalDateTime;
25-
import com.baeldung.jackson.date.EventWithSerializer;
2622
import com.fasterxml.jackson.core.JsonProcessingException;
2723
import com.fasterxml.jackson.databind.ObjectMapper;
2824
import com.fasterxml.jackson.databind.SerializationFeature;
@@ -57,7 +53,7 @@ public void whenSerializingDateToISO8601_thenSerializedToText() throws JsonProce
5753

5854
final ObjectMapper mapper = new ObjectMapper();
5955
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
60-
56+
6157
// StdDateFormat is ISO8601 since jackson 2.9
6258
mapper.setDateFormat(new StdDateFormat().withColonInTimeZone(true));
6359

@@ -143,7 +139,7 @@ public void whenSerializingJava8DateWithCustomSerializer_thenCorrect() throws Js
143139
}
144140

145141
@Test
146-
public void whenDeserializingDateWithJackson_thenCorrect() throws JsonProcessingException, IOException {
142+
public void whenDeserializingDateWithJackson_thenCorrect() throws IOException {
147143
final String json = "{\"name\":\"party\",\"eventDate\":\"20-12-2014 02:30:00\"}";
148144

149145
final SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
@@ -156,7 +152,7 @@ public void whenDeserializingDateWithJackson_thenCorrect() throws JsonProcessing
156152
}
157153

158154
@Test
159-
public void whenDeserializingDateUsingCustomDeserializer_thenCorrect() throws JsonProcessingException, IOException {
155+
public void whenDeserializingDateUsingCustomDeserializer_thenCorrect() throws IOException {
160156
final String json = "{\"name\":\"party\",\"eventDate\":\"20-12-2014 02:30:00\"}";
161157

162158
final SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
@@ -179,6 +175,28 @@ public void whenSerializingJava8Date_thenCorrect() throws JsonProcessingExceptio
179175
assertThat(result, containsString("2014-12-20T02:30"));
180176
}
181177

178+
@Test
179+
public void whenSerializingJava8DateAndReadingValue_thenCorrect() throws IOException {
180+
String stringDate = "\"2014-12-20\"";
181+
182+
ObjectMapper mapper = new ObjectMapper();
183+
mapper.registerModule(new JavaTimeModule());
184+
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
185+
186+
LocalDate result = mapper.readValue(stringDate, LocalDate.class);
187+
assertThat(result.toString(), containsString("2014-12-20"));
188+
}
189+
190+
@Test
191+
public void whenSerializingJava8DateAndReadingFromEntity_thenCorrect() throws IOException {
192+
String json = "{\"name\":\"party\",\"eventDate\":\"20-12-2014\"}";
193+
194+
ObjectMapper mapper = new ObjectMapper();
195+
196+
EventWithLocalDate result = mapper.readValue(json, EventWithLocalDate.class);
197+
assertThat(result.getEventDate().toString(), containsString("2014-12-20"));
198+
}
199+
182200
@Test
183201
public void whenSerializingJodaTime_thenCorrect() throws JsonProcessingException {
184202
final DateTime date = new DateTime(2014, 12, 20, 2, 30, DateTimeZone.forID("Europe/London"));

0 commit comments

Comments
 (0)