介绍:FastJson 是ailibaba 的一款解析Json的开源框架

使用方式1

引入jar 包

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

@SpringBootApplication
//启动类extends WebMvcConfigurerAdapter
public class App extends WebMvcConfigurerAdapter{
//重写configureMessageConverters
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
super.configureMessageConverters(converters); FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
FastJsonConfig fastJsonConfig = new FastJsonConfig();
//是否需要格式化
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
fastConverter.setFastJsonConfig(fastJsonConfig);
converters.add(fastConverter);
}
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}

使用方式2

@SpringBootApplication
public class App {
//使用@Bean 依赖注入
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters() {
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
fastConverter.setFastJsonConfig(fastJsonConfig);
HttpMessageConverter<?> converter = fastConverter;
return new HttpMessageConverters(converter);
}
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}

FastJson 其他用法:

格式化:

@JSONField(format = "yyyy-MM-dd")
 private Date createDate;

自定义名称:

@JSONField(name = "AGE")
  private int age;

将 Java 对象转换换为 JSON 对象:

JSON.toJSONString()

创建JSON 对象:

public void whenGenerateJson_thanGenerationCorrect() throws ParseException {
JSONArray jsonArray = new JSONArray();
for (int i = 0; i < 2; i++) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("AGE", 10);
jsonObject.put("FULL NAME", "Doe " + i);
jsonObject.put("DATE OF BIRTH", "2016/12/12 12:12:12");
jsonArray.add(jsonObject);
}
String jsonOutput = jsonArray.toJSONString();
}

JSON 对象需要转java对象:

public void whenJson_thanConvertToObjectCorrect() {
Person person = new Person(20, "John", "Doe", new Date());
String jsonObject = JSON.toJSONString(person);
Person newPerson = JSON.parseObject(jsonObject, Person.class)
}

Spring boot FastJson的更多相关文章

  1. Spring Boot fastJSON的使用

    springBoot,默认使用的json解析框架是Jackson. 虽然jackson能够满足json的解析,如果想使用熟悉的alibaba的fastjon,我们只需要在pom文件中配置maven依赖 ...

  2. 在spring boot环境中使用fastjson + redis的高速缓存技术

    因为项目需求,需要在spring boot环境中使用redis作数据缓存.之前的解决方案是参考的http://wiselyman.iteye.com/blog/2184884,具体使用的是Jackso ...

  3. spring boot 之fastJson的使用(二)

    昨天说了springboot的简单入门程序.今天进一步深入.今天说一下,fastJson的使用.做过springmvc的都知道fastjson.其实boot自带json可是本人用惯了fastjson, ...

  4. spring boot @ResponseBody转换JSON 时 Date 类型处理方法,Jackson和FastJson两种方式,springboot 2.0.9配置fastjson不生效官方解决办法

    spring boot @ResponseBody转换JSON 时 Date 类型处理方法 ,这里一共有两种不同解析方式(Jackson和FastJson两种方式,springboot我用的1.x的版 ...

  5. spring boot (二):使用fastJson解析json数据

    如果我们想在spring boot中使用第三方的json解析框架: 1)我们需要在pom.xml文件中引入第三方包的依赖; 2)实现方法: 方法1 需要在启动类中继承WebMvcConfigurerA ...

  6. spring boot配置使用fastjson

    一.前言 spring boot默认使用jackson来操作json数据,相比于jackson,fastjson更好用,功能也强大,所以这里记录一下在spring boot中配置使用fastjson的 ...

  7. Spring Boot返回json数据及完美使用FastJson解析Json数据

     Spring Boot返回json数据 视频地址:http://www.iqiyi.com/w_19rubxzsr5.html 博文参考:https://blog.csdn.net/linxingl ...

  8. FastJson/spring boot: json输出方法二

    1.引入FastJson依赖包 <!-- FastJson --> <dependency> <groupId>com.alibaba</groupId> ...

  9. FastJson/spring boot: json输出

    1.引入FastJson依赖包 <!-- FastJson --> <dependency> <groupId>com.alibaba</groupId> ...

随机推荐

  1. oracle创建表空间 授权

    --创建表空间 临时表空间 create temporary tablespace xiaodai_temp tempfile '/main/app/oracle/oradata/devdb/xiao ...

  2. 如何在win+r 或者是win10的应用搜索输入subl就能打开sublime

    这虽然不是什么技术贴,我实在不想开启sublime还要动鼠标,或者输入subl长长的全称,这里有两种做法: 第一种 在环境变量添加sublime安装目录的变量,一般sublime的安装目录会有subl ...

  3. 使用Bootstrap Popover实现一个弹框上三角形的代码记录

          $(function () {        var options = {          trigger: 'manual',          content: function ...

  4. Hadoop生态集群之HDFS

    一.HDFS是什么 HDFS是hadoop集群中的一个分布式的我文件存储系统.他将多台集群组建成一个集群,进行海量数据的存储.为超大数据集的应用处理带来了很多便利. 和其他的分布式文件存储系统相比他有 ...

  5. phpstudy安装redis

    php安装扩展,首先要在php官网下载相应的库文件, http://pecl.php.net/package/redis 下载相应版本的文件,首先phpinfo()看看当前的php环境版本等等   我 ...

  6. MySQL数据类型--与MySQL零距离接触2-12主键约束

    定义一个主键,可以用PRIMARY KEY,也可以用KEY. 主键约束的字段禁止为空. 写入4条记录,查看它的自动编号: 自动编号确实是1 2 3 4 AUTO_INCREMENT字段必须定义为主键, ...

  7. windows go dll 框架

    乘着还没有添加商业功能之前,先给大家把福利分享了 希望有需要的朋友能够用的上 这个框架是在用windows平台,GO做的http/https服务,调用dll现有的库接口实现特定功能的大框架 //dll ...

  8. color xml arm相关

    #-------------------------------------------------------------------------- # C O L O R S #--------- ...

  9. OBV15 案例5,上M10拉高出货

  10. python爬虫-基础入门-爬取整个网站《2》

    python爬虫-基础入门-爬取整个网站<2> 描述: 开场白已在<python爬虫-基础入门-爬取整个网站<1>>中描述过了,这里不在描述,只附上 python3 ...