From 78ad3920c5f4bb07fedad5614a60b8ca18baf3a1 Mon Sep 17 00:00:00 2001 From: mperor Date: Thu, 6 Mar 2025 22:45:41 +0100 Subject: [PATCH] Jackson tester example --- spring-web-lab/README.md | 3 +- .../src/main/resources/application.yml | 6 ++- .../lab/spring/jackson/JacksonTest.java | 47 +++++++++++++++++++ 3 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 spring-web-lab/src/test/java/pl/mperor/lab/spring/jackson/JacksonTest.java diff --git a/spring-web-lab/README.md b/spring-web-lab/README.md index 45022aa..e57bce6 100644 --- a/spring-web-lab/README.md +++ b/spring-web-lab/README.md @@ -2,4 +2,5 @@ ## Sources 🔗 -* [Java Senior Quiz 🧩](https://youtube.com/playlist?list=PLO5muTI694A5yRrEDNY1czqEUVbYHG_AH&si=KteWFowVcOSTQ8F-) \ No newline at end of file +* [Java Senior Quiz 🧩](https://youtube.com/playlist?list=PLO5muTI694A5yRrEDNY1czqEUVbYHG_AH&si=KteWFowVcOSTQ8F-) +* [Action Jackson! Effective JSON processing in Spring Boot Applications by Joris Kuipers @ Spring I/O](https://youtu.be/BhiF6e24l5k?si=IxS0X6FtgfWzdP-c) \ No newline at end of file diff --git a/spring-web-lab/src/main/resources/application.yml b/spring-web-lab/src/main/resources/application.yml index ec0d75f..b6c381d 100644 --- a/spring-web-lab/src/main/resources/application.yml +++ b/spring-web-lab/src/main/resources/application.yml @@ -6,4 +6,8 @@ #logging: # level: -# org.springframework.boot.actuate: DEBUG \ No newline at end of file +# org.springframework.boot.actuate: DEBUG + +#spring: +# jackson: +# default-property-inclusion: NON_NULL \ No newline at end of file diff --git a/spring-web-lab/src/test/java/pl/mperor/lab/spring/jackson/JacksonTest.java b/spring-web-lab/src/test/java/pl/mperor/lab/spring/jackson/JacksonTest.java new file mode 100644 index 0000000..7fdc321 --- /dev/null +++ b/spring-web-lab/src/test/java/pl/mperor/lab/spring/jackson/JacksonTest.java @@ -0,0 +1,47 @@ +package pl.mperor.lab.spring.jackson; + +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.json.JsonTest; +import org.springframework.boot.test.json.JacksonTester; +import org.springframework.boot.test.json.JsonContent; + +import java.io.IOException; +import java.util.Map; + +@JsonTest +public class JacksonTest { + + @Autowired + private JacksonTester sampleTester; + @Autowired + private JacksonTester> wrappedTester; + + record Sample(int integer, String string) {} + + @Test + void testSampleMarshalling() throws IOException { + var sample = new Sample(1, "Text"); + JsonContent content = sampleTester.write(sample); + Assertions.assertThat(content).isEqualToJson(""" + { + "integer" : 1, + "string" : "Text" + } + """ + ); + + var wrapped = Map.of("sample", sample); + Assertions.assertThat(wrappedTester.write(wrapped)).isEqualToJson(""" + { + "sample" : { + "integer" : 1, + "string" : "Text" + } + } + """ + ); + } + +}