• springboot LocalDateTime,Json数据序列化


    1. 逻辑说明

    spring程序启动后会自动创建对象转换工具org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter
    根据能找到的java解析json包来依次处理

    jaxb2Present = ClassUtils.isPresent("javax.xml.bind.Binder", classLoader);
    jackson2Present = ClassUtils.isPresent("com.fasterxml.jackson.databind.ObjectMapper", classLoader) && ClassUtils.isPresent("com.fasterxml.jackson.core.JsonGenerator", classLoader);
    jackson2XmlPresent = ClassUtils.isPresent("com.fasterxml.jackson.dataformat.xml.XmlMapper", classLoader);
    jackson2SmilePresent = ClassUtils.isPresent("com.fasterxml.jackson.dataformat.smile.SmileFactory", classLoader);
    gsonPresent = ClassUtils.isPresent("com.google.gson.Gson", classLoader);
    jsonbPresent = ClassUtils.isPresent("javax.json.bind.Jsonb", classLoader);
    

    创建AbstractJackson2HttpMessageConverter对象
    不走bean的创建过程, 所以spring.jackson.* 配置和注入ObjectMapper的方式都是没有办法调整解析结果的
    有效的方式有两种

    1). WebMvcConfigurationSupport中修改

    //启动的时候进入ObjectMapper 构造断点,系统自动创建了
    @Override
    protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        super.extendMessageConverters(converters);
        for (HttpMessageConverter<?> converter : converters){
            if (converter instanceof AbstractJackson2HttpMessageConverter){
               AbstractJackson2HttpMessageConverter jackson2HttpMessageConverter = (AbstractJackson2HttpMessageConverter)converter;
                jackson2HttpMessageConverter.getObjectMapper().disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
                jackson2HttpMessageConverter.getObjectMapper().setLocale(Locale.CHINA);
                jackson2HttpMessageConverter.getObjectMapper().setTimeZone(TimeZone.getTimeZone("GMT+:08:00"));
                jackson2HttpMessageConverter.getObjectMapper().setDateFormat(DateTimeFormatter.ofPattern(DatePattern.NORM_DATETIME_PATTERN));
                //jackson2HttpMessageConverter.getObjectMapper().setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"))            
            }
        }
    }
    

    2). 使用 @DateTimeFormat 和 @JsonFormat 注解,@JsonFormat(timezone = "GMT+8", pattern = "yyyyMMddHHmmss")

    参考com.fasterxml.jackson.datatype.jsr310.ser中的逻辑判断

    先用 this._formatter ,不存在使用_defaultFormatter

     public void serialize(LocalDateTime value, JsonGenerator g, SerializerProvider provider) throws IOException {
            if (this.useTimestamp(provider)) {
                g.writeStartArray();
                this._serializeAsArrayContents(value, g, provider);
                g.writeEndArray();
            } else {
                DateTimeFormatter dtf = this._formatter;
                if (dtf == null) {
                    dtf = this._defaultFormatter();
                }
                g.writeString(value.format(dtf));
            }
        }
     protected DateTimeFormatter _defaultFormatter() {
           return DateTimeFormatter.ISO_LOCAL_DATE_TIME;
      }
    
  • 相关阅读:
    文件编码转换
    mysql密码的奇怪问题
    python文件读写
    python中JSON的使用
    mysql默认字符编码的修改
    烧写uboot与linux操作系统,安装Samba,jlink驱动安装
    Busybox是什么?
    ubuntu 搭建GTK+以及glade2集成开发环境的一些方法
    Ubuntu linux安装ssh server
    UBoot启动过程(国嵌)
  • 原文地址:https://www.cnblogs.com/WillingCPP/p/13891660.html
Copyright © 2020-2023  润新知