springboot2はデフォルトのjsonパーサーJacksonをfastjsonに変更します

  FastjsonはAliによって作成されています。fasjsonは近年いくつかの深刻な脆弱性を露呈していますが、公平を期すために、特にデータ量が多い場合、fastjsonのパフォーマンスは確かに非常に有利であるため、fastjsonが依然として最初の選択肢です。SpringBootのデフォルトjsonパーサーはJacksonであり、fastjsonに置き換える必要があります。

springboot2はデフォルトのjsonパーサーJacksonをfastjsonに変更しますspringboot2はデフォルトのjsonパーサーJacksonをfastjsonに変更します

1.交換方法

1.1。依存関係を導入します。[注意、1.2.61未満の重大なリスクの高い脆弱性があります。1.2.61の修復。1.2.61にアップグレードする必要があります。最新バージョンは1.2.62です]

        <dependency> 
            <groupid> com.alibaba </ groupid> 
            <artifactid> fastjsonlt; / artifactid> 
            <version> 1.2.62lt; / version> 
        </ dependency>

1.2、構成

注:Springboot 2.0以降、WebMvcConfigurerAdapterは廃止されました。以前のバージョン1からWebMvcConfigurerAdapterを継承する方法はお勧めしません。以下に2つの構成方法を紹介します。WebMvcConfigurationSupportを実装する別の方法があります。数万の道路があり、1つのオプションで十分です。

方法1(推奨):デフォルトのパーサーをBeanに置き換えます

パッケージcom.anson.config; 

インポートcom.alibaba.fastjson.serializer.SerializerFeature; 
インポートcom.alibaba.fastjson.support.config.FastJsonConfig; 
インポートcom.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter; 
import org.springframework.boot.autoconfigure.http.HttpMessageConverters; 
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.http.MediaType; 
import org.springframework.http.converter.HttpMessageConverter; 

インポートjava.nio.charset.Charset; 
import java.util.ArrayList; 
インポートjava.util.List; 

@Configuration 
public class WebConfig { 
 
    / **
     * @Author anson
     * @ Descriptionメッセージコンバーターを構成します
     * @ Date:2019-12-8 11:23:33 
     * @version:1.0 
     * new HttpMessageConverters(true、converters); 
     *置き換えるにはtrueに設定する必要があります。そうしないと、置き換えられません
     * @returnメッセージ変換Beanを返します
     * / 
    @Bean 
    public HttpMessageConverters fastJsonMessageConverters(){ 
        List> Converters = new ArrayList <>(); 
        //メッセージを変換するオブジェクトを定義する必要があります; 
        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter(); 
        / / fastJson構成情報を追加します; 
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setDateFormat( "yyyy-MM-dd HH:mm:ss"); 
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
        //グローバル時間構成
        fastJsonConfig.setCharset(Charset.forName( "UTF-8")); 
        //中国語の文字化け文字
        リストの処理fastMediaTypes = new ArrayList <>(); 
        fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8); 
        //変換中構成情報を追加します
        。fastConverter.setSupportedMediaTypes(fastMediaTypes); 
        fastConverter.setFastJsonConfig(fastJsonConfig); 

        Converters.add(0、fastConverter); 
        return new HttpMessageConverters(converters); 
    } 
}

方法2、WebMvcConfigurerを実装する

@Configuration
パブリッククラスWebConfigureはWebMvcConfigurerを実装します{ 
 
    / ***
     メッセージコンバーターを構成します* @ param
     コンバーター
     * / 
    @Override 
    public void configureMessageConverters(List> Converters){ 
       //メッセージを変換するオブジェクトを定義する必要があります; 
        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter( ); 
        // fastJson構成情報を追加します; 
        FastJsonConfig fastJsonConfig = new FastJsonConfig(); 
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); 
        //グローバル時間構成
        fastJsonConfig.setDateFormat( "yyyy-MM-dd HH:mm:ss"); 
        fastJsonConfig .setCharset(Charset.forName( "UTF-8")); 
        / /中国語の文字化けした
        リストの問題を処理するfastMediaTypes = new ArrayList <>();
        fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8); 
        //在変換中追加配置信息。
        fastConverter.setSupportedMediaTypes(fastMediaTypes); 
        fastConverter.setFastJsonConfig(fastJsonConfig); 
        Converters.add(0、fastConverter); 
    } 
}

おすすめ

転載: blog.csdn.net/yaxuan88521/article/details/114776727