springboot-4-整合fastjson
使用fastjson作为springboot的默认json解析, 原来使用的是jackson
1, 引入依赖
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.</version>
</dependency>
注意: FastJsonHttpMessageConverter4支持4.2以上版本
FastJsonHttpMessageConverter,支持4.2以下的版本
第一种方法:
(1)启动类继承extends WebMvcConfigurerAdapter
(2)覆盖方法configureMessageConverters
@SpringBootApplication
public class ApiCoreApp extends WebMvcConfigurerAdapter { @Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
super.configureMessageConverters(converters);
// 定义一个convert转换消息对象
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
// 添加fastjosn配置信息, 比如是否格式化返回的json数据
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(
SerializerFeature.PrettyFormat
);
// convert中添加配置
fastConverter.setFastJsonConfig(fastJsonConfig);
// 添加到convert中
converters.add(fastConverter);
}
}
第二种方法:
(1)在App.java启动类中,注入Bean : HttpMessageConverters
package com.iwhere; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.bind.annotation.RestController; import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter; @RestController
@SpringBootApplication
public class App {
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters() {
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat,
SerializerFeature.WriteMapNullValue,
SerializerFeature.WriteNullListAsEmpty,
SerializerFeature.WriteNullNumberAsZero,
SerializerFeature.WriteNullBooleanAsFalse,
SerializerFeature.WriteNullStringAsEmpty);
fastConverter.setFastJsonConfig(fastJsonConfig);
// 返回HttpMessageConvert对象
HttpMessageConverter<?> converter = fastConverter;
return new HttpMessageConverters(converter);
}
}
测试方法:
在实体类中使用@JSONField(Serialize=false) 字段不返回即是成功
在data字段上添加
@JSONFIled(format="yyyy-mm-dd HH:mm:ss")
既可以实现自动转化
本博客来源: http://412887952-qq-com.iteye.com/blog/2315202
springboot-4-整合fastjson的更多相关文章
- springboot整合fastJson遇到重定向问题
通过网上教程使用springboot整合fastJson后遇到页面重定向问题(使用的springboot版本是2.0.2.RELEASE ,其他的版本可能不会出现以下问题),如下图: 我的项目结构如下 ...
- springboot下整合各种配置文件
本博是在springboot下整合其他中间件,比如,mq,redis,durid,日志...等等 以后遇到再更.springboot真是太便捷了,让我们赶紧涌入到springboot的怀抱吧. ap ...
- springBoot 官方整合的redis 使用教程:(StringRedisTemplate 方式存储 Object类型value)
前言:最近新项目准备用 redis 简单的缓存 一些查询信息,以便第二次查询效率高一点. 项目框架:springBoot.java.maven 说明:edis存储的数据类型,key一般都是Strin ...
- SpringBoot2.0整合fastjson的正确姿势
SpringBoot2.0如何集成fastjson?在网上查了一堆资料,但是各文章的说法不一,有些还是错的,可能只是简单测试一下就认为ok了,最后有没生效都不知道.恰逢公司项目需要将J ...
- SpringBoot 同时整合thymeleaf html、vue html和jsp
问题描述 SpringBoot如何同时访问html和jsp SpringBoot访问html页面可以,访问jsp页面报错 SpringBoot如何同时整合thymeleaf html.vue html ...
- SpringBoot+AOP整合
SpringBoot+AOP整合 https://blog.csdn.net/lmb55/article/details/82470388 https://www.cnblogs.com/onlyma ...
- SpringBoot+Redis整合
SpringBoot+Redis整合 1.在pom.xml添加Redis依赖 <!--整合Redis--> <dependency> <groupId>org.sp ...
- SpringBoot+Swagger整合API
SpringBoot+Swagger整合API Swagger:整合规范的api,有界面的操作,测试 1.在pom.xml加入swagger依赖 <!--整合Swagger2配置类--> ...
- springboot+maven整合spring security
springboot+maven整合spring security已经做了两次了,然而还是不太熟悉,这里针对后台简单记录一下需要做哪些事情,具体的步骤怎么操作网上都有,不再赘述.1.pom.xml中添 ...
- SpringBoot Mybatis整合(注解版),SpringBoot集成Mybatis(注解版)
SpringBoot Mybatis整合(注解版),SpringBoot集成Mybatis(注解版) ================================ ©Copyright 蕃薯耀 2 ...
随机推荐
- Andfix热修复技术使用
AndFix,全称是Android hot-fix.是阿里开源的一个Android热补丁框架,允许APP在不重新发版本的情况下修复线上的bug.支持Android 2.3 到 6.0. andfix的 ...
- 开源WebGIS实施方案(六):空间数据(PostGIS)与GeoServer服务迁移
研发环境的变更,或者研发完成进行项目现场实施.运维的时候,经常就会面临数据及服务的迁移,这其中就包含空间数据以及GeoServer服务的迁移工作. 这里需要提醒的是:如果采用的是类似的开源WebGIS ...
- Jesery客户端工具类
public class JerseyClientUtil { public static<T> T sendMsg(String url,Object object,Class<T ...
- 使用Postman验证TFS Rest API
概述 你可能已经了解到,TFS自2015版本发布以来,开始支持通过REST API的方式提供接口服务,第三方平台可以通过通用的HTTP协议访问TFS系统,获取数据.请求编译等.REST API在原有. ...
- [JS] IE下ajax请求不生效或者请求结果不更新
问题描述: IE8及以下版本里用jQuery发简单的GET时,第一次或者新开窗口后的请求没问题,可以正确返回结果.但是之后刷新页面或者触发某些操作得到的ajax请求结果永远和第一次一样. 问题分析: ...
- 如何连接MSSQLSERVER2014(其他版本数据库大体相同)
购买了服务器后我在上面安装了SqlServer2014,但是程序中总是远程连接不上,特此记录一下: 一.一些基本设置如下链接: 转载自:https://blog.sun0816.com/20381.h ...
- NetCore入门篇:(七)Net Core项目使用Controller之二
一.简介 1.说明Post,Get定义的区别. 2.说明如何路由定义. 二.Get.Post定义 1.api不定义访问方式时,同时支持get 和 post.如果定义某种方式,则仅支持某种方式.具体看代 ...
- 超简单工具puer——“低碳”的前后端分离开发
本文由作者郑海波授权网易云社区发布. 前几天,跟一同事(MIHTool作者)讨教了一下开发调试工具.其实个人觉得相较于定制一个类似MIHTool的Hybrid App容器,基于长连的B/S架构的工具其 ...
- 登录状态保持Session/Cookie
登录成功: 1.Session保存User对象. 2.Cookie保存唯一值UserID(或者usercode)和加密Sign(生成规则自己定义,MD5用户名,用户ID,私有串等),并设置过期时间. ...
- Mysql -Linux系统下安装指南
博客参考: https://www.cnblogs.com/pyyu/p/9467289.html 1. Mysql安装 .首先在 RHEL/CentOS 和 Fedora 操作系统中添加 Mari ...