SpringBoot 集成fastJson分析

虾米哥 阅读:182 2021-03-31 21:10:52 评论:0

第一步:添加fastjson 依赖:

 

<fastjson.version>1.2.47</fastjson.version>
        <!-- json 依赖 --> 
		<dependency> 
			<groupId>com.alibaba</groupId> 
			<artifactId>fastjson</artifactId> 
			<version>${fastjson.version}</version> 
		</dependency>

第二步:如果返回对象是json 对象,需要添加一个转换器(org.springframework.boot.autoconfigure.HttpMessageConverters):

1、springboot 注解bean 方式:

 
import com.alibaba.fastjson.serializer.SerializerFeature; 
import com.alibaba.fastjson.support.config.FastJsonConfig; 
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter; 
import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
import org.springframework.boot.autoconfigure.http.HttpMessageConverters; 
import org.springframework.context.annotation.Bean; 
import org.springframework.http.converter.HttpMessageConverter; 
import tk.mybatis.spring.annotation.MapperScan; 
 
 
@SpringBootApplication 
@MapperScan("com.zzg.springboot.mapper") 
public class Application{ 
    public static void main(String[] args) { 
        SpringApplication.run(Application.class, args); 
    } 
    /** 
     * 覆盖方法configureMessageConverters,使用fastJson 
     * @return 
     */ 
    @Bean 
    public HttpMessageConverters fastJsonHttpMessageConverters() { 
        //1、定义一个convert转换消息的对象 
        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter(); 
        //2、添加fastjson的配置信息 
        FastJsonConfig fastJsonConfig = new FastJsonConfig(); 
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); 
        //3、在convert中添加配置信息 
        fastConverter.setFastJsonConfig(fastJsonConfig); 
        //4、将convert添加到converters中 
        HttpMessageConverter<?> converter = fastConverter; 
        return new HttpMessageConverters(converter); 
    } 
 
}

2、继承org.springframework.web.servlet.config.annotation.WebMvcConfigurer 接口,并且覆写configureMessageConverters()方法。

package com.zzg.springboot; 
 
import com.alibaba.fastjson.serializer.SerializerFeature; 
import com.alibaba.fastjson.support.config.FastJsonConfig; 
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter; 
import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
import org.springframework.http.converter.HttpMessageConverter; 
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; 
import tk.mybatis.spring.annotation.MapperScan; 
 
import java.util.List; 
 
@SpringBootApplication 
@MapperScan("com.zzg.springboot.mapper") 
public class Application implements WebMvcConfigurer { 
    public static void main(String[] args) { 
        SpringApplication.run(Application.class, args); 
    } 
 
    @Override 
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { 
        //1、定义一个convert转换消息的对象 
        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter(); 
        //2、添加fastjson的配置信息 
        FastJsonConfig fastJsonConfig = new FastJsonConfig(); 
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); 
        //3、在convert中添加配置信息 
        fastConverter.setFastJsonConfig(fastJsonConfig); 
        //4、将convert添加到converters中 
        converters.add(fastConverter); 
    } 
}

 

声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

发表评论
搜索
排行榜
关注我们

一个IT知识分享的公众号