SpringBoot整合fastJson

fastJson是一个java语言编写的高性能功能完善的json库,是阿里的开源项目,并且是目前java语言中最快的JSON库,接口简单易用。

一、pom.xml文件添加依赖
这里我添加的是fastJson最新版本,
版本可以去https://mvnrepository.com/artifact/com.alibaba/fastjson查询。

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.57</version>
</dependency>

二、具体使用
两种方式:
1.启动类继承WebMvcConfigurerAdapter并重写configureMessageConverters方法
2.启动类中bean注入HttpMessageConverters
这里我使用的是第一种。

VoiceSourceModel实体类:
package com.hearing.model;
import lombok.Data;
/**
 * 音频表
 * Created by liulin on 2019/2/8.
 */
@Data
public class VoiceSourceModel {
    /**
     * id
     */
    private int id;
    /**
     *  音频名称
     */
    private String voiceName;
    /**
     * 音频url
     */
    private String voiceUrl;
    /**
     * 音频类型 1-mp3, 2-wma, 3-wave
     */
    private int voiceType;
}
控制类IndexController:
@RestController
@RequestMapping("/index")
public class IndexController {

    @GetMapping("test_fastjson")
    public String testFastJson(HttpServletRequest request) {
        VoiceSourceModel voiceSourceModel = new VoiceSourceModel();
        voiceSourceModel.setId(1);
        voiceSourceModel.setVoiceName("生命树");
        voiceSourceModel.setVoiceUrl("www.baidu.com");
        voiceSourceModel.setVoiceType(3);
        return JSON.toJSONString(voiceSourceModel);
    }

}
1.启动类继承WebMvcConfigurerAdapter并重写configureMessageConverters方法
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class HearingTestApplication extends WebMvcConfigurerAdapter {

    @Override
    public void configureMessageConverters (List<HttpMessageConverter<?>> converters) {
        // 定义一个converter转换消息的对象
        FastJsonHttpMessageConverter fastconverter = new FastJsonHttpMessageConverter();
        // 添加fastjson的配置信息,比如:是否需要格式化返回的json数据
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
        // 在converter中添加配置信息
        fastconverter.setFastJsonConfig(fastJsonConfig);
        // 将converter添加到converters中
        converters.add(fastconverter);
    }

    public static void main(String[] args) {
        SpringApplication.run(HearingTestApplication.class, args);
    }
}

右键Run As -> Java Application,启动应用,浏览器输入http://localhost:6060/index/test_fastjson访问看到如下所示表示成功。

{
    "id": 1,
    "voiceName": "生命树",
    "voiceType": 3,
    "voiceUrl": "www.baidu.com"
}
2.启动类中bean注入HttpMessageConverters
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class HearingTestApplication {
   @Bean
   public HttpMessageConverters fastJsonHttpMessageConverters() {
       // 定义一个converter转换消息的对象
      FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
      // 添加fastjson的配置信息,比如:是否需要格式化返回的json数据
      FastJsonConfig fastJsonConfig = new FastJsonConfig();
      fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
      // 在converter中添加配置信息
      fastConverter.setFastJsonConfig(fastJsonConfig);
      // 将converter赋值给HttpMessageConverter
     HttpMessageConverter<?> converter = fastConverter;
      // 返回HttpMessageConverters对象
      return new HttpMessageConverters(fastConverter);
   }
   public static void main(String[] args) {
      SpringApplication.run(HearingTestApplication.class, args);
   }
}

启动后效果如下:

"{\"id\":1,\"voiceName\":\"生命树\",\"voiceType\":3,\"voiceUrl\":\"www.baidu.com\"}"

不知道为什么不是树形结构。就效果而言,我更喜欢第一种,所以采用了第一种配置。

说明:
使用第二种配置时,启动后json遇到中文乱码,对此解决的方法是在GetMapping中添加了produces = “application/json; charset=utf-8″,该方法同样可以解决第一种配置遇到的中文乱码,如下所示:

@GetMapping(value = "test_fastjson", produces = "application/json;charset=UTF-8”)

同时,我还了解到:Springboot2.0以后,WebMvcConfigurerAdapter过时了,原因是Springboot2.0最低支持JDK1.8,引入了新的default关键字,该关键字配置在interface接口的方法时子类可以不去实现该方法,相当于抽象类内已经实现的接口方法。
新的代替方法是:直接实现WebMvcConfigurer或者实现WebMvcConfigurationSupport。
经调试确认无误,也因此我将第一种修改为如下所示。
最终版:

@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class HearingTestApplication implements WebMvcConfigurer {

    @Override
    public void configureMessageConverters (List<HttpMessageConverter<?>> converters) {
        // 定义一个converter转换消息的对象
        FastJsonHttpMessageConverter fastconverter = new FastJsonHttpMessageConverter();
        // 添加fastjson的配置信息,比如:是否需要格式化返回的json数据
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
        // 在converter中添加配置信息
        fastconverter.setFastJsonConfig(fastJsonConfig);
        // 将converter添加到converters中
        converters.add(fastconverter);
    }

    public static void main(String[] args) {
        SpringApplication.run(HearingTestApplication.class, args);
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容