美文网首页
ObjectMapper时间格式化

ObjectMapper时间格式化

作者: 荒天帝886 | 来源:发表于2019-05-16 15:32 被阅读0次

作用:规范化LocalDateTime

package com.fzy.javastudy.spring.config;
import com.fasterxml.jackson.databind.ObjectMapper;
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.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Configuration
public class LocalDateTimeConfig {
    private final static String DATE_TIME_FORMATTER = "yyyy-MM-dd HH:mm:ss";
    @Bean
    ObjectMapper objectMapper() {
        return new Jackson2ObjectMapperBuilder()
                .findModulesViaServiceLoader(true)
                .serializerByType(LocalDateTime.class, new LocalDateTimeSerializer(
                        DateTimeFormatter.ofPattern(DATE_TIME_FORMATTER)))
                .deserializerByType(LocalDateTime.class, new LocalDateTimeDeserializer(
                        DateTimeFormatter.ofPattern(DATE_TIME_FORMATTER)))
                .build();
    }
}

相关文章

网友评论

      本文标题:ObjectMapper时间格式化

      本文链接:https://www.haomeiwen.com/subject/touiaqtx.html