spring boot @ResponseBody转换JSON 时 Date 类型处理方法 ,这里一共有两种不同解析方式(Jackson和FastJson两种方式,springboot我用的1.x的版本)

第一种方式:默认的json处理是 jackson 也就是对configureMessageConverters 没做配置时

  mybatis数据查询返回的时间,是一串数字,如何转化成时间。两种方法,推荐第一种

  方法一:

  可以在apllication.property加入下面配置就可以

  #时间戳统一转换
  spring.jackson.date-format=yyyy-MM-dd HH:mm:ss

  spring.jackson.time-zone=GMT+8

  方法二:

  @JsonFormat(timezone = "GMT+8", pattern = "yyyyMMddHHmmss")

  private Date createTime;

第二种方式:当configureMessageConverters 配置为FasJson处理时;

  方法一:全局配置:    fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");

@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter { @Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
super.configureMessageConverters(converters); FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter(); FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(
SerializerFeature.WriteNullListAsEmpty,
SerializerFeature.WriteMapNullValue,
SerializerFeature.WriteNullStringAsEmpty
); //此处是全局处理方式
fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss"); fastConverter.setFastJsonConfig(fastJsonConfig); List<MediaType> supportedMediaTypes = new ArrayList<MediaType>();
supportedMediaTypes.add(MediaType.ALL); // 全部格式
fastConverter.setSupportedMediaTypes(supportedMediaTypes);
converters.add(fastConverter);
}
}

  

  方法二:在所需要的字段上配置(比较灵活的方式,根据不同需求转换):

  @JSONField(format="yyyyMMdd")

  private Date createTime;

说明:这里如果字段和全局都配置了 ,最后是以全局转换

重要补充:

当springboot版本是2.0.9以上配置fastjson不生效解决如下:

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.StringHttpMessageConverter; import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List; @Configuration
public class MyConfiguration { @Bean
public HttpMessageConverters customConverters() {
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
// 创建配置类
FastJsonConfig config = new FastJsonConfig();
config.setSerializerFeatures(
SerializerFeature.WriteNullListAsEmpty,
SerializerFeature.WriteMapNullValue,
SerializerFeature.WriteNullStringAsEmpty
); //此处是全局处理方式
config.setDateFormat("yyyy-MM-dd HH:mm:ss");
config.setCharset(Charset.forName("UTF-8"));
fastConverter.setFastJsonConfig(config); List<MediaType> supportedMediaTypes = new ArrayList<>();
supportedMediaTypes.add(MediaType.ALL);
fastConverter.setSupportedMediaTypes(supportedMediaTypes);
//支持text 转string
StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter();
return new HttpMessageConverters(fastConverter, stringHttpMessageConverter);
}

参考spring官网文档:https://docs.spring.io/spring-boot/docs/2.0.9.BUILD-SNAPSHOT/reference/htmlsingle/#boot-features-spring-mvc-message-converters

spring boot @ResponseBody转换JSON 时 Date 类型处理方法,Jackson和FastJson两种方式,springboot 2.0.9配置fastjson不生效官方解决办法的更多相关文章

  1. Eclipse项目中引用第三方jar包时将项目打包成jar文件的两种方式

    转载自:http://www.cnblogs.com/lanxuezaipiao/p/3291641.html 方案一:用Eclipse自带的Export功能 步骤1:准备主清单文件 “MANIFES ...

  2. Spring自动注入,类型注入、名称注入(两种方式)

    参考: https://blog.csdn.net/qq_41767337/article/details/89002422 https://www.iteye.com/blog/breezylee- ...

  3. android启动第一个界面时即闪屏的核心代码(两种方式)

    闪屏,就是SplashScreen,也能够说是启动画面,就是启动的时候,闪(展示)一下,持续数秒后.自己主动关闭.  第一种方式: android的实现很easy,使用Handler对象的postDe ...

  4. Spring boot之返回json数据

    1.步骤: 1. 编写实体类Demo 2. 编写getDemo()方法 3. 测试 2.项目构建 编写实体类Demo package com.kfit; /** * 这是一个测试实体类. */ pub ...

  5. 解决nodejs中json序列化时Date类型默认为UTC格式

