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;
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));
}

View File

@ -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);
}
}

View File

@ -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());
}

View File

@ -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);
}
}

View File

@ -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);
}

View File

@ -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;
}
}

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.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);
}
}

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.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 + "\"}");
}