SpringBoot学习之路:06.Spring Boot替换默认的Jackson

       SpringBoot和Springmvc都可以返回接送数据,SpringBoot默认是使用Jackson解析json数据的,个人觉得阿里的Fastjson性能更好点,API使用更方便,于是将SpringBoot默认的Jackson替换成阿里的Fastjson。

一.配置类注入的方式

package com.maxbill.core.config.json;

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.web.HttpMessageConverters; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.converter.HttpMessageConverter;

/** * @功能 JSON解析器配置 * @作者 zuoshuai(MaxBill) * @日期 2017/7/6 * @时间 12:24 * @备注 替换默认的json框架,替换成阿里的fastjson */ @Configuration public class JsonConfig {

1@Bean 2public HttpMessageConverters fastJsonHttpMessageConverters() { 3 FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter(); 4 FastJsonConfig fastJsonConfig = new FastJsonConfig(); 5 fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); 6 fastConverter.setFastJsonConfig(fastJsonConfig); 7 HttpMessageConverter<?> converter = fastConverter; 8 return new HttpMessageConverters(converter); 9}

}

二.配置类继承WebMvcConfigurerAdapter覆盖方法

package com.maxbill.core.config.json;

import com.alibaba.fastjson.serializer.SerializerFeature; import com.alibaba.fastjson.support.config.FastJsonConfig; import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter; import org.springframework.context.annotation.Configuration; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.List;

/** * @功能 JSON解析器配置 * @作者 zuoshuai(MaxBill) * @日期 2017/7/6 * @时间 12:35 * @备注 替换默认的json框架,替换成阿里的fastjson */ @Configuration public class JsonConfigBack extends WebMvcConfigurerAdapter {

1@Override 2public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { 3 super.configureMessageConverters(converters); 4 FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter(); 5 FastJsonConfig fastJsonConfig = new FastJsonConfig(); 6 fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); 7 fastConverter.setFastJsonConfig(fastJsonConfig); 8 converters.add(fastConverter); 9}

}

注意:记得引入Fastjson的依赖包;在1.2.10版本以后有两个方法支持HttpMessageconvert了

一:FastJsonHttpMessageConverter,支持4.2以下的版本;

二:FastJsonHttpMessageConverter4支持4.2以上的版本。

所以Fastjson需要在1.2.10版本以上。

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

SpringBoot系列——Jackson序列化

  前言  SpringBoot提供了与三个JSON映射库的集成:GsonJacksonJSONB  Jackson是首选的默认库。  官网介绍:  https://docs.spring.io/springboot/docs/2.1.6.RELEASE/reference/html/boot

SpringBoot学习之路:06.Spring Boot替换默认的Jackson

    SpringBoot和Springmvc都可以返回接送数据,SpringBoot默认是使用Jackson解析json数据的,个人觉得阿里的Fastjson性能更好点,API使用更方便,于是将SpringBoot默认的Jackson替换成阿里的Fastjson。一.配置类注入的方式packagecom.maxbill.core.

SpringBoot系列——Jackson序列化

  前言  SpringBoot提供了与三个JSON映射库的集成:GsonJacksonJSONB  Jackson是首选的默认库。  官网介绍:  https://docs.spring.io/springboot/docs/2.1.6.RELEASE/reference/html/boot

SpringBoot学习之路:06.Spring Boot替换默认的Jackson - HelloWorld