BAEL-4687: added web configuration for jackson

This commit is contained in:
Adina Rolea 2020-10-27 16:12:59 +02:00
parent 9b8455e829
commit 2a8ff6f23b
3 changed files with 41 additions and 2 deletions

View File

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

View File

@ -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<HttpMessageConverter<?>> converters) {
converters.add(mappingJackson2HttpMessageConverter());
}
@Bean
public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder()
.serializers(localDateTimeSerializer)
.serializationInclusion(JsonInclude.Include.NON_NULL);
return new MappingJackson2HttpMessageConverter(builder.build());
}
}

View File

@ -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
spring.jackson.serialization.write-dates-as-timestamps=false