1. 程式人生 > >SpringBoot 整合fastJson

SpringBoot 整合fastJson

第一步:新增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);
    }
}