-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(track): dates are serialized as integer array instead of iso string
- Loading branch information
1 parent
cf2ecb5
commit f451e1a
Showing
3 changed files
with
50 additions
and
0 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
services/track/src/main/java/de/codecentric/habitcentric/track/CustomJsonSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package de.codecentric.habitcentric.track; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.SerializationFeature; | ||
import org.springframework.kafka.support.JacksonUtils; | ||
import org.springframework.kafka.support.serializer.JsonSerializer; | ||
|
||
/** | ||
* Custom Spring Kafka {@link JsonSerializer} that serializes timestamps as strings instead of | ||
* integer arrays. | ||
* | ||
* <p>For some reason, Spring Boot does not use the auto-configured {@link ObjectMapper} when | ||
* auto-configuring Spring Kafka. Instead, it calls the no-args constructor of the {@link | ||
* JsonSerializer} class provided by the property {@code spring.kafka.producer.value-serializer}. | ||
* | ||
* @param <T> | ||
* @see <a | ||
* href="https://github.com/spring-projects/spring-kafka/issues/680">https://github.com/spring-projects/spring-kafka/issues/680</a> | ||
* @see <a | ||
* href="https://docs.spring.io/spring-kafka/reference/tips.html#tip-json">https://docs.spring.io/spring-kafka/reference/tips.html#tip-json</a> | ||
*/ | ||
public class CustomJsonSerializer<T> extends JsonSerializer<T> { | ||
|
||
public CustomJsonSerializer() { | ||
super(createMapper()); | ||
} | ||
|
||
private static ObjectMapper createMapper() { | ||
var mapper = JacksonUtils.enhancedObjectMapper(); | ||
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); | ||
return mapper; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
services/track/src/test/java/de/codecentric/habitcentric/track/CustomJsonSerializerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package de.codecentric.habitcentric.track; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
class CustomJsonSerializerTest { | ||
|
||
@Test | ||
void creates() { | ||
try (var subject = new CustomJsonSerializer<>()) { | ||
assertThat(subject).isNotNull(); | ||
} | ||
} | ||
} |