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

Easter79
• 阅读 959

       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 {

@Bean
public HttpMessageConverters fastJsonHttpMessageConverters() {
    FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
    fastConverter.setFastJsonConfig(fastJsonConfig);
    HttpMessageConverter<?> converter = fastConverter;
    return new HttpMessageConverters(converter);
}

}

二.配置类继承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 {

@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    super.configureMessageConverters(converters);
    FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
    fastConverter.setFastJsonConfig(fastJsonConfig);
    converters.add(fastConverter);
}

}

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

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

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

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

点赞
收藏
评论区
推荐文章
blmius blmius
2年前
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
kenx kenx
2年前
SpringBoot 默认json解析器详解和字段序列化自定义
前言在我们开发项目API接口的时候,一些没有数据的字段会默认返回NULL,数字类型也会是NULL,这个时候前端希望字符串能够统一返回空字符,数字默认返回0,那我们就需要自定义json序列化处理SpringBoot默认的json解析方案我们知道在springboot中有默认的json解析器,SpringBoot中默认使用的Json解析技术框架是ja
Jacquelyn38 Jacquelyn38
2年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
Easter79 Easter79
2年前
SpringBoot系列——Jackson序列化
  前言  SpringBoot提供了与三个JSON映射库的集成:GsonJacksonJSONB  Jackson是首选的默认库。  官网介绍:  https://docs.spring.io/springboot/docs/2.1.6.RELEASE/reference/html/boot
Stella981 Stella981
2年前
SpringBoot系列——Jackson序列化
  前言  SpringBoot提供了与三个JSON映射库的集成:GsonJacksonJSONB  Jackson是首选的默认库。  官网介绍:  https://docs.spring.io/springboot/docs/2.1.6.RELEASE/reference/html/boot
Stella981 Stella981
2年前
Spring Boot 406(type=Not Acceptable, status=406)异常解决办法
使用SpringBoot,Controller请求返回的参数类型是ResponseBody,如果请求的时候使用使用配置的默认请求扩展名,例如.html,SpringMVC会抛出一个typeNotAcceptable,status406错误,如下:WhitelabelErrorPageThisapplica
Stella981 Stella981
2年前
BeetlSQL 3.0.10 发布,多数据源分布式sega事务支持
本次发布主要增加了分布式Sega事务支持,适合多数据源按照社区建议,修改了了springboot的yml配置方式修改了@Jackson和@UpdateTime,本来是用来作为例子,但社区开发者提供了较好的完整实现增加Sega支持<dependency<groupIdcom.ibeetl</gr
Stella981 Stella981
2年前
SpringBoot Jackson 框架返回结果处理
1\.常用框架框架阿里fastjson谷歌gsonJavaBean序列化为Json,性能:JacksonFastJsonGsonJsonlib2\.Jackson处理相关结果1\.指定字段
Stella981 Stella981
2年前
Spring Boot Jackson命名策略
在SpringBoot的Jackson中我们可以使用@JsonProperty对Java属性转Json字符串的key进行指定。那么,当批量处理统一类型的格式时,@JsonProperty就显得比较麻烦了。publicclassLoginUser{@JsonProperty("user_name")priv
Stella981 Stella981
2年前
SpringBoot学习之路:06.Spring Boot替换默认的Jackson
    SpringBoot和Springmvc都可以返回接送数据,SpringBoot默认是使用Jackson解析json数据的,个人觉得阿里的Fastjson性能更好点,API使用更方便,于是将SpringBoot默认的Jackson替换成阿里的Fastjson。一.配置类注入的方式packagecom.maxbill.core.
Easter79
Easter79
Lv1
今生可爱与温柔,每一样都不能少。
文章
2.8k
粉丝
5
获赞
1.2k