springboot使用fastson

目錄

 

springboot1.x

springboot2.x


springboot1.x

@Configuration

public class WebAppMvcConfig extends WebMvcConfigurerAdapter {

    @Override

    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {

        super.configureMessageConverters(converters);

        /*

         * 1、需要先定義一個convert轉換消息的對象 2、添加fastJson的配置信息,比如:是否要格式化返回json數據

         * 3、在convert中添加配置信息 4、將convert添加到converters當中

         */

        // 1、需要先定義一個·convert轉換消息的對象;

        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();

        // 2、添加fastjson的配置信息,比如 是否要格式化返回json數據

        FastJsonConfig fastJsonConfig = new FastJsonConfig();

        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);

        // 3、在convert中添加配置信息.

        fastConverter.setFastJsonConfig(fastJsonConfig);

        // 4、將convert添加到converters當中.

        converters.add(fastConverter);

    }

}

@Data

public class BaseBean implements Serializable {

    private Long    id;

    private String  isDeleted;

    private String  creator;

    private String  modifier;

    @JSONField(format = "yyyy-MM-dd HH:mm:ss")

    private Date    gmtCreated;

    @JSONField(format = "yyyy-MM-dd HH:mm:ss")

    private Date    gmtModified;

}
 

 

這樣就可以實現 前後交互護時候,代碼減少很多了。

這裏會自動轉換form時間類型的。

 

springboot2.x

@Configuration

public class WebMvcConfig implements WebMvcConfigurer {

 

    /**

     * JSON 轉換

     */

    @Override

    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {

       /*

        * 1、需要先定義一個convert轉換消息的對象 2、添加fastJson的配置信息,比如:是否要格式化返回json數據 3、在convert中添加配置信息

        * 4、將convert添加到converters當中

        */

       // 1、需要先定義一個·convert轉換消息的對象;

       FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();

       // 2、添加fastjson的配置信息,比如 是否要格式化返回json數據

       FastJsonConfig fastJsonConfig = new FastJsonConfig();

       fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);

       // 3、在convert中添加配置信息.

       fastConverter.setFastJsonConfig(fastJsonConfig);

       // 4、將convert添加到converters當中.

       converters.add(fastConverter);

      

       WebMvcConfigurer.super.extendMessageConverters(converters);

    }

   

}

這樣後,發現不行。根本用的不是fastjson。配置不起作用。

debug發現:我們自己的fastjson配置在最下面,最上面的是jackson.那麼想辦法調整下順序把。

 

調整代碼後,加一行代碼:converters.add(0, fastConverter);

@Configuration

public class WebMvcConfig implements WebMvcConfigurer {

 

    /**

     * JSON 轉換

     */

    @Override

    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {

       /*

        * 1、需要先定義一個convert轉換消息的對象 2、添加fastJson的配置信息,比如:是否要格式化返回json數據 3、在convert中添加配置信息

        * 4、將convert添加到converters當中

        */

       // 1、需要先定義一個·convert轉換消息的對象;

       FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();

       // 2、添加fastjson的配置信息,比如 是否要格式化返回json數據

       FastJsonConfig fastJsonConfig = new FastJsonConfig();

       fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);

       // 3、在convert中添加配置信息.

       fastConverter.setFastJsonConfig(fastJsonConfig);

       // 4、將convert添加到converters當中.

       //converters.add(fastConverter);

       converters.add(0, fastConverter);

       WebMvcConfigurer.super.extendMessageConverters(converters);

    }

   

}

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章