springboot 默认使用的 jackson 但是听说某宝的fastjson 性能很好,而且平时用的习惯,所以来整合一下。
首先在pom 中导入依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.15 </version>
</dependency>
```
导入后当然还需要配置一下啦下面介绍一下配置的方式,以及简单的使用fastjson。
第一种创建一个fastjson的配置类继承WebMvcConfigurerAdapter 将fastjson配置添加到Converters
@Configuration
public class FaJsonConfig extends WebMvcConfigurerAdapter{
public void configureMessageConverters(List> converters) {
/**1 预先定义一个Converters 转换消息的对象
* 2 添加fastjson的配置信息
* 3 早converter中添加配置信息
* 4.将converter添加到converters中
*/
super.configureMessageConverters(converters);//
FastJsonHttpMessageConverter fastConstructor=new FastJsonHttpMessageConverter();//1
FastJsonConfig fastJsonConfig=new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);//2
fastConstructor.setFastJsonConfig(fastJsonConfig);//3
converters.add(fastConstructor);//4
}
}
//或者声明成一个bean 代码如下
@Configuration
public class FaJsonConfig extends WebMvcConfigurerAdapter{
/*public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
*//**1 预先定义一个Converters 转换消息的对象
* 2 添加fastjson的配置信息
* 3 早converter中添加配置信息
* 4.将converter添加到converters中
*//*
super.configureMessageConverters(converters);//
FastJsonHttpMessageConverter fastConstructor=new FastJsonHttpMessageConverter();//1
FastJsonConfig fastJsonConfig=new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);//2
fastConstructor.setFastJsonConfig(fastJsonConfig);//3
converters.add(fastConstructor);//4
}*/
@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);
}
}
那么这时候在实体类中使用@JSONField(serialize=false),是不是此字段就不返回了,如果是的话,那么恭喜你配置成功了。
下面创建两个类简要说下fastjson的简单使用
usermodel
public class User{
private int id;
@JSONField(format = "yyyy-MM-dd HH:mm:ss")
private Date createTime;//创建时间
@JSONField(serialize = false)
private String name;
@JSONField(deserialize = false)
private int age;
@JSONField(name = "list")
private List<Address> addrList;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public List<Address> getAddrList() {
return addrList;
}
public void setAddrList(List<Address> addrList) {
this.addrList = addrList;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
}
address model
public class Address{
/*
* 省份
*/
private String province;
/*
* 城市
*/
private String city;
/*
* 地区
*/
private String area;
/*
* 具体详细地址
*/
private String detail;
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getArea() {
return area;
}
public void setaArea(String area) {
this.area = area;
}
public String getDetail() {
return detail;
}
public void setDetail(String detail) {
this.detail = detail;
}
}
准备一个测试用的json字符串
{“addrList”:[{“city”:”某市”,”detail”:”某大学”,”area”:”某区”,”province”:”某省”},{“city”:”某某市”,”detail”:”某某大学”,”area”:”某某区”,”province”:”某某省”}],”age”:22,”id”:1,”name”:”cxhc”}
1、反序列化
public static void main( String[] args )
{
SpringApplication.run(App.class,args);
System.out.println( "Hello World" );
String jsonString="{'addrList':[{'city':'某市','detail':'某大学','area':'某区','province':'某省'},{'city':'某某市','detail':'某某大学','area':'某某区','province':'某某省'}],'age':22,'id':1,'name':'cxhc'}";
User user = JSON.parseObject(jsonString, User.class);
System.out.println(user);
}
得到输出结果:
User [id=1, name=cxhc, age=22, addrList=[Address [province=某省, city=某市, area=null, detail=某大学], Address [province=某某省, city=某某市, area=null, detail=某某大学]]]
2、序列化
public static void main( String[] args )
{
SpringApplication.run(App.class,args);
System.out.println( "Hello World" );
User user=new User();
user.setName("cxhc");
user.setCreateTime(new Date());
user.setAge(11);
user.setId(1);
List <Address> list=new ArrayList<Address>();
Address address=new Address();
address.setaArea("某区");
address.setCity("某市");
address.setDetail("某大学");
address.setProvince("某省");
list.add(address);
address.setaArea("某某区");
address.setCity("某某市");
address.setDetail("某某大学");
address.setProvince("某某省");
list.add(address);
user.setAddrList(list);
System.out.println(JSON.toJSONString(user));
}
//得到如下结果
{“addrList”:[{“city”:”某市”,”detail”:”某大学”,”province”:”某省”},{“city”:”某某市”,”detail”:”某某大学”,”province”:”某某省”}],”age”:22,”id”:1,”name”:”cxhc”}
3、JSONField
package com.alibaba.fastjson.annotation;
public @interface JSONField {
// 配置序列化和反序列化的顺序,1.1.42版本之后才支持
int ordinal() default 0;
// 指定字段的名称
String name() default “”;
// 指定字段的格式,对日期格式有用
String format() default “”;
// 是否序列化
boolean serialize() default true;
// 是否反序列化
boolean deserialize() default true;
}
3.1 指定字段序列化名称
例如
@JSONField(name = "list")
private List<Address> addrList;
3.2格式化
@JSONField(format = "yyyy-MM-dd HH:mm:ss")
private Date createTime;//创建时间
3.3 序列化的时候不被序列化
@JSONField(serialize = false)
3.4 反序列化的时候 不被反序列化
@JSONField(deserialize = false)
文章地址:http://www.haha174.top/article/details/254138
源码地址:https://github.com/haha174/boot.git