SpringBoot使用FastJson 替换 jackson

fastJson使用比较多。用起来顺手。所以想把jackson替换掉。

  1. 引入最新fastjson。maven引入不用多说啦。
  2. 新建类,继承自WebMvcConfigurerAdapter,重写extendMessageConverters方法。

import com.alibaba.fastjson.serializer.SerializerFeature
import com.alibaba.fastjson.support.config.FastJsonConfig
import org.springframework.context.annotation.Configuration
import org.springframework.http.converter.HttpMessageConverter
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter
import org.springframework.boot.SpringBootConfiguration
import org.springframework.http.MediaType
import org.springframework.web.servlet.config.annotation.EnableWebMvc
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter

@SpringBootConfiguration
@EnableWebMvc
open class WebMvcConfigFastJson : WebMvcConfigurerAdapter(){
    /**
     *  这里是kotlin的语法。java基本一样。
     */
    override fun extendMessageConverters(converters: MutableList<HttpMessageConverter<*>>) {
        super.extendMessageConverters(converters)
        //这里把springBoot依赖的jackson去掉。
        //如果没有其他包依赖jackson,也可以直接pom里把jackson去掉
        converters.removeIf({ httpMessageConverter -> httpMessageConverter is MappingJackson2HttpMessageConverter })

        val converter:FastJsonHttpMessageConverter  = FastJsonHttpMessageConverter()
        var config:FastJsonConfig =  FastJsonConfig()
        config.dateFormat = "yyyy-MM-dd HH:mm:ss"
        config.setSerializerFeatures(
                SerializerFeature.BrowserSecure
                ,SerializerFeature.WriteBigDecimalAsPlain
                ,SerializerFeature.WriteEnumUsingToString
                ,SerializerFeature.WriteDateUseDateFormat)
        //处理中文乱码问题
        var fastMediaTypes:ArrayList<MediaType> = ArrayList<MediaType>()
        fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8)
        converter.setSupportedMediaTypes(fastMediaTypes)
        converter.setFastJsonConfig(config)
        converters.add(converter)
    }
}

这里是pom.xml去掉jackson

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </exclusion>
    </exclusions>
</dependency>

然后就可以开心的使用的fastjson了

参阅:

  1. Spring Boot使用Gson替换Jackson【http://blog.gelu.me/2017/spring-boot-uses-gson-to-replace-jackson/】
  2. SpringMVC中使用@RequestBody,@ResponseBody注解实现Java对象和XML/JSON数据自动转换(上)【http://blog.csdn.net/fw0124/article/details/48280083】
  3. spring boot(2)-@SpringBootApplication详解【http://blog.csdn.net/wangb_java/article/details/70943500?utm_source=itdadao&utm_medium=referral】

转载于:https://my.oschina.net/ElEGenT/blog/1531408

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值