SpringBoot-01SpringBoot配置FastJson

一. SpringBoot自带JSON解析工具

 通过观察SpringBoot自动引入的jar包可以发现, SpringBoot其实自身已经携带JSON解析工具:Jackson. 如果不想使用Jackson而使用FastJson的话, 需要在SpringBoot中配置FastJson.

二. 步骤

1. 在pom.xml中引入FastJson依赖库

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

2. 配置FastJson(两种方式)

方式一:
 ① SpringBoot启动类继承WebMvcConfigurerAdapter
 ② 重写configureMessageConverters()方法

package online.bendou.springboot.fastjsondemo;

import java.util.List;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;

/**
 * SpringBoot的启动类, 使用@SpringBootApplication修饰
 * @author Lx
 * 
 * 继承WebMvcConfigurerAdapter, 重写
 *
 */
@SpringBootApplication
public class Application extends WebMvcConfigurerAdapter{

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        super.configureMessageConverters(converters);

        /*
         * 1. 需要先定义一个convert转换器对象 
         * 2. 配置添加fastjson的配置信息, 比如: 是否要格式化返回的json数据;
         * 3. 把配置信息添加到convert转换器对象中;
         * 4. 解决中文乱码
         * 5. 将convert添加到转换器对象当中;
         */
        // 1.
        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
        // 2.
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
        // 3.
        fastConverter.setFastJsonConfig(fastJsonConfig);
        //4. 解决中文乱码问题 
        List<MediaType> fastMediaTypes = new ArrayList<MediaType>();
        fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
        fastConverter.setSupportedMediaTypes(fastMediaTypes);
        // 5.
        converters.add(fastConverter);
    };

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

}

 ③ 在实体类中添加FastJson注解校验配置的FastJson是否生效

package online.bendou.springboot.fastjsondemo.entity;

import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.annotation.JSONField;

@Component
@Entity(name="user_info")
public class User {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Integer id;
    private String name;

    // 出生日期
    @JSONField(format="yyyy-MM-dd")
    private Date birthday;

    get/set...  
}

 ④ 编写Contoller

package online.bendou.springboot.fastjsondemo.controller;

import java.util.Date;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import online.bendou.springboot.fastjsondemo.entity.User;


@RestController
public class FastJsonController {

    @GetMapping("/user")
    public Object findAll(){
        User user = new User();
        user.setId(1);
        user.setName("张三");
        user.setBirthday(new Date());
        return user;
    }

}

 ⑤ 运行启动类, 浏览器访问测试

方式二:
  在Application.java启动类中注入Bean: HttpMessageConverters

package online.bendou.springboot.fastjsondemo;

import java.util.ArrayList;
import java.util.List;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;

/**
 * 配置FastJson的方式二:
 *      在启动类中注入HttpMessageConverters
 * @author Lx
 *
 */
@RestController
public class Application {

    @Bean
    public HttpMessageConverters fastJsonHttpMessageConverters(){
        // 1.创建一个converter对象
        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
        // 2.创建配置对象
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        // 3.添加配置
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
        // 4.将配置添加到转换器对象中
        fastConverter.setFastJsonConfig(fastJsonConfig);
        // 5. 解决中文乱码问题
        List<MediaType> mediaTypes = new ArrayList<MediaType>();
        mediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
        fastConverter.setSupportedMediaTypes(mediaTypes);
        // 6.将转换器对象转化为HttpMessageConverter对象
        HttpMessageConverter<?> converter = fastConverter;
        return new HttpMessageConverters(converter);
    }

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

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值