BAEL-4687: returned fixed date instead of dynamic

This commit is contained in:
Adina Rolea 2020-12-17 14:23:06 +02:00
parent d4b22c74e3
commit d8f681c38e
8 changed files with 32 additions and 34 deletions

View File

@ -1,11 +1,13 @@
package com.baeldung.boot.jackson.config; package com.baeldung.boot.jackson.config;
import java.time.format.DateTimeFormatter;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer; import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class CoffeeConstants { public class CoffeeConstants {
public static final String dateTimeFormat = "dd-MM-yyyy HH:mm"; public static final String DATETIME_FORMAT = "dd-MM-yyyy HH:mm";
public static LocalDateTimeSerializer localDateTimeSerializer = new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(dateTimeFormat)); public static final LocalDateTime FIXED_DATE = LocalDateTime.now();
public static LocalDateTimeSerializer LOCAL_DATETIME_SERIALIZER = new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DATETIME_FORMAT));
} }

View File

@ -1,12 +1,11 @@
package com.baeldung.boot.jackson.config; 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.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import com.fasterxml.jackson.annotation.JsonInclude; import static com.baeldung.boot.jackson.config.CoffeeConstants.LOCAL_DATETIME_SERIALIZER;
@Configuration @Configuration
public class CoffeeCustomizerConfig { public class CoffeeCustomizerConfig {
@ -14,6 +13,6 @@ public class CoffeeCustomizerConfig {
@Bean @Bean
public Jackson2ObjectMapperBuilderCustomizer jsonCustomizer() { public Jackson2ObjectMapperBuilderCustomizer jsonCustomizer() {
return builder -> builder.serializationInclusion(JsonInclude.Include.NON_NULL) return builder -> builder.serializationInclusion(JsonInclude.Include.NON_NULL)
.serializers(localDateTimeSerializer); .serializers(LOCAL_DATETIME_SERIALIZER);
} }
} }

View File

@ -1,13 +1,12 @@
package com.baeldung.boot.jackson.config; 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.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; 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 @Configuration
public class CoffeeHttpConverterConfiguration { public class CoffeeHttpConverterConfiguration {
@ -15,7 +14,7 @@ public class CoffeeHttpConverterConfiguration {
@Bean @Bean
public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() { public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder() Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder()
.serializers(localDateTimeSerializer) .serializers(LOCAL_DATETIME_SERIALIZER)
.serializationInclusion(JsonInclude.Include.NON_NULL); .serializationInclusion(JsonInclude.Include.NON_NULL);
return new MappingJackson2HttpMessageConverter(builder.build()); return new MappingJackson2HttpMessageConverter(builder.build());
} }

View File

@ -1,13 +1,12 @@
package com.baeldung.boot.jackson.config; 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.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary; import org.springframework.context.annotation.Primary;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; 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 @Configuration
public class CoffeeJacksonBuilderConfig { public class CoffeeJacksonBuilderConfig {
@ -16,7 +15,7 @@ public class CoffeeJacksonBuilderConfig {
@Primary @Primary
public Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder() { public Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder() {
return new Jackson2ObjectMapperBuilder() return new Jackson2ObjectMapperBuilder()
.serializers(localDateTimeSerializer) .serializers(LOCAL_DATETIME_SERIALIZER)
.serializationInclusion(JsonInclude.Include.NON_NULL); .serializationInclusion(JsonInclude.Include.NON_NULL);
} }
} }

View File

@ -1,14 +1,13 @@
package com.baeldung.boot.jackson.config; 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.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; 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 @Configuration
public class CoffeeObjectMapperConfig { public class CoffeeObjectMapperConfig {
@ -17,7 +16,7 @@ public class CoffeeObjectMapperConfig {
@Primary @Primary
public ObjectMapper objectMapper() { public ObjectMapper objectMapper() {
JavaTimeModule module = new JavaTimeModule(); JavaTimeModule module = new JavaTimeModule();
module.addSerializer(localDateTimeSerializer); module.addSerializer(LOCAL_DATETIME_SERIALIZER);
return new ObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_NULL) return new ObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_NULL)
.registerModule(module); .registerModule(module);
} }

View File

@ -1,13 +1,12 @@
package com.baeldung.boot.jackson.config; 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.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource; import org.springframework.context.annotation.PropertySource;
import com.fasterxml.jackson.databind.Module; import static com.baeldung.boot.jackson.config.CoffeeConstants.LOCAL_DATETIME_SERIALIZER;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
@Configuration @Configuration
@PropertySource("classpath:coffee.properties") @PropertySource("classpath:coffee.properties")
@ -16,7 +15,7 @@ public class CoffeeRegisterModuleConfig {
@Bean @Bean
public Module javaTimeModule() { public Module javaTimeModule() {
JavaTimeModule module = new JavaTimeModule(); JavaTimeModule module = new JavaTimeModule();
module.addSerializer(localDateTimeSerializer); module.addSerializer(LOCAL_DATETIME_SERIALIZER);
return module; return module;
} }
} }

View File

@ -5,7 +5,7 @@ import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import java.time.LocalDateTime; import static com.baeldung.boot.jackson.config.CoffeeConstants.FIXED_DATE;
@RestController @RestController
public class CoffeeController { public class CoffeeController {
@ -15,7 +15,7 @@ public class CoffeeController {
@RequestParam(required = false) String brand, @RequestParam(required = false) String brand,
@RequestParam(required = false) String name) { @RequestParam(required = false) String name) {
return new Coffee().setBrand(brand) return new Coffee().setBrand(brand)
.setDate(LocalDateTime.now()) .setDate(FIXED_DATE)
.setName(name); .setName(name);
} }
} }

View File

@ -6,9 +6,9 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.test.web.client.TestRestTemplate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import static com.baeldung.boot.jackson.config.CoffeeConstants.FIXED_DATE;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ -19,13 +19,14 @@ public abstract class AbstractCoffeeIntegrationTest {
@Test @Test
public void whenGetCoffee_thenSerializedWithDateAndNonNull() { public void whenGetCoffee_thenSerializedWithDateAndNonNull() {
String formattedDate = DateTimeFormatter.ofPattern(CoffeeConstants.dateTimeFormat) String formattedDate = DateTimeFormatter.ofPattern(CoffeeConstants.DATETIME_FORMAT)
.format(LocalDateTime.now()); .format(FIXED_DATE);
String brand = "Lavazza"; String brand = "Lavazza";
String url = "/coffee?brand=" + brand; String url = "/coffee?brand=" + brand;
String response = restTemplate.getForObject(url, String.class); String response = restTemplate.getForObject(url, String.class);
assertThat(response).isEqualTo( assertThat(response).isEqualTo(
"{\"brand\":\"" + brand + "\",\"date\":\"" + formattedDate + "\"}"); "{\"brand\":\"" + brand + "\",\"date\":\"" + formattedDate + "\"}");
} }