SpringBoot拾级而上·整合 Fastjson

一、添加Fastjson依赖

build.gradle文件dependencies中添加
最新版本可从这里获取
https://mvnrepository.com/artifact/com.alibaba/fastjson

compile group: 'com.alibaba', name: 'fastjson', version: '1.2.47'

二、创建一个配置管理类 WebConfig

package com.springboot.springboot_web.model;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import 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.converter.HttpMessageConverter;

@Configuration
public class WebConfig {

    @Bean
    public HttpMessageConverters fastJsonHttpMessageConverters() {
        FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();

        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);

        fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);

        HttpMessageConverter<?> converter = fastJsonHttpMessageConverter;

        return new HttpMessageConverters(converter);

    }
}

三、测试类

1、创建一个实体类 User

public class User {

    private Integer id;
    private String username;
    private String password;
    private Date birthday;

    省略getter 和 setter方法
}

2、创建控制器类 FastjsonController

package com.springboot.springboot_web.control;

import com.springboot.springboot_web.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

@Controller
@RequestMapping("/fastjson")
public class FastjsonController {
    @RequestMapping("/test")
    @ResponseBody
    public User testJson() {
        User user = new User();

        user.setId(1);
        user.setUsername("jack");
        user.setPassword("jack123");
        user.setBirthday(new Date());

        return user;
    }
}

此时,打开浏览器,访问http://localhost:8081/fastjson/test,返回结果

{ "birthday":1533091223682, "id":1, "password":"jack123", "username":"jack" }

此时,还不能看出 Fastjson 是否正常工作,修改 User 类中使用 Fastjson 的注解,如下内容

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

再次访问http://localhost:8081/fastjson/test,返回结果
日期格式与我们修改的内容格式一致,说明 Fastjson 整合成功。

{ "birthday":"2018-08-01", "id":1, "password":"jack123", "username":"jack" }
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,991评论 19 139
  • Nginx 的配置文件使用的就是一门微型的编程语言,许多真实世界里的 Nginx 配置文件其实就是一个一个的小程序...
    SkTj阅读 4,256评论 0 7
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,971评论 6 342
  • 江南以南 雨柔人断肠 有个花圈 挂在墙上 色彩斑斓 我站在朝西的阳台上 眺望雨水中 再也回不去的故乡 冬日尸骨未寒...
    沙泥文森阅读 182评论 0 3
  • 今天读到这样一句话,是作家连岳写的:“要学会浪费,你的孩子玩水、不关灯、把蛋糕抹在脸上,不要再吼他们,这几块钱能换...
    黑的白的黑阅读 294评论 0 1