Spring Boot配置使用FastJson解析JSON数据

SpringBoot集成FastJson教程
本文详细介绍如何在SpringBoot项目中替换默认的JSON处理器Jackson,使用FastJson进行JSON数据处理。文章提供了两种配置方法,一种是在启动类中注入Bean:HttpMessageConverters,另一种是通过继承WebMvcConfigurerAdapter并覆盖configureMessageConverters方法。最后通过一个简单的示例验证了配置的有效性。

本人使用FastJson比较熟悉,SpringBoot使用JSON不习惯,需要替换成FastJson。

 

通过观看https://my.oschina.net/sdlvzg/blog/1154281创建项目,再执行以下操作

 

引入依赖库:

        

		<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
		<!-- 改变spring boot 默认的json解析,通过引入fastjson改变 -->
		<!-- fastjson版本必须 大于1.2.10 -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>fastjson</artifactId>
			<version>1.2.23</version>
		</dependency>

 

配置FastJson

有两种方法:

 

方法一:在App.java启动类中,注入Bean : HttpMessageConverters


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

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

/**
 * Spring Boot启动类
 * 
 * @author Administrator
 *
 */
@SpringBootApplication // 等价于@Configuration,@EnableAutoConfiguration和@ComponentScan
public class Application {
	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}

	//引入Fastjson解析json,不使用默认的jackson
	//必须在pom.xml引入fastjson的jar包,并且版必须大于1.2.10
	@Bean
	public HttpMessageConverters fastJsonHttpMessageConverters() {
		//1、定义一个convert转换消息的对象
		FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
		//2、添加fastjson的配置信息
		FastJsonConfig fastJsonConfig = new FastJsonConfig();
		fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
		//3、在convert中添加配置信息
		fastConverter.setFastJsonConfig(fastJsonConfig);
		//4、将convert添加到converters中
		HttpMessageConverter<?> converter = fastConverter;
		return new HttpMessageConverters(converter);
	}
}

方法二:启动类继承extends WebMvcConfigurerAdapter,覆盖方法configureMessageConverters

import java.util.List;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
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;

/**
 * Spring Boot启动类
 * 
 * @author Administrator
 *
 */
@SpringBootApplication // 等价于@Configuration,@EnableAutoConfiguration和@ComponentScan
public class Application extends WebMvcConfigurerAdapter {
	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}
	// 引入Fastjson解析json,不使用默认的jackson
	// 必须在pom.xml引入fastjson的jar包,并且版必须大于1.2.10
	public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
		super.configureMessageConverters(converters);
        //1、定义一个convert转换消息的对象
		FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
        //2、添加fastjson的配置信息
		FastJsonConfig fastJsonConfig = new FastJsonConfig();
		fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
        //3、在convert中添加配置信息
		fastConverter.setFastJsonConfig(fastJsonConfig);
        //4、将convert添加到converters中
		converters.add(fastConverter);
	}
}

验证是否生效

创建实体:


import java.util.Date;

import com.alibaba.fastjson.annotation.JSONField;

public class Demo {
	private long id;// 主键.
	private String name;// 测试名称.
	
	@JSONField(serialize=false)			//转为JSON时不包括此属性
	private String remark;	//备注
	
	@JSONField(format="yyyy-MM-dd HH:mm")   //转为JSON时按格式转换
	private Date createTime;

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}
	
}

创建Controller


import java.util.Date;

import org.lvgang.bean.Demo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/demo")
public class DemoController {
   
    /**
     * 返回demo数据:
     * 请求地址:http://127.0.0.1:8080/demo/getDemo
     * @return
     */
    @RequestMapping("/getDemo")
    public Demo getDemo(){
       Demo demo = new Demo();
       demo.setId(1);
       demo.setName("Angel");
       demo.setCreateTime(new Date());
       demo.setRemark("remark");
       return demo;
    }
   
}

通过前台访问http://127.0.0.1:8080/demo/getDemo,返回的结果为:

{ "createTime":"2017-07-04 10:07", "id":1, "name":"Angel" }    表示配置完成

转载于:https://my.oschina.net/sdlvzg/blog/1153921

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值