package com.dji.sample.configuration;
|
|
import com.fasterxml.jackson.annotation.JsonInclude;
|
import com.fasterxml.jackson.core.JsonGenerator;
|
import com.fasterxml.jackson.core.JsonParser;
|
import com.fasterxml.jackson.databind.*;
|
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
|
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.json.Jackson2ObjectMapperBuilder;
|
|
import java.io.IOException;
|
import java.time.LocalDateTime;
|
import java.time.format.DateTimeFormatter;
|
|
@Configuration
|
public class SpringBeanConfiguration {
|
|
@Bean
|
// @ConditionalOnMissingBean(ObjectMapper.class)
|
public ObjectMapper objectMapper(Jackson2ObjectMapperBuilder builder) {
|
ObjectMapper objectMapper = builder.createXmlMapper(false).build();
|
objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
|
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
|
|
JavaTimeModule timeModule = new JavaTimeModule();
|
timeModule.addDeserializer(LocalDateTime.class,
|
new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
|
timeModule.addSerializer(LocalDateTime.class,
|
new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
|
|
objectMapper.disable(MapperFeature.IGNORE_DUPLICATE_MODULE_REGISTRATIONS);
|
objectMapper.registerModules(timeModule);
|
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
|
objectMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
|
objectMapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
|
objectMapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true);
|
objectMapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>() {
|
@Override
|
public void serialize(Object value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
|
gen.writeString("");
|
}
|
});
|
return objectMapper;
|
}
|
}
|