From 9b8455e82992dff01cae00a1560f0d4ed750841c Mon Sep 17 00:00:00 2001 From: Adina Rolea Date: Tue, 27 Oct 2020 15:47:40 +0200 Subject: [PATCH 01/10] BAEL-4687: added spring boot jackson configuration example --- spring-boot-modules/pom.xml | 1 + .../spring-boot-jackson/pom.xml | 21 +++++++++ .../baeldung/boot/jackson/Application.java | 12 ++++++ .../jackson/config/CoffeeConfiguration.java | 43 +++++++++++++++++++ .../jackson/controller/CoffeeController.java | 25 +++++++++++ .../baeldung/boot/jackson/model/Coffee.java | 26 +++++++++++ .../boot/jackson/model/CoffeeResponse.java | 28 ++++++++++++ .../src/main/resources/application.properties | 3 ++ .../boot/jackson/CoffeeIntegrationTest.java | 32 ++++++++++++++ 9 files changed, 191 insertions(+) create mode 100644 spring-boot-modules/spring-boot-jackson/pom.xml create mode 100644 spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/Application.java create mode 100644 spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/config/CoffeeConfiguration.java create mode 100644 spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/controller/CoffeeController.java create mode 100644 spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/model/Coffee.java create mode 100644 spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/model/CoffeeResponse.java create mode 100644 spring-boot-modules/spring-boot-jackson/src/main/resources/application.properties create mode 100644 spring-boot-modules/spring-boot-jackson/src/test/java/com/baeldung/boot/jackson/CoffeeIntegrationTest.java diff --git a/spring-boot-modules/pom.xml b/spring-boot-modules/pom.xml index fa70a9f058..3d721d7147 100644 --- a/spring-boot-modules/pom.xml +++ b/spring-boot-modules/pom.xml @@ -68,6 +68,7 @@ spring-boot-vue spring-boot-xml spring-boot-actuator + spring-boot-jackson diff --git a/spring-boot-modules/spring-boot-jackson/pom.xml b/spring-boot-modules/spring-boot-jackson/pom.xml new file mode 100644 index 0000000000..b502bca908 --- /dev/null +++ b/spring-boot-modules/spring-boot-jackson/pom.xml @@ -0,0 +1,21 @@ + + + + parent-boot-2 + com.baeldung + 0.0.1-SNAPSHOT + ../../parent-boot-2 + + 4.0.0 + + spring-boot-jackson + + + org.springframework.boot + spring-boot-starter-web + + + + \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/Application.java b/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/Application.java new file mode 100644 index 0000000000..c4de34879f --- /dev/null +++ b/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/Application.java @@ -0,0 +1,12 @@ +package com.baeldung.boot.jackson; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Application { + + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } +} diff --git a/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/config/CoffeeConfiguration.java b/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/config/CoffeeConfiguration.java new file mode 100644 index 0000000000..ac7be062c2 --- /dev/null +++ b/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/config/CoffeeConfiguration.java @@ -0,0 +1,43 @@ +package com.baeldung.boot.jackson.config; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; +import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer; +import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Primary; +import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; + +import java.time.format.DateTimeFormatter; + +@Configuration +public class CoffeeConfiguration { + public static final String dateTimeFormat = "dd-MM-yyyy HH:mm"; + private LocalDateTimeSerializer localDateTimeSerializer = new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(dateTimeFormat)); + + @Bean + public Jackson2ObjectMapperBuilderCustomizer jsonCustomizer() { + return builder -> builder.serializationInclusion(JsonInclude.Include.NON_NULL) + .serializers(localDateTimeSerializer); + } + + @Bean + @Primary + public ObjectMapper objectMapper() { + JavaTimeModule module = new JavaTimeModule(); + module.addSerializer(localDateTimeSerializer); + return new ObjectMapper() + .setSerializationInclusion(JsonInclude.Include.NON_NULL) + .registerModule(module); + } + + @Bean + @Primary + public Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder() { + return new Jackson2ObjectMapperBuilder() + .serializers(localDateTimeSerializer) + .serializationInclusion(JsonInclude.Include.NON_NULL); + } +} diff --git a/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/controller/CoffeeController.java b/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/controller/CoffeeController.java new file mode 100644 index 0000000000..2a0f6e1240 --- /dev/null +++ b/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/controller/CoffeeController.java @@ -0,0 +1,25 @@ +package com.baeldung.boot.jackson.controller; + +import com.baeldung.boot.jackson.model.Coffee; +import com.baeldung.boot.jackson.model.CoffeeResponse; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import java.time.LocalDateTime; + +@RestController +public class CoffeeController { + + @GetMapping("/coffee") + public CoffeeResponse createCoffee(@RequestParam(required = false) String brand, + @RequestParam(required = false) String name) { + Coffee coffee = new Coffee() + .setBrand(brand) + .setName(name); + + return new CoffeeResponse() + .setDate(LocalDateTime.now()) + .setBody(coffee); + } +} diff --git a/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/model/Coffee.java b/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/model/Coffee.java new file mode 100644 index 0000000000..12c2e200df --- /dev/null +++ b/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/model/Coffee.java @@ -0,0 +1,26 @@ +package com.baeldung.boot.jackson.model; + +public class Coffee { + + private String name; + + private String brand; + + public String getName() { + return name; + } + + public Coffee setName(String name) { + this.name = name; + return this; + } + + public String getBrand() { + return brand; + } + + public Coffee setBrand(String brand) { + this.brand = brand; + return this; + } +} diff --git a/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/model/CoffeeResponse.java b/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/model/CoffeeResponse.java new file mode 100644 index 0000000000..c2667df03a --- /dev/null +++ b/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/model/CoffeeResponse.java @@ -0,0 +1,28 @@ +package com.baeldung.boot.jackson.model; + +import java.time.LocalDateTime; + +public class CoffeeResponse { + + private LocalDateTime date; + + private T body; + + public LocalDateTime getDate() { + return date; + } + + public CoffeeResponse setDate(LocalDateTime date) { + this.date = date; + return this; + } + + public T getBody() { + return body; + } + + public CoffeeResponse setBody(T body) { + this.body = body; + return this; + } +} diff --git a/spring-boot-modules/spring-boot-jackson/src/main/resources/application.properties b/spring-boot-modules/spring-boot-jackson/src/main/resources/application.properties new file mode 100644 index 0000000000..2129762424 --- /dev/null +++ b/spring-boot-modules/spring-boot-jackson/src/main/resources/application.properties @@ -0,0 +1,3 @@ +spring.jackson.default-property-inclusion=non_null +spring.jackson.serialization.write-dates-as-timestamps=false +spring.jackson.date-format=dd-MM-yyyy HH:mm \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-jackson/src/test/java/com/baeldung/boot/jackson/CoffeeIntegrationTest.java b/spring-boot-modules/spring-boot-jackson/src/test/java/com/baeldung/boot/jackson/CoffeeIntegrationTest.java new file mode 100644 index 0000000000..d508ee45d1 --- /dev/null +++ b/spring-boot-modules/spring-boot-jackson/src/test/java/com/baeldung/boot/jackson/CoffeeIntegrationTest.java @@ -0,0 +1,32 @@ +package com.baeldung.boot.jackson; + +import com.baeldung.boot.jackson.config.CoffeeConfiguration; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.web.client.TestRestTemplate; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; + +import static org.assertj.core.api.Assertions.assertThat; + +@SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +public class CoffeeIntegrationTest { + + @Autowired + TestRestTemplate restTemplate; + + @Test + public void whenQueryCoffeeWithoutParam_thenNullIsNotInserted() { + String formattedDate = DateTimeFormatter.ofPattern(CoffeeConfiguration.dateTimeFormat) + .format(LocalDateTime.now()); + String brand = "Lavazza"; + + String url = "/coffee?brand=" + brand; + String response = restTemplate.getForObject(url, String.class); + + assertThat(response).isEqualTo( + "{\"date\":\"" + formattedDate + "\",\"body\":{\"brand\":\"" + brand + "\"}}"); + } +} From 2a8ff6f23b38b356d5ab70d8dcad9cf5110c20e6 Mon Sep 17 00:00:00 2001 From: Adina Rolea Date: Tue, 27 Oct 2020 16:12:59 +0200 Subject: [PATCH 02/10] BAEL-4687: added web configuration for jackson --- .../jackson/config/CoffeeConfiguration.java | 8 +++++ .../config/CoffeeWebConfiguration.java | 32 +++++++++++++++++++ .../src/main/resources/application.properties | 3 +- 3 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/config/CoffeeWebConfiguration.java diff --git a/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/config/CoffeeConfiguration.java b/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/config/CoffeeConfiguration.java index ac7be062c2..a6804a12f0 100644 --- a/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/config/CoffeeConfiguration.java +++ b/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/config/CoffeeConfiguration.java @@ -1,6 +1,7 @@ package com.baeldung.boot.jackson.config; import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.databind.Module; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer; @@ -40,4 +41,11 @@ public class CoffeeConfiguration { .serializers(localDateTimeSerializer) .serializationInclusion(JsonInclude.Include.NON_NULL); } + + @Bean + public Module javaTimeModule() { + JavaTimeModule module = new JavaTimeModule(); + module.addSerializer(localDateTimeSerializer); + return module; + } } diff --git a/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/config/CoffeeWebConfiguration.java b/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/config/CoffeeWebConfiguration.java new file mode 100644 index 0000000000..a53ab3a805 --- /dev/null +++ b/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/config/CoffeeWebConfiguration.java @@ -0,0 +1,32 @@ +package com.baeldung.boot.jackson.config; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.http.converter.HttpMessageConverter; +import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; +import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +import java.time.format.DateTimeFormatter; +import java.util.List; + +@Configuration +public class CoffeeWebConfiguration implements WebMvcConfigurer { + public static final String dateTimeFormat = "dd-MM-yyyy HH:mm"; + private LocalDateTimeSerializer localDateTimeSerializer = new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(dateTimeFormat)); + + @Override + public void configureMessageConverters(List> converters) { + converters.add(mappingJackson2HttpMessageConverter()); + } + + @Bean + public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() { + Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder() + .serializers(localDateTimeSerializer) + .serializationInclusion(JsonInclude.Include.NON_NULL); + return new MappingJackson2HttpMessageConverter(builder.build()); + } +} diff --git a/spring-boot-modules/spring-boot-jackson/src/main/resources/application.properties b/spring-boot-modules/spring-boot-jackson/src/main/resources/application.properties index 2129762424..352add464b 100644 --- a/spring-boot-modules/spring-boot-jackson/src/main/resources/application.properties +++ b/spring-boot-modules/spring-boot-jackson/src/main/resources/application.properties @@ -1,3 +1,2 @@ spring.jackson.default-property-inclusion=non_null -spring.jackson.serialization.write-dates-as-timestamps=false -spring.jackson.date-format=dd-MM-yyyy HH:mm \ No newline at end of file +spring.jackson.serialization.write-dates-as-timestamps=false \ No newline at end of file From 0218395e89cdc42fcd1e522502a01d2278a50636 Mon Sep 17 00:00:00 2001 From: Adina Rolea Date: Tue, 27 Oct 2020 17:07:14 +0200 Subject: [PATCH 03/10] BAEL-4687: simplified configuration --- .../jackson/controller/CoffeeController.java | 12 +++----- .../baeldung/boot/jackson/model/Coffee.java | 13 +++++++++ .../boot/jackson/model/CoffeeResponse.java | 28 ------------------- .../boot/jackson/CoffeeIntegrationTest.java | 4 +-- 4 files changed, 19 insertions(+), 38 deletions(-) delete mode 100644 spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/model/CoffeeResponse.java diff --git a/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/controller/CoffeeController.java b/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/controller/CoffeeController.java index 2a0f6e1240..075126de67 100644 --- a/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/controller/CoffeeController.java +++ b/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/controller/CoffeeController.java @@ -1,7 +1,6 @@ package com.baeldung.boot.jackson.controller; import com.baeldung.boot.jackson.model.Coffee; -import com.baeldung.boot.jackson.model.CoffeeResponse; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @@ -12,14 +11,11 @@ import java.time.LocalDateTime; public class CoffeeController { @GetMapping("/coffee") - public CoffeeResponse createCoffee(@RequestParam(required = false) String brand, - @RequestParam(required = false) String name) { - Coffee coffee = new Coffee() + public Coffee getCoffee(@RequestParam(required = false) String brand, + @RequestParam(required = false) String name) { + return new Coffee() .setBrand(brand) - .setName(name); - - return new CoffeeResponse() .setDate(LocalDateTime.now()) - .setBody(coffee); + .setName(name); } } diff --git a/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/model/Coffee.java b/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/model/Coffee.java index 12c2e200df..4df6b4bd6d 100644 --- a/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/model/Coffee.java +++ b/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/model/Coffee.java @@ -1,11 +1,15 @@ package com.baeldung.boot.jackson.model; +import java.time.LocalDateTime; + public class Coffee { private String name; private String brand; + private LocalDateTime date; + public String getName() { return name; } @@ -23,4 +27,13 @@ public class Coffee { this.brand = brand; return this; } + + public LocalDateTime getDate() { + return date; + } + + public Coffee setDate(LocalDateTime date) { + this.date = date; + return this; + } } diff --git a/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/model/CoffeeResponse.java b/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/model/CoffeeResponse.java deleted file mode 100644 index c2667df03a..0000000000 --- a/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/model/CoffeeResponse.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.baeldung.boot.jackson.model; - -import java.time.LocalDateTime; - -public class CoffeeResponse { - - private LocalDateTime date; - - private T body; - - public LocalDateTime getDate() { - return date; - } - - public CoffeeResponse setDate(LocalDateTime date) { - this.date = date; - return this; - } - - public T getBody() { - return body; - } - - public CoffeeResponse setBody(T body) { - this.body = body; - return this; - } -} diff --git a/spring-boot-modules/spring-boot-jackson/src/test/java/com/baeldung/boot/jackson/CoffeeIntegrationTest.java b/spring-boot-modules/spring-boot-jackson/src/test/java/com/baeldung/boot/jackson/CoffeeIntegrationTest.java index d508ee45d1..1fda173d37 100644 --- a/spring-boot-modules/spring-boot-jackson/src/test/java/com/baeldung/boot/jackson/CoffeeIntegrationTest.java +++ b/spring-boot-modules/spring-boot-jackson/src/test/java/com/baeldung/boot/jackson/CoffeeIntegrationTest.java @@ -18,7 +18,7 @@ public class CoffeeIntegrationTest { TestRestTemplate restTemplate; @Test - public void whenQueryCoffeeWithoutParam_thenNullIsNotInserted() { + public void whenGetCoffee_thenSerializedWithDateAndNonNull() { String formattedDate = DateTimeFormatter.ofPattern(CoffeeConfiguration.dateTimeFormat) .format(LocalDateTime.now()); String brand = "Lavazza"; @@ -27,6 +27,6 @@ public class CoffeeIntegrationTest { String response = restTemplate.getForObject(url, String.class); assertThat(response).isEqualTo( - "{\"date\":\"" + formattedDate + "\",\"body\":{\"brand\":\"" + brand + "\"}}"); + "{\"brand\":\"" + brand + "\",\"date\":\"" + formattedDate + "\"}"); } } From 6e8b8c086de2d100d5212f354dd70cc42b540b18 Mon Sep 17 00:00:00 2001 From: Adina Rolea Date: Wed, 4 Nov 2020 10:36:43 +0200 Subject: [PATCH 04/10] BAEL-4687: updated jackson configuration --- .../jackson/config/CoffeeConfiguration.java | 9 ++++++ .../config/CoffeeWebConfiguration.java | 32 ------------------- 2 files changed, 9 insertions(+), 32 deletions(-) delete mode 100644 spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/config/CoffeeWebConfiguration.java diff --git a/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/config/CoffeeConfiguration.java b/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/config/CoffeeConfiguration.java index a6804a12f0..d13ce51e9b 100644 --- a/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/config/CoffeeConfiguration.java +++ b/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/config/CoffeeConfiguration.java @@ -10,6 +10,7 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; +import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import java.time.format.DateTimeFormatter; @@ -18,6 +19,14 @@ public class CoffeeConfiguration { public static final String dateTimeFormat = "dd-MM-yyyy HH:mm"; private LocalDateTimeSerializer localDateTimeSerializer = new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(dateTimeFormat)); + @Bean + public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() { + Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder() + .serializers(localDateTimeSerializer) + .serializationInclusion(JsonInclude.Include.NON_NULL); + return new MappingJackson2HttpMessageConverter(builder.build()); + } + @Bean public Jackson2ObjectMapperBuilderCustomizer jsonCustomizer() { return builder -> builder.serializationInclusion(JsonInclude.Include.NON_NULL) diff --git a/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/config/CoffeeWebConfiguration.java b/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/config/CoffeeWebConfiguration.java deleted file mode 100644 index a53ab3a805..0000000000 --- a/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/config/CoffeeWebConfiguration.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.baeldung.boot.jackson.config; - -import com.fasterxml.jackson.annotation.JsonInclude; -import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.http.converter.HttpMessageConverter; -import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; -import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; -import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; - -import java.time.format.DateTimeFormatter; -import java.util.List; - -@Configuration -public class CoffeeWebConfiguration implements WebMvcConfigurer { - public static final String dateTimeFormat = "dd-MM-yyyy HH:mm"; - private LocalDateTimeSerializer localDateTimeSerializer = new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(dateTimeFormat)); - - @Override - public void configureMessageConverters(List> converters) { - converters.add(mappingJackson2HttpMessageConverter()); - } - - @Bean - public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() { - Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder() - .serializers(localDateTimeSerializer) - .serializationInclusion(JsonInclude.Include.NON_NULL); - return new MappingJackson2HttpMessageConverter(builder.build()); - } -} From 644939da0755d266186dc15e0ee4934b9c2387a3 Mon Sep 17 00:00:00 2001 From: Adina Rolea Date: Mon, 16 Nov 2020 14:27:29 +0200 Subject: [PATCH 05/10] BAEL-4687: added test for each configuration --- .../boot/jackson/{ => app}/Application.java | 5 +- .../jackson/config/CoffeeConfiguration.java | 60 ------------------- .../boot/jackson/config/CoffeeConstants.java | 11 ++++ .../config/CoffeeCustomizerConfig.java | 18 ++++++ .../CoffeeHttpConverterConfiguration.java | 21 +++++++ .../config/CoffeeJacksonBuilderConfig.java | 21 +++++++ .../config/CoffeeObjectMapperConfig.java | 24 ++++++++ .../config/CoffeeRegisterModuleConfig.java | 21 +++++++ .../src/main/resources/application.properties | 2 - .../src/main/resources/coffee.properties | 1 + .../AbstractCoffeeIntegrationTest.java} | 12 ++-- .../app/CoffeeCustomizerIntegrationTest.java | 8 +++ .../CoffeeHttpConverterIntegrationTest.java | 8 +++ .../CoffeeJacksonBuilderIntegrationTest.java | 8 +++ .../CoffeeObjectMapperIntegrationTest.java | 8 +++ .../CoffeeRegisterModuleIntegrationTest.java | 8 +++ 16 files changed, 166 insertions(+), 70 deletions(-) rename spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/{ => app}/Application.java (62%) delete mode 100644 spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/config/CoffeeConfiguration.java create mode 100644 spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/config/CoffeeConstants.java create mode 100644 spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/config/CoffeeCustomizerConfig.java create mode 100644 spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/config/CoffeeHttpConverterConfiguration.java create mode 100644 spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/config/CoffeeJacksonBuilderConfig.java create mode 100644 spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/config/CoffeeObjectMapperConfig.java create mode 100644 spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/config/CoffeeRegisterModuleConfig.java delete mode 100644 spring-boot-modules/spring-boot-jackson/src/main/resources/application.properties create mode 100644 spring-boot-modules/spring-boot-jackson/src/main/resources/coffee.properties rename spring-boot-modules/spring-boot-jackson/src/test/java/com/baeldung/boot/jackson/{CoffeeIntegrationTest.java => app/AbstractCoffeeIntegrationTest.java} (81%) create mode 100644 spring-boot-modules/spring-boot-jackson/src/test/java/com/baeldung/boot/jackson/app/CoffeeCustomizerIntegrationTest.java create mode 100644 spring-boot-modules/spring-boot-jackson/src/test/java/com/baeldung/boot/jackson/app/CoffeeHttpConverterIntegrationTest.java create mode 100644 spring-boot-modules/spring-boot-jackson/src/test/java/com/baeldung/boot/jackson/app/CoffeeJacksonBuilderIntegrationTest.java create mode 100644 spring-boot-modules/spring-boot-jackson/src/test/java/com/baeldung/boot/jackson/app/CoffeeObjectMapperIntegrationTest.java create mode 100644 spring-boot-modules/spring-boot-jackson/src/test/java/com/baeldung/boot/jackson/app/CoffeeRegisterModuleIntegrationTest.java diff --git a/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/Application.java b/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/app/Application.java similarity index 62% rename from spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/Application.java rename to spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/app/Application.java index c4de34879f..6f57a534a8 100644 --- a/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/Application.java +++ b/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/app/Application.java @@ -1,11 +1,12 @@ -package com.baeldung.boot.jackson; +package com.baeldung.boot.jackson.app; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.ComponentScan; @SpringBootApplication +@ComponentScan(basePackages = "com.baeldung.boot.jackson.controller") public class Application { - public static void main(String[] args) { SpringApplication.run(Application.class, args); } diff --git a/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/config/CoffeeConfiguration.java b/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/config/CoffeeConfiguration.java deleted file mode 100644 index d13ce51e9b..0000000000 --- a/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/config/CoffeeConfiguration.java +++ /dev/null @@ -1,60 +0,0 @@ -package com.baeldung.boot.jackson.config; - -import com.fasterxml.jackson.annotation.JsonInclude; -import com.fasterxml.jackson.databind.Module; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; -import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer; -import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Primary; -import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; -import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; - -import java.time.format.DateTimeFormatter; - -@Configuration -public class CoffeeConfiguration { - public static final String dateTimeFormat = "dd-MM-yyyy HH:mm"; - private LocalDateTimeSerializer localDateTimeSerializer = new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(dateTimeFormat)); - - @Bean - public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() { - Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder() - .serializers(localDateTimeSerializer) - .serializationInclusion(JsonInclude.Include.NON_NULL); - return new MappingJackson2HttpMessageConverter(builder.build()); - } - - @Bean - public Jackson2ObjectMapperBuilderCustomizer jsonCustomizer() { - return builder -> builder.serializationInclusion(JsonInclude.Include.NON_NULL) - .serializers(localDateTimeSerializer); - } - - @Bean - @Primary - public ObjectMapper objectMapper() { - JavaTimeModule module = new JavaTimeModule(); - module.addSerializer(localDateTimeSerializer); - return new ObjectMapper() - .setSerializationInclusion(JsonInclude.Include.NON_NULL) - .registerModule(module); - } - - @Bean - @Primary - public Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder() { - return new Jackson2ObjectMapperBuilder() - .serializers(localDateTimeSerializer) - .serializationInclusion(JsonInclude.Include.NON_NULL); - } - - @Bean - public Module javaTimeModule() { - JavaTimeModule module = new JavaTimeModule(); - module.addSerializer(localDateTimeSerializer); - return module; - } -} diff --git a/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/config/CoffeeConstants.java b/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/config/CoffeeConstants.java new file mode 100644 index 0000000000..7e7d7b8bc2 --- /dev/null +++ b/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/config/CoffeeConstants.java @@ -0,0 +1,11 @@ +package com.baeldung.boot.jackson.config; + +import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer; + +import java.time.format.DateTimeFormatter; + +public class CoffeeConstants { + + public static final String dateTimeFormat = "dd-MM-yyyy HH:mm"; + public static LocalDateTimeSerializer localDateTimeSerializer = new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(dateTimeFormat)); +} diff --git a/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/config/CoffeeCustomizerConfig.java b/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/config/CoffeeCustomizerConfig.java new file mode 100644 index 0000000000..c13615e702 --- /dev/null +++ b/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/config/CoffeeCustomizerConfig.java @@ -0,0 +1,18 @@ +package com.baeldung.boot.jackson.config; + +import com.fasterxml.jackson.annotation.JsonInclude; +import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import static com.baeldung.boot.jackson.config.CoffeeConstants.localDateTimeSerializer; + +@Configuration +public class CoffeeCustomizerConfig { + + @Bean + public Jackson2ObjectMapperBuilderCustomizer jsonCustomizer() { + return builder -> builder.serializationInclusion(JsonInclude.Include.NON_NULL) + .serializers(localDateTimeSerializer); + } +} diff --git a/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/config/CoffeeHttpConverterConfiguration.java b/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/config/CoffeeHttpConverterConfiguration.java new file mode 100644 index 0000000000..83474a5c1f --- /dev/null +++ b/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/config/CoffeeHttpConverterConfiguration.java @@ -0,0 +1,21 @@ +package com.baeldung.boot.jackson.config; + +import com.fasterxml.jackson.annotation.JsonInclude; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; +import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; + +import static com.baeldung.boot.jackson.config.CoffeeConstants.localDateTimeSerializer; + +@Configuration +public class CoffeeHttpConverterConfiguration { + + @Bean + public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() { + Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder() + .serializers(localDateTimeSerializer) + .serializationInclusion(JsonInclude.Include.NON_NULL); + return new MappingJackson2HttpMessageConverter(builder.build()); + } +} diff --git a/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/config/CoffeeJacksonBuilderConfig.java b/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/config/CoffeeJacksonBuilderConfig.java new file mode 100644 index 0000000000..7a7b3e48bf --- /dev/null +++ b/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/config/CoffeeJacksonBuilderConfig.java @@ -0,0 +1,21 @@ +package com.baeldung.boot.jackson.config; + +import com.fasterxml.jackson.annotation.JsonInclude; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Primary; +import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; + +import static com.baeldung.boot.jackson.config.CoffeeConstants.localDateTimeSerializer; + +@Configuration +public class CoffeeJacksonBuilderConfig { + + @Bean + @Primary + public Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder() { + return new Jackson2ObjectMapperBuilder() + .serializers(localDateTimeSerializer) + .serializationInclusion(JsonInclude.Include.NON_NULL); + } +} \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/config/CoffeeObjectMapperConfig.java b/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/config/CoffeeObjectMapperConfig.java new file mode 100644 index 0000000000..5697928cc5 --- /dev/null +++ b/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/config/CoffeeObjectMapperConfig.java @@ -0,0 +1,24 @@ +package com.baeldung.boot.jackson.config; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Primary; + +import static com.baeldung.boot.jackson.config.CoffeeConstants.localDateTimeSerializer; + +@Configuration +public class CoffeeObjectMapperConfig { + + @Bean + @Primary + public ObjectMapper objectMapper() { + JavaTimeModule module = new JavaTimeModule(); + module.addSerializer(localDateTimeSerializer); + return new ObjectMapper() + .setSerializationInclusion(JsonInclude.Include.NON_NULL) + .registerModule(module); + } +} diff --git a/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/config/CoffeeRegisterModuleConfig.java b/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/config/CoffeeRegisterModuleConfig.java new file mode 100644 index 0000000000..855bc84966 --- /dev/null +++ b/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/config/CoffeeRegisterModuleConfig.java @@ -0,0 +1,21 @@ +package com.baeldung.boot.jackson.config; + +import com.fasterxml.jackson.databind.Module; +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.PropertySource; + +import static com.baeldung.boot.jackson.config.CoffeeConstants.localDateTimeSerializer; + +@Configuration +@PropertySource("classpath:coffee.properties") +public class CoffeeRegisterModuleConfig { + + @Bean + public Module javaTimeModule() { + JavaTimeModule module = new JavaTimeModule(); + module.addSerializer(localDateTimeSerializer); + return module; + } +} diff --git a/spring-boot-modules/spring-boot-jackson/src/main/resources/application.properties b/spring-boot-modules/spring-boot-jackson/src/main/resources/application.properties deleted file mode 100644 index 352add464b..0000000000 --- a/spring-boot-modules/spring-boot-jackson/src/main/resources/application.properties +++ /dev/null @@ -1,2 +0,0 @@ -spring.jackson.default-property-inclusion=non_null -spring.jackson.serialization.write-dates-as-timestamps=false \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-jackson/src/main/resources/coffee.properties b/spring-boot-modules/spring-boot-jackson/src/main/resources/coffee.properties new file mode 100644 index 0000000000..269845cbf1 --- /dev/null +++ b/spring-boot-modules/spring-boot-jackson/src/main/resources/coffee.properties @@ -0,0 +1 @@ +spring.jackson.default-property-inclusion=non_null \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-jackson/src/test/java/com/baeldung/boot/jackson/CoffeeIntegrationTest.java b/spring-boot-modules/spring-boot-jackson/src/test/java/com/baeldung/boot/jackson/app/AbstractCoffeeIntegrationTest.java similarity index 81% rename from spring-boot-modules/spring-boot-jackson/src/test/java/com/baeldung/boot/jackson/CoffeeIntegrationTest.java rename to spring-boot-modules/spring-boot-jackson/src/test/java/com/baeldung/boot/jackson/app/AbstractCoffeeIntegrationTest.java index 1fda173d37..13e1f05f97 100644 --- a/spring-boot-modules/spring-boot-jackson/src/test/java/com/baeldung/boot/jackson/CoffeeIntegrationTest.java +++ b/spring-boot-modules/spring-boot-jackson/src/test/java/com/baeldung/boot/jackson/app/AbstractCoffeeIntegrationTest.java @@ -1,6 +1,6 @@ -package com.baeldung.boot.jackson; +package com.baeldung.boot.jackson.app; -import com.baeldung.boot.jackson.config.CoffeeConfiguration; +import com.baeldung.boot.jackson.config.CoffeeConstants; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @@ -12,20 +12,20 @@ import java.time.format.DateTimeFormatter; import static org.assertj.core.api.Assertions.assertThat; @SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) -public class CoffeeIntegrationTest { +public abstract class AbstractCoffeeIntegrationTest { @Autowired - TestRestTemplate restTemplate; + protected TestRestTemplate restTemplate; @Test public void whenGetCoffee_thenSerializedWithDateAndNonNull() { - String formattedDate = DateTimeFormatter.ofPattern(CoffeeConfiguration.dateTimeFormat) + String formattedDate = DateTimeFormatter.ofPattern(CoffeeConstants.dateTimeFormat) .format(LocalDateTime.now()); + String brand = "Lavazza"; String url = "/coffee?brand=" + brand; String response = restTemplate.getForObject(url, String.class); - assertThat(response).isEqualTo( "{\"brand\":\"" + brand + "\",\"date\":\"" + formattedDate + "\"}"); } diff --git a/spring-boot-modules/spring-boot-jackson/src/test/java/com/baeldung/boot/jackson/app/CoffeeCustomizerIntegrationTest.java b/spring-boot-modules/spring-boot-jackson/src/test/java/com/baeldung/boot/jackson/app/CoffeeCustomizerIntegrationTest.java new file mode 100644 index 0000000000..d690de1b9c --- /dev/null +++ b/spring-boot-modules/spring-boot-jackson/src/test/java/com/baeldung/boot/jackson/app/CoffeeCustomizerIntegrationTest.java @@ -0,0 +1,8 @@ +package com.baeldung.boot.jackson.app; + +import com.baeldung.boot.jackson.config.CoffeeCustomizerConfig; +import org.springframework.context.annotation.Import; + +@Import(CoffeeCustomizerConfig.class) +public class CoffeeCustomizerIntegrationTest extends AbstractCoffeeIntegrationTest { +} diff --git a/spring-boot-modules/spring-boot-jackson/src/test/java/com/baeldung/boot/jackson/app/CoffeeHttpConverterIntegrationTest.java b/spring-boot-modules/spring-boot-jackson/src/test/java/com/baeldung/boot/jackson/app/CoffeeHttpConverterIntegrationTest.java new file mode 100644 index 0000000000..62b1d42152 --- /dev/null +++ b/spring-boot-modules/spring-boot-jackson/src/test/java/com/baeldung/boot/jackson/app/CoffeeHttpConverterIntegrationTest.java @@ -0,0 +1,8 @@ +package com.baeldung.boot.jackson.app; + +import com.baeldung.boot.jackson.config.CoffeeHttpConverterConfiguration; +import org.springframework.context.annotation.Import; + +@Import(CoffeeHttpConverterConfiguration.class) +public class CoffeeHttpConverterIntegrationTest extends AbstractCoffeeIntegrationTest { +} diff --git a/spring-boot-modules/spring-boot-jackson/src/test/java/com/baeldung/boot/jackson/app/CoffeeJacksonBuilderIntegrationTest.java b/spring-boot-modules/spring-boot-jackson/src/test/java/com/baeldung/boot/jackson/app/CoffeeJacksonBuilderIntegrationTest.java new file mode 100644 index 0000000000..52a55394c0 --- /dev/null +++ b/spring-boot-modules/spring-boot-jackson/src/test/java/com/baeldung/boot/jackson/app/CoffeeJacksonBuilderIntegrationTest.java @@ -0,0 +1,8 @@ +package com.baeldung.boot.jackson.app; + +import com.baeldung.boot.jackson.config.CoffeeJacksonBuilderConfig; +import org.springframework.context.annotation.Import; + +@Import(CoffeeJacksonBuilderConfig.class) +public class CoffeeJacksonBuilderIntegrationTest extends AbstractCoffeeIntegrationTest { +} diff --git a/spring-boot-modules/spring-boot-jackson/src/test/java/com/baeldung/boot/jackson/app/CoffeeObjectMapperIntegrationTest.java b/spring-boot-modules/spring-boot-jackson/src/test/java/com/baeldung/boot/jackson/app/CoffeeObjectMapperIntegrationTest.java new file mode 100644 index 0000000000..34743ceba5 --- /dev/null +++ b/spring-boot-modules/spring-boot-jackson/src/test/java/com/baeldung/boot/jackson/app/CoffeeObjectMapperIntegrationTest.java @@ -0,0 +1,8 @@ +package com.baeldung.boot.jackson.app; + +import com.baeldung.boot.jackson.config.CoffeeObjectMapperConfig; +import org.springframework.context.annotation.Import; + +@Import(CoffeeObjectMapperConfig.class) +public class CoffeeObjectMapperIntegrationTest extends AbstractCoffeeIntegrationTest { +} diff --git a/spring-boot-modules/spring-boot-jackson/src/test/java/com/baeldung/boot/jackson/app/CoffeeRegisterModuleIntegrationTest.java b/spring-boot-modules/spring-boot-jackson/src/test/java/com/baeldung/boot/jackson/app/CoffeeRegisterModuleIntegrationTest.java new file mode 100644 index 0000000000..69bbd5be2a --- /dev/null +++ b/spring-boot-modules/spring-boot-jackson/src/test/java/com/baeldung/boot/jackson/app/CoffeeRegisterModuleIntegrationTest.java @@ -0,0 +1,8 @@ +package com.baeldung.boot.jackson.app; + +import com.baeldung.boot.jackson.config.CoffeeRegisterModuleConfig; +import org.springframework.context.annotation.Import; + +@Import(CoffeeRegisterModuleConfig.class) +public class CoffeeRegisterModuleIntegrationTest extends AbstractCoffeeIntegrationTest { +} From 003a5f203419f19c3a4a0f023ada288ea982d2e6 Mon Sep 17 00:00:00 2001 From: Adina Rolea Date: Mon, 16 Nov 2020 14:29:46 +0200 Subject: [PATCH 06/10] BAEL-4687: updated parent --- spring-boot-modules/spring-boot-jackson/pom.xml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/spring-boot-modules/spring-boot-jackson/pom.xml b/spring-boot-modules/spring-boot-jackson/pom.xml index b502bca908..1b2e55839a 100644 --- a/spring-boot-modules/spring-boot-jackson/pom.xml +++ b/spring-boot-modules/spring-boot-jackson/pom.xml @@ -3,10 +3,10 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> - parent-boot-2 - com.baeldung - 0.0.1-SNAPSHOT - ../../parent-boot-2 + com.baeldung.spring-boot-modules + spring-boot-modules + 1.0.0-SNAPSHOT + ../ 4.0.0 From c19b868d3b92fd2911ea9b5a89e21677550294b4 Mon Sep 17 00:00:00 2001 From: Adina Rolea Date: Fri, 4 Dec 2020 19:55:44 +0200 Subject: [PATCH 07/10] BAEL-4687: renamed project from jackson to data 2 --- spring-boot-modules/pom.xml | 2 +- .../{spring-boot-jackson => spring-boot-data-2}/pom.xml | 6 +++--- .../java/com/baeldung/boot/jackson/app/Application.java | 0 .../com/baeldung/boot/jackson/config/CoffeeConstants.java | 0 .../boot/jackson/config/CoffeeCustomizerConfig.java | 0 .../jackson/config/CoffeeHttpConverterConfiguration.java | 0 .../boot/jackson/config/CoffeeJacksonBuilderConfig.java | 0 .../boot/jackson/config/CoffeeObjectMapperConfig.java | 0 .../boot/jackson/config/CoffeeRegisterModuleConfig.java | 0 .../baeldung/boot/jackson/controller/CoffeeController.java | 0 .../main/java/com/baeldung/boot/jackson/model/Coffee.java | 0 .../src/main/resources/coffee.properties | 0 .../boot/jackson/app/AbstractCoffeeIntegrationTest.java | 0 .../boot/jackson/app/CoffeeCustomizerIntegrationTest.java | 0 .../jackson/app/CoffeeHttpConverterIntegrationTest.java | 0 .../jackson/app/CoffeeJacksonBuilderIntegrationTest.java | 0 .../boot/jackson/app/CoffeeObjectMapperIntegrationTest.java | 0 .../jackson/app/CoffeeRegisterModuleIntegrationTest.java | 0 18 files changed, 4 insertions(+), 4 deletions(-) rename spring-boot-modules/{spring-boot-jackson => spring-boot-data-2}/pom.xml (79%) rename spring-boot-modules/{spring-boot-jackson => spring-boot-data-2}/src/main/java/com/baeldung/boot/jackson/app/Application.java (100%) rename spring-boot-modules/{spring-boot-jackson => spring-boot-data-2}/src/main/java/com/baeldung/boot/jackson/config/CoffeeConstants.java (100%) rename spring-boot-modules/{spring-boot-jackson => spring-boot-data-2}/src/main/java/com/baeldung/boot/jackson/config/CoffeeCustomizerConfig.java (100%) rename spring-boot-modules/{spring-boot-jackson => spring-boot-data-2}/src/main/java/com/baeldung/boot/jackson/config/CoffeeHttpConverterConfiguration.java (100%) rename spring-boot-modules/{spring-boot-jackson => spring-boot-data-2}/src/main/java/com/baeldung/boot/jackson/config/CoffeeJacksonBuilderConfig.java (100%) rename spring-boot-modules/{spring-boot-jackson => spring-boot-data-2}/src/main/java/com/baeldung/boot/jackson/config/CoffeeObjectMapperConfig.java (100%) rename spring-boot-modules/{spring-boot-jackson => spring-boot-data-2}/src/main/java/com/baeldung/boot/jackson/config/CoffeeRegisterModuleConfig.java (100%) rename spring-boot-modules/{spring-boot-jackson => spring-boot-data-2}/src/main/java/com/baeldung/boot/jackson/controller/CoffeeController.java (100%) rename spring-boot-modules/{spring-boot-jackson => spring-boot-data-2}/src/main/java/com/baeldung/boot/jackson/model/Coffee.java (100%) rename spring-boot-modules/{spring-boot-jackson => spring-boot-data-2}/src/main/resources/coffee.properties (100%) rename spring-boot-modules/{spring-boot-jackson => spring-boot-data-2}/src/test/java/com/baeldung/boot/jackson/app/AbstractCoffeeIntegrationTest.java (100%) rename spring-boot-modules/{spring-boot-jackson => spring-boot-data-2}/src/test/java/com/baeldung/boot/jackson/app/CoffeeCustomizerIntegrationTest.java (100%) rename spring-boot-modules/{spring-boot-jackson => spring-boot-data-2}/src/test/java/com/baeldung/boot/jackson/app/CoffeeHttpConverterIntegrationTest.java (100%) rename spring-boot-modules/{spring-boot-jackson => spring-boot-data-2}/src/test/java/com/baeldung/boot/jackson/app/CoffeeJacksonBuilderIntegrationTest.java (100%) rename spring-boot-modules/{spring-boot-jackson => spring-boot-data-2}/src/test/java/com/baeldung/boot/jackson/app/CoffeeObjectMapperIntegrationTest.java (100%) rename spring-boot-modules/{spring-boot-jackson => spring-boot-data-2}/src/test/java/com/baeldung/boot/jackson/app/CoffeeRegisterModuleIntegrationTest.java (100%) diff --git a/spring-boot-modules/pom.xml b/spring-boot-modules/pom.xml index 3d721d7147..4ff825309a 100644 --- a/spring-boot-modules/pom.xml +++ b/spring-boot-modules/pom.xml @@ -68,7 +68,7 @@ spring-boot-vue spring-boot-xml spring-boot-actuator - spring-boot-jackson + spring-boot-data-2 diff --git a/spring-boot-modules/spring-boot-jackson/pom.xml b/spring-boot-modules/spring-boot-data-2/pom.xml similarity index 79% rename from spring-boot-modules/spring-boot-jackson/pom.xml rename to spring-boot-modules/spring-boot-data-2/pom.xml index 1b2e55839a..199a204500 100644 --- a/spring-boot-modules/spring-boot-jackson/pom.xml +++ b/spring-boot-modules/spring-boot-data-2/pom.xml @@ -1,6 +1,6 @@ - com.baeldung.spring-boot-modules @@ -10,7 +10,7 @@ 4.0.0 - spring-boot-jackson + spring-boot-data-2 org.springframework.boot diff --git a/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/app/Application.java b/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/jackson/app/Application.java similarity index 100% rename from spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/app/Application.java rename to spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/jackson/app/Application.java diff --git a/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/config/CoffeeConstants.java b/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/jackson/config/CoffeeConstants.java similarity index 100% rename from spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/config/CoffeeConstants.java rename to spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/jackson/config/CoffeeConstants.java diff --git a/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/config/CoffeeCustomizerConfig.java b/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/jackson/config/CoffeeCustomizerConfig.java similarity index 100% rename from spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/config/CoffeeCustomizerConfig.java rename to spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/jackson/config/CoffeeCustomizerConfig.java diff --git a/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/config/CoffeeHttpConverterConfiguration.java b/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/jackson/config/CoffeeHttpConverterConfiguration.java similarity index 100% rename from spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/config/CoffeeHttpConverterConfiguration.java rename to spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/jackson/config/CoffeeHttpConverterConfiguration.java diff --git a/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/config/CoffeeJacksonBuilderConfig.java b/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/jackson/config/CoffeeJacksonBuilderConfig.java similarity index 100% rename from spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/config/CoffeeJacksonBuilderConfig.java rename to spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/jackson/config/CoffeeJacksonBuilderConfig.java diff --git a/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/config/CoffeeObjectMapperConfig.java b/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/jackson/config/CoffeeObjectMapperConfig.java similarity index 100% rename from spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/config/CoffeeObjectMapperConfig.java rename to spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/jackson/config/CoffeeObjectMapperConfig.java diff --git a/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/config/CoffeeRegisterModuleConfig.java b/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/jackson/config/CoffeeRegisterModuleConfig.java similarity index 100% rename from spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/config/CoffeeRegisterModuleConfig.java rename to spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/jackson/config/CoffeeRegisterModuleConfig.java diff --git a/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/controller/CoffeeController.java b/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/jackson/controller/CoffeeController.java similarity index 100% rename from spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/controller/CoffeeController.java rename to spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/jackson/controller/CoffeeController.java diff --git a/spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/model/Coffee.java b/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/jackson/model/Coffee.java similarity index 100% rename from spring-boot-modules/spring-boot-jackson/src/main/java/com/baeldung/boot/jackson/model/Coffee.java rename to spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/jackson/model/Coffee.java diff --git a/spring-boot-modules/spring-boot-jackson/src/main/resources/coffee.properties b/spring-boot-modules/spring-boot-data-2/src/main/resources/coffee.properties similarity index 100% rename from spring-boot-modules/spring-boot-jackson/src/main/resources/coffee.properties rename to spring-boot-modules/spring-boot-data-2/src/main/resources/coffee.properties diff --git a/spring-boot-modules/spring-boot-jackson/src/test/java/com/baeldung/boot/jackson/app/AbstractCoffeeIntegrationTest.java b/spring-boot-modules/spring-boot-data-2/src/test/java/com/baeldung/boot/jackson/app/AbstractCoffeeIntegrationTest.java similarity index 100% rename from spring-boot-modules/spring-boot-jackson/src/test/java/com/baeldung/boot/jackson/app/AbstractCoffeeIntegrationTest.java rename to spring-boot-modules/spring-boot-data-2/src/test/java/com/baeldung/boot/jackson/app/AbstractCoffeeIntegrationTest.java diff --git a/spring-boot-modules/spring-boot-jackson/src/test/java/com/baeldung/boot/jackson/app/CoffeeCustomizerIntegrationTest.java b/spring-boot-modules/spring-boot-data-2/src/test/java/com/baeldung/boot/jackson/app/CoffeeCustomizerIntegrationTest.java similarity index 100% rename from spring-boot-modules/spring-boot-jackson/src/test/java/com/baeldung/boot/jackson/app/CoffeeCustomizerIntegrationTest.java rename to spring-boot-modules/spring-boot-data-2/src/test/java/com/baeldung/boot/jackson/app/CoffeeCustomizerIntegrationTest.java diff --git a/spring-boot-modules/spring-boot-jackson/src/test/java/com/baeldung/boot/jackson/app/CoffeeHttpConverterIntegrationTest.java b/spring-boot-modules/spring-boot-data-2/src/test/java/com/baeldung/boot/jackson/app/CoffeeHttpConverterIntegrationTest.java similarity index 100% rename from spring-boot-modules/spring-boot-jackson/src/test/java/com/baeldung/boot/jackson/app/CoffeeHttpConverterIntegrationTest.java rename to spring-boot-modules/spring-boot-data-2/src/test/java/com/baeldung/boot/jackson/app/CoffeeHttpConverterIntegrationTest.java diff --git a/spring-boot-modules/spring-boot-jackson/src/test/java/com/baeldung/boot/jackson/app/CoffeeJacksonBuilderIntegrationTest.java b/spring-boot-modules/spring-boot-data-2/src/test/java/com/baeldung/boot/jackson/app/CoffeeJacksonBuilderIntegrationTest.java similarity index 100% rename from spring-boot-modules/spring-boot-jackson/src/test/java/com/baeldung/boot/jackson/app/CoffeeJacksonBuilderIntegrationTest.java rename to spring-boot-modules/spring-boot-data-2/src/test/java/com/baeldung/boot/jackson/app/CoffeeJacksonBuilderIntegrationTest.java diff --git a/spring-boot-modules/spring-boot-jackson/src/test/java/com/baeldung/boot/jackson/app/CoffeeObjectMapperIntegrationTest.java b/spring-boot-modules/spring-boot-data-2/src/test/java/com/baeldung/boot/jackson/app/CoffeeObjectMapperIntegrationTest.java similarity index 100% rename from spring-boot-modules/spring-boot-jackson/src/test/java/com/baeldung/boot/jackson/app/CoffeeObjectMapperIntegrationTest.java rename to spring-boot-modules/spring-boot-data-2/src/test/java/com/baeldung/boot/jackson/app/CoffeeObjectMapperIntegrationTest.java diff --git a/spring-boot-modules/spring-boot-jackson/src/test/java/com/baeldung/boot/jackson/app/CoffeeRegisterModuleIntegrationTest.java b/spring-boot-modules/spring-boot-data-2/src/test/java/com/baeldung/boot/jackson/app/CoffeeRegisterModuleIntegrationTest.java similarity index 100% rename from spring-boot-modules/spring-boot-jackson/src/test/java/com/baeldung/boot/jackson/app/CoffeeRegisterModuleIntegrationTest.java rename to spring-boot-modules/spring-boot-data-2/src/test/java/com/baeldung/boot/jackson/app/CoffeeRegisterModuleIntegrationTest.java From 65e9e80f0c0f1ccefbfe858eed02e52ca0d62682 Mon Sep 17 00:00:00 2001 From: Adina Rolea Date: Fri, 4 Dec 2020 20:01:12 +0200 Subject: [PATCH 08/10] BAEL-4687: formatted code with eclipse formatter --- .../boot/jackson/config/CoffeeConstants.java | 4 ++-- .../jackson/config/CoffeeCustomizerConfig.java | 5 +++-- .../config/CoffeeHttpConverterConfiguration.java | 5 +++-- .../config/CoffeeJacksonBuilderConfig.java | 5 +++-- .../jackson/config/CoffeeObjectMapperConfig.java | 14 +++++++------- .../config/CoffeeRegisterModuleConfig.java | 7 ++++--- .../boot/jackson/controller/CoffeeController.java | 15 +++++++-------- 7 files changed, 29 insertions(+), 26 deletions(-) diff --git a/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/jackson/config/CoffeeConstants.java b/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/jackson/config/CoffeeConstants.java index 7e7d7b8bc2..90cad011ae 100644 --- a/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/jackson/config/CoffeeConstants.java +++ b/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/jackson/config/CoffeeConstants.java @@ -1,9 +1,9 @@ package com.baeldung.boot.jackson.config; -import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer; - import java.time.format.DateTimeFormatter; +import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer; + public class CoffeeConstants { public static final String dateTimeFormat = "dd-MM-yyyy HH:mm"; diff --git a/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/jackson/config/CoffeeCustomizerConfig.java b/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/jackson/config/CoffeeCustomizerConfig.java index c13615e702..363c67fd9a 100644 --- a/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/jackson/config/CoffeeCustomizerConfig.java +++ b/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/jackson/config/CoffeeCustomizerConfig.java @@ -1,11 +1,12 @@ package com.baeldung.boot.jackson.config; -import com.fasterxml.jackson.annotation.JsonInclude; +import static com.baeldung.boot.jackson.config.CoffeeConstants.localDateTimeSerializer; + import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import static com.baeldung.boot.jackson.config.CoffeeConstants.localDateTimeSerializer; +import com.fasterxml.jackson.annotation.JsonInclude; @Configuration public class CoffeeCustomizerConfig { diff --git a/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/jackson/config/CoffeeHttpConverterConfiguration.java b/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/jackson/config/CoffeeHttpConverterConfiguration.java index 83474a5c1f..b67f215816 100644 --- a/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/jackson/config/CoffeeHttpConverterConfiguration.java +++ b/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/jackson/config/CoffeeHttpConverterConfiguration.java @@ -1,12 +1,13 @@ package com.baeldung.boot.jackson.config; -import com.fasterxml.jackson.annotation.JsonInclude; +import static com.baeldung.boot.jackson.config.CoffeeConstants.localDateTimeSerializer; + import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; -import static com.baeldung.boot.jackson.config.CoffeeConstants.localDateTimeSerializer; +import com.fasterxml.jackson.annotation.JsonInclude; @Configuration public class CoffeeHttpConverterConfiguration { diff --git a/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/jackson/config/CoffeeJacksonBuilderConfig.java b/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/jackson/config/CoffeeJacksonBuilderConfig.java index 7a7b3e48bf..ec4de1d353 100644 --- a/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/jackson/config/CoffeeJacksonBuilderConfig.java +++ b/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/jackson/config/CoffeeJacksonBuilderConfig.java @@ -1,12 +1,13 @@ package com.baeldung.boot.jackson.config; -import com.fasterxml.jackson.annotation.JsonInclude; +import static com.baeldung.boot.jackson.config.CoffeeConstants.localDateTimeSerializer; + import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; -import static com.baeldung.boot.jackson.config.CoffeeConstants.localDateTimeSerializer; +import com.fasterxml.jackson.annotation.JsonInclude; @Configuration public class CoffeeJacksonBuilderConfig { diff --git a/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/jackson/config/CoffeeObjectMapperConfig.java b/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/jackson/config/CoffeeObjectMapperConfig.java index 5697928cc5..3754de018a 100644 --- a/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/jackson/config/CoffeeObjectMapperConfig.java +++ b/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/jackson/config/CoffeeObjectMapperConfig.java @@ -1,13 +1,14 @@ package com.baeldung.boot.jackson.config; -import com.fasterxml.jackson.annotation.JsonInclude; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; +import static com.baeldung.boot.jackson.config.CoffeeConstants.localDateTimeSerializer; + import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; -import static com.baeldung.boot.jackson.config.CoffeeConstants.localDateTimeSerializer; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; @Configuration public class CoffeeObjectMapperConfig { @@ -17,8 +18,7 @@ public class CoffeeObjectMapperConfig { public ObjectMapper objectMapper() { JavaTimeModule module = new JavaTimeModule(); module.addSerializer(localDateTimeSerializer); - return new ObjectMapper() - .setSerializationInclusion(JsonInclude.Include.NON_NULL) - .registerModule(module); + return new ObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_NULL) + .registerModule(module); } } diff --git a/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/jackson/config/CoffeeRegisterModuleConfig.java b/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/jackson/config/CoffeeRegisterModuleConfig.java index 855bc84966..ea00e5a58e 100644 --- a/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/jackson/config/CoffeeRegisterModuleConfig.java +++ b/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/jackson/config/CoffeeRegisterModuleConfig.java @@ -1,12 +1,13 @@ package com.baeldung.boot.jackson.config; -import com.fasterxml.jackson.databind.Module; -import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; +import static com.baeldung.boot.jackson.config.CoffeeConstants.localDateTimeSerializer; + import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; -import static com.baeldung.boot.jackson.config.CoffeeConstants.localDateTimeSerializer; +import com.fasterxml.jackson.databind.Module; +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; @Configuration @PropertySource("classpath:coffee.properties") diff --git a/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/jackson/controller/CoffeeController.java b/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/jackson/controller/CoffeeController.java index 075126de67..a960dafd89 100644 --- a/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/jackson/controller/CoffeeController.java +++ b/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/jackson/controller/CoffeeController.java @@ -1,21 +1,20 @@ package com.baeldung.boot.jackson.controller; -import com.baeldung.boot.jackson.model.Coffee; +import java.time.LocalDateTime; + import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; -import java.time.LocalDateTime; +import com.baeldung.boot.jackson.model.Coffee; @RestController public class CoffeeController { @GetMapping("/coffee") - public Coffee getCoffee(@RequestParam(required = false) String brand, - @RequestParam(required = false) String name) { - return new Coffee() - .setBrand(brand) - .setDate(LocalDateTime.now()) - .setName(name); + public Coffee getCoffee(@RequestParam(required = false) String brand, @RequestParam(required = false) String name) { + return new Coffee().setBrand(brand) + .setDate(LocalDateTime.now()) + .setName(name); } } From d4b22c74e3ab06291ba92585f136342c347b83e2 Mon Sep 17 00:00:00 2001 From: Adina Rolea Date: Thu, 17 Dec 2020 14:02:59 +0200 Subject: [PATCH 09/10] BAEL-4687: updated formatting --- .../boot/jackson/controller/CoffeeController.java | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/jackson/controller/CoffeeController.java b/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/jackson/controller/CoffeeController.java index a960dafd89..ad2f6ab942 100644 --- a/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/jackson/controller/CoffeeController.java +++ b/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/jackson/controller/CoffeeController.java @@ -1,20 +1,21 @@ package com.baeldung.boot.jackson.controller; -import java.time.LocalDateTime; - +import com.baeldung.boot.jackson.model.Coffee; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; -import com.baeldung.boot.jackson.model.Coffee; +import java.time.LocalDateTime; @RestController public class CoffeeController { @GetMapping("/coffee") - public Coffee getCoffee(@RequestParam(required = false) String brand, @RequestParam(required = false) String name) { + public Coffee getCoffee( + @RequestParam(required = false) String brand, + @RequestParam(required = false) String name) { return new Coffee().setBrand(brand) - .setDate(LocalDateTime.now()) - .setName(name); + .setDate(LocalDateTime.now()) + .setName(name); } } From d8f681c38e2d3dd8981a942e90e69778a3485840 Mon Sep 17 00:00:00 2001 From: Adina Rolea Date: Thu, 17 Dec 2020 14:23:06 +0200 Subject: [PATCH 10/10] BAEL-4687: returned fixed date instead of dynamic --- .../boot/jackson/config/CoffeeConstants.java | 10 ++++++---- .../boot/jackson/config/CoffeeCustomizerConfig.java | 7 +++---- .../config/CoffeeHttpConverterConfiguration.java | 7 +++---- .../jackson/config/CoffeeJacksonBuilderConfig.java | 7 +++---- .../jackson/config/CoffeeObjectMapperConfig.java | 13 ++++++------- .../jackson/config/CoffeeRegisterModuleConfig.java | 9 ++++----- .../boot/jackson/controller/CoffeeController.java | 4 ++-- .../jackson/app/AbstractCoffeeIntegrationTest.java | 9 +++++---- 8 files changed, 32 insertions(+), 34 deletions(-) diff --git a/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/jackson/config/CoffeeConstants.java b/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/jackson/config/CoffeeConstants.java index 90cad011ae..d1875d03d9 100644 --- a/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/jackson/config/CoffeeConstants.java +++ b/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/jackson/config/CoffeeConstants.java @@ -1,11 +1,13 @@ package com.baeldung.boot.jackson.config; -import java.time.format.DateTimeFormatter; - import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; + public class CoffeeConstants { - public static final String dateTimeFormat = "dd-MM-yyyy HH:mm"; - public static LocalDateTimeSerializer localDateTimeSerializer = new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(dateTimeFormat)); + public static final String DATETIME_FORMAT = "dd-MM-yyyy HH:mm"; + public static final LocalDateTime FIXED_DATE = LocalDateTime.now(); + public static LocalDateTimeSerializer LOCAL_DATETIME_SERIALIZER = new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DATETIME_FORMAT)); } diff --git a/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/jackson/config/CoffeeCustomizerConfig.java b/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/jackson/config/CoffeeCustomizerConfig.java index 363c67fd9a..edb2b478fc 100644 --- a/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/jackson/config/CoffeeCustomizerConfig.java +++ b/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/jackson/config/CoffeeCustomizerConfig.java @@ -1,12 +1,11 @@ package com.baeldung.boot.jackson.config; -import static com.baeldung.boot.jackson.config.CoffeeConstants.localDateTimeSerializer; - +import com.fasterxml.jackson.annotation.JsonInclude; import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import com.fasterxml.jackson.annotation.JsonInclude; +import static com.baeldung.boot.jackson.config.CoffeeConstants.LOCAL_DATETIME_SERIALIZER; @Configuration public class CoffeeCustomizerConfig { @@ -14,6 +13,6 @@ public class CoffeeCustomizerConfig { @Bean public Jackson2ObjectMapperBuilderCustomizer jsonCustomizer() { return builder -> builder.serializationInclusion(JsonInclude.Include.NON_NULL) - .serializers(localDateTimeSerializer); + .serializers(LOCAL_DATETIME_SERIALIZER); } } diff --git a/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/jackson/config/CoffeeHttpConverterConfiguration.java b/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/jackson/config/CoffeeHttpConverterConfiguration.java index b67f215816..eff2b5c252 100644 --- a/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/jackson/config/CoffeeHttpConverterConfiguration.java +++ b/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/jackson/config/CoffeeHttpConverterConfiguration.java @@ -1,13 +1,12 @@ package com.baeldung.boot.jackson.config; -import static com.baeldung.boot.jackson.config.CoffeeConstants.localDateTimeSerializer; - +import com.fasterxml.jackson.annotation.JsonInclude; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; -import com.fasterxml.jackson.annotation.JsonInclude; +import static com.baeldung.boot.jackson.config.CoffeeConstants.LOCAL_DATETIME_SERIALIZER; @Configuration public class CoffeeHttpConverterConfiguration { @@ -15,7 +14,7 @@ public class CoffeeHttpConverterConfiguration { @Bean public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() { Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder() - .serializers(localDateTimeSerializer) + .serializers(LOCAL_DATETIME_SERIALIZER) .serializationInclusion(JsonInclude.Include.NON_NULL); return new MappingJackson2HttpMessageConverter(builder.build()); } diff --git a/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/jackson/config/CoffeeJacksonBuilderConfig.java b/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/jackson/config/CoffeeJacksonBuilderConfig.java index ec4de1d353..8057fff3db 100644 --- a/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/jackson/config/CoffeeJacksonBuilderConfig.java +++ b/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/jackson/config/CoffeeJacksonBuilderConfig.java @@ -1,13 +1,12 @@ package com.baeldung.boot.jackson.config; -import static com.baeldung.boot.jackson.config.CoffeeConstants.localDateTimeSerializer; - +import com.fasterxml.jackson.annotation.JsonInclude; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; -import com.fasterxml.jackson.annotation.JsonInclude; +import static com.baeldung.boot.jackson.config.CoffeeConstants.LOCAL_DATETIME_SERIALIZER; @Configuration public class CoffeeJacksonBuilderConfig { @@ -16,7 +15,7 @@ public class CoffeeJacksonBuilderConfig { @Primary public Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder() { return new Jackson2ObjectMapperBuilder() - .serializers(localDateTimeSerializer) + .serializers(LOCAL_DATETIME_SERIALIZER) .serializationInclusion(JsonInclude.Include.NON_NULL); } } \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/jackson/config/CoffeeObjectMapperConfig.java b/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/jackson/config/CoffeeObjectMapperConfig.java index 3754de018a..f1ce6458ae 100644 --- a/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/jackson/config/CoffeeObjectMapperConfig.java +++ b/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/jackson/config/CoffeeObjectMapperConfig.java @@ -1,14 +1,13 @@ package com.baeldung.boot.jackson.config; -import static com.baeldung.boot.jackson.config.CoffeeConstants.localDateTimeSerializer; - -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Primary; - import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Primary; + +import static com.baeldung.boot.jackson.config.CoffeeConstants.LOCAL_DATETIME_SERIALIZER; @Configuration public class CoffeeObjectMapperConfig { @@ -17,7 +16,7 @@ public class CoffeeObjectMapperConfig { @Primary public ObjectMapper objectMapper() { JavaTimeModule module = new JavaTimeModule(); - module.addSerializer(localDateTimeSerializer); + module.addSerializer(LOCAL_DATETIME_SERIALIZER); return new ObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_NULL) .registerModule(module); } diff --git a/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/jackson/config/CoffeeRegisterModuleConfig.java b/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/jackson/config/CoffeeRegisterModuleConfig.java index ea00e5a58e..fc157f8156 100644 --- a/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/jackson/config/CoffeeRegisterModuleConfig.java +++ b/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/jackson/config/CoffeeRegisterModuleConfig.java @@ -1,13 +1,12 @@ package com.baeldung.boot.jackson.config; -import static com.baeldung.boot.jackson.config.CoffeeConstants.localDateTimeSerializer; - +import com.fasterxml.jackson.databind.Module; +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; -import com.fasterxml.jackson.databind.Module; -import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; +import static com.baeldung.boot.jackson.config.CoffeeConstants.LOCAL_DATETIME_SERIALIZER; @Configuration @PropertySource("classpath:coffee.properties") @@ -16,7 +15,7 @@ public class CoffeeRegisterModuleConfig { @Bean public Module javaTimeModule() { JavaTimeModule module = new JavaTimeModule(); - module.addSerializer(localDateTimeSerializer); + module.addSerializer(LOCAL_DATETIME_SERIALIZER); return module; } } diff --git a/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/jackson/controller/CoffeeController.java b/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/jackson/controller/CoffeeController.java index ad2f6ab942..23749b18a2 100644 --- a/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/jackson/controller/CoffeeController.java +++ b/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/jackson/controller/CoffeeController.java @@ -5,7 +5,7 @@ import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; -import java.time.LocalDateTime; +import static com.baeldung.boot.jackson.config.CoffeeConstants.FIXED_DATE; @RestController public class CoffeeController { @@ -15,7 +15,7 @@ public class CoffeeController { @RequestParam(required = false) String brand, @RequestParam(required = false) String name) { return new Coffee().setBrand(brand) - .setDate(LocalDateTime.now()) + .setDate(FIXED_DATE) .setName(name); } } diff --git a/spring-boot-modules/spring-boot-data-2/src/test/java/com/baeldung/boot/jackson/app/AbstractCoffeeIntegrationTest.java b/spring-boot-modules/spring-boot-data-2/src/test/java/com/baeldung/boot/jackson/app/AbstractCoffeeIntegrationTest.java index 13e1f05f97..f1bc35a8ce 100644 --- a/spring-boot-modules/spring-boot-data-2/src/test/java/com/baeldung/boot/jackson/app/AbstractCoffeeIntegrationTest.java +++ b/spring-boot-modules/spring-boot-data-2/src/test/java/com/baeldung/boot/jackson/app/AbstractCoffeeIntegrationTest.java @@ -6,9 +6,9 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.web.client.TestRestTemplate; -import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; +import static com.baeldung.boot.jackson.config.CoffeeConstants.FIXED_DATE; import static org.assertj.core.api.Assertions.assertThat; @SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @@ -19,13 +19,14 @@ public abstract class AbstractCoffeeIntegrationTest { @Test public void whenGetCoffee_thenSerializedWithDateAndNonNull() { - String formattedDate = DateTimeFormatter.ofPattern(CoffeeConstants.dateTimeFormat) - .format(LocalDateTime.now()); + String formattedDate = DateTimeFormatter.ofPattern(CoffeeConstants.DATETIME_FORMAT) + .format(FIXED_DATE); String brand = "Lavazza"; - String url = "/coffee?brand=" + brand; + String response = restTemplate.getForObject(url, String.class); + assertThat(response).isEqualTo( "{\"brand\":\"" + brand + "\",\"date\":\"" + formattedDate + "\"}"); }