zoukankan      html  css  js  c++  java
  • 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;
      }
    
  • 相关阅读:
    (4)使用 JDK8 日期時間 API
    (3)使用 Joda-Time
    (2)時間的 ABC
    (1)Date 與 Calendar 怎麼了?
    Android 开发绕不过的坑:你的 Bitmap 究竟占多大内存?
    AsyncTask 源码阅读笔记
    LinkedHashMap 阅读笔记
    HashMap 阅读笔记
    DiskLruCache 阅读笔记
    Android面试复习
  • 原文地址:https://www.cnblogs.com/WillingCPP/p/13891660.html
Copyright © 2011-2022 走看看