FastJsonConfig @JSONField 配置覆盖问题

FastJsonConfig @JSONField 日期格式配置覆盖问题

实体类

 
 public class ClueResp { 
    @JSONField(format="yyyy-MM-dd") 
    private Date happendDate;
    
    private Date operateDate;

    ……省略 set get
}

配置类


@Configuration
public class FastJsonConfiguration { 

    @Bean
    public HttpMessageConverters fastJsonHttpMessageConverters() { 
        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter(); 
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
        fastJsonConfig.setSerializerFeatures(
                SerializerFeature.PrettyFormat,
                SerializerFeature.WriteNullStringAsEmpty,
                SerializerFeature.WriteMapNullValue,
                SerializerFeature.WriteNullListAsEmpty);
                
        fastJsonConfig.setSerializeFilters(new ValueFilter() {
            // Map<class,Map<filed,dateFormat>>
            Map<Class, Map<String, String>> dateFormatCache = new ConcurrentHashMap<>();

            @Override
            public Object process(Object obj, String key, Object value) {

                if (value instanceof Date) {
                    Class<?> objClass = obj.getClass();
                    if (dateFormatCache.get(objClass) == null) {
                        dateFormatCache.putIfAbsent(objClass, new HashMap<>());
                        Map<String, String> newFormatMap = dateFormatCache.get(objClass);
                        ReflectionUtils.doWithFields(obj.getClass(), (field) -> {
                            if (field.getType() == Date.class) {
                                JSONField annotation = field.getAnnotation(JSONField.class);
                                if (annotation != null) {
                                    String format = annotation.format();
                                    if (StringUtils.isNotBlank(format)) {
                                        newFormatMap.putIfAbsent(field.getName(), format);
                                    }
                                }
                            }
                        });
                    }

                    Map<String, String> dateFormatMap = dateFormatCache.get(objClass);
                    if (!dateFormatMap.isEmpty()) {
                        String format = dateFormatMap.get(key);
                        if(StringUtils.isNotBlank(format)) {
                            SimpleDateFormat sdf = new SimpleDateFormat(format);
                            return sdf.format(value);
                        }
                    }
                }
                return value;
            }
        });

        //中文乱码解决方案
        List<MediaType> fastMedisTypes = new ArrayList<MediaType>();
        //设定Json格式且编码为utf-8
        fastMedisTypes.add(MediaType.APPLICATION_JSON_UTF8);
        fastConverter.setSupportedMediaTypes(fastMedisTypes);
        //将转换规则应用于转换对象
        fastConverter.setFastJsonConfig(fastJsonConfig);

        //long返回给前端转换为String
        SerializeConfig serializeConfig = fastJsonConfig.getSerializeConfig();
        serializeConfig.put(Long.class, ToStringSerializer.instance);
        serializeConfig.put(Long.TYPE, ToStringSerializer.instance);
        fastConverter.setFastJsonConfig(fastJsonConfig);

        return new HttpMessageConverters(fastConverter);
    }
}

主要代码

fastJsonConfig.setSerializeFilters(new ValueFilter() {
        // Map<class,Map<filed,dateFormat>>
            Map<Class, Map<String, String>> dateFormatCache = new ConcurrentHashMap<>();

            @Override
            public Object process(Object obj, String key, Object value) {

                if (value instanceof Date) {
                    Class<?> objClass = obj.getClass();
                    if (dateFormatCache.get(objClass) == null) {
                        dateFormatCache.putIfAbsent(objClass, new HashMap<>());
                        Map<String, String> newFormatMap = dateFormatCache.get(objClass);
                        ReflectionUtils.doWithFields(obj.getClass(), (field) -> {
                            if (field.getType() == Date.class) {
                                JSONField annotation = field.getAnnotation(JSONField.class);
                                if (annotation != null) {
                                    String format = annotation.format();
                                    if (StringUtils.isNotBlank(format)) {
                                        newFormatMap.putIfAbsent(field.getName(), format);
                                    }
                                }
                            }
                        });
                    }

                    Map<String, String> dateFormatMap = dateFormatCache.get(objClass);
                    if (!dateFormatMap.isEmpty()) {
                        String format = dateFormatMap.get(key);
                        if(StringUtils.isNotBlank(format)) {
                            SimpleDateFormat sdf = new SimpleDateFormat(format);
                            return sdf.format(value);
                        }
                    }
                }

                return value;
            }
        });

结果

{
  "code": 0,
  "data": {
    "happendDate": "2020-11-29",
    "operateDate": "2020-11-29 11:14:33"
  },
  "message": "成功"
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值