    在nodejs中,json序列化时Date类型时,默认转为UTC格式. 如下图 上面只是一个例子,下面我用一个更具体化的例子来展示一个这个情况,我们在开发WEB项目中,经常用到Express组件, 我 ...

  6. 解决nodejs中json序列化时Date类型为UTC格式

    在nodejs中,json序列化时Date类型时,默认转为UTC格式. 如下图 zhupengfei@DESKTOP-HJASOE3 MINGW64 /d/MyProject/exp2 $ node ...

  7. Spring Boot 之遇见JSON

    MVC框架中,Spring Boot内置了jackson来完成JSON的序列化和反序列化操作,并且,在与其他技术集成的时候,如Redis.MongoDB.Elasticsearch等对象序列化,都可使 ...

  8. Spring Boot 之使用 Json 详解

    Spring Boot 之使用 Json 详解 简介 Spring Boot 支持的 Json 库 Spring Web 中的序列化.反序列化 指定类的 Json 序列化.反序列化 @JsonTest ...

  9. 三:C#对象转换Json时的一些高级(特殊)设置;

    导航目录: Newtonsoft.Json 概述 一:Newtonsoft.Json 支持序列化与反序列化的.net 对象类型:    二:C#对象.集合.DataTable与Json内容互转示例: ...

随机推荐

  1. FFmpeg 学习(七):FFmpeg 学习整理总结

    一.FFmpeg 播放视频的基本流程整理 播放流程: video.avi(Container) -> 打开得到 Video_Stream -> 读取Packet -> 解析到 Fra ...

  2. 吴恩达机器学习笔记30-神经网络的反向传播算法(Backpropagation Algorithm)

    之前我们在计算神经网络预测结果的时候我们采用了一种正向传播方法,我们从第一层开始正向一层一层进行计算,直到最后一层的ℎ

  3. JS创建对象,数组,函数的三种方式

    害怕自己忘记,简单总结一下 创建对象的3种方法 ①:创建一个空对象   var obj = {}; ②:对象字面量 var obj = { name: "Tom", age: 27 ...

  4. npm安装webpack失败(mac和window都可能会遇到这样的情况,以下问题主要以mac为例)

     问题描述:我想查看一下webpack的版本,于是输入了命令webpack -v, 结果如下图所示: 注:这里提示我们要安装webpack-cli,是因为到了webpack4, webpack 已经将 ...

  5. 基于阿里云 DNS API 实现的 DDNS 工具

    0.简要介绍 0.1 思路说明 AliDDNSNet 是基于 .NET Core 开发的动态 DNS 解析工具,借助于阿里云的 DNS API 来实现域名与动态 IP 的绑定功能.工具核心就是调用了阿 ...

  6. SQL中的Join和Where的区别

    一.sql语句中left join.inner join中的on与where的区别 0.各种join操作的概念和作用 left join :左连接,返回左表中所有的记录以及右表中连接字段相等的记录. ...

  7. Net Core平台灵活简单的日志记录框架NLog+SqlServer初体验

    Net Core平台灵活简单的日志记录框架NLog+SqlServer初体验 前几天分享的"[Net Core平台灵活简单的日志记录框架NLog+Mysql组合初体验][http://www ...

  8. Asp.Net Core Web Api图片上传(一)集成MongoDB存储实例教程

    Asp.Net Core Web Api图片上传及MongoDB存储实例教程(一) 图片或者文件上传相信大家在开发中应该都会用到吧,有的时候还要对图片生成缩略图.那么如何在Asp.Net Core W ...

  9. Java垃圾回收(GC)机制详解

    一.为什么需要垃圾回收 如果不进行垃圾回收,内存迟早都会被消耗空,因为我们在不断的分配内存空间而不进行回收.除非内存无限大,我们可以任性的分配而不回收,但是事实并非如此.所以,垃圾回收是必须的. 二. ...

  10. 【云服务器部署】---Linux下安装MySQL

    [云服务器部署]---Linux下安装MySQL 有关如何阿里云ECS建网站,推荐一片文章,我是是通过这篇文章安装tomcat和jdk的 网址:阿里云ECS建网站(建站)超详细全套完整图文教程! 注意 ...