加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

使用fastjson (demo)

发布时间:2020-12-16 19:47:02 所属栏目:百科 来源:网络整理
导读:加入maven 依赖 dependencygroupIdcom.alibaba/groupIdartifactIdfastjson/artifactIdversion1.2.45/version/dependency 创建HttpMessageConverters ,这里是无XML方式创建 import org.springframework.boot.autoconfigure.web.HttpMessageConverters;import

加入maven 依赖

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

创建HttpMessageConverters ,这里是无XML方式创建
import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

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

@Configuration
public class BeanConfig {

	@Bean
	public HttpMessageConverters fastJsonHttpMessageConverters() {
		FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
		FastJsonConfig config = new FastJsonConfig();
		config.setSerializerFeatures(SerializerFeature.PrettyFormat);
		config.setSerializerFeatures();
		converter.setFastJsonConfig(config);
		converter.setDefaultCharset(Charset.forName("UTF-8")); // 设置编码
		return new HttpMessageConverters(converter);
	}
}
如果传输Bean 对象 可以在字段上使用 @JSONField 注解来制定返回json 对象

@JSONField(format="yyyy-MM-dd HH:mm:ss")
private Date createTime;

@JSONField(serialize=false)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读