使用fastjson
fastjson是阿里巴巴的一个开源JSON解析框架,是目前JSON解析速度最快的开源框架,该框架也可以集成到Spring Boot中。
不同于Gson,fastjson继承完成之后并不能立马使用,需要开发者提供相应的HttpMessageConverter后才能使用,集成fastjson的步骤如下。
首先除去jackson-databind依赖,引入fastjson依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
然后配置fastjson的HttpMessageConverter:
MyFastJsonConfig.java
package com.shrimpking.config;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.nio.charset.StandardCharsets;
/**
* Created by IntelliJ IDEA.
*
* @Author : Shrimpking
* @create 2023/6/4 12:24
*/
@Configuration
public class MyFastJsonConfig
{
@Bean
FastJsonHttpMessageConverter fastJsonHttpMessageConverter(){
FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
FastJsonConfig config = new FastJsonConfig();
config.setDateFormat("yyyy-MM-dd");
config.setCharset(StandardCharsets.UTF_8);
config.setSerializerFeatures(
SerializerFeature.WriteClassName,
SerializerFeature.WriteMapNullValue,
SerializerFeature.PrettyFormat,
SerializerFeature.WriteNullListAsEmpty,
SerializerFeature.WriteNullStringAsEmpty
);
converter.setFastJsonConfig(config);
return converter;
}
}
代码解释:
• 自定义MyFastJsonConfig,完成对FastJsonHttpMessageConverterBean的提供。
• 第7~15行分别配置了JSON解析过程的一些细节,例如日期格式、数据编码、是否在生成的JSON中输出类名、是否输出value为null的数据、生成的JSON格式化、空集合输出[]而非null、空字符串输出""而非null等基本配置。MyFastJsonConfig配置完成后,还需要配置一下响应编码,否则返回的JSON中文会乱码,在application.properties中添加如下配置:
server.port=8099
#MyFastJsonConfig
server.servlet.encoding.force-response=true
Book.java
package com.shrimpking;
import java.util.Date;
/**
* Created by IntelliJ IDEA.
*
* @Author : Shrimpking
* @create 2023/6/4 12:31
*/
public class Book
{
private String name;
private String author;
private Float price;
private Date publicationDate;
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getAuthor()
{
return author;
}
public void setAuthor(String author)
{
this.author = author;
}
public Float getPrice()
{
return price;
}
public void setPrice(Float price)
{
this.price = price;
}
public Date getPublicationDate()
{
return publicationDate;
}
public void setPublicationDate(Date publicationDate)
{
this.publicationDate = publicationDate;
}
@Override
public String toString()
{
return "Book{" + "name='" + name + '\'' + ", author='" + author + '\'' + ", price=" + price + ", publicationDate=" + publicationDate + '}';
}
}
BookController.java
package com.shrimpking;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
/**
* Created by IntelliJ IDEA.
*
* @Author : Shrimpking
* @create 2023/6/4 12:33
*/
@RestController
public class BookController
{
@GetMapping("/fastjsonbook")
public Book test()
{
Book book = new Book();
book.setName("fastjson");
book.setAuthor("alibaba");
book.setPrice(9999999f);
book.setPublicationDate(new Date());
return book;
}
}
对于FastJsonHttpMessageConverter的配置,除了上面这种方式之外,还有另一种方式。
在Spring Boot项目中,当开发者引入spring-boot-starter-web依赖之后,该依赖又依赖了spring-boot-autoconfigure,在这个自动化配置中,有一个WebMvcAutoConfiguration类提供了对Spring MVC最基本的配置,如果某一项自动化配置不满足开发需求,开发者可以针对该项自定义配置,只需要实现WebMvcConfigurer接口即可(在Spring 5.0之前是通过继承WebMvcConfigurerAdapter类来实现的),代码如下:
package com.shrimpking.config;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.nio.charset.StandardCharsets;
import java.util.List;
/**
* Created by IntelliJ IDEA.
*
* @Author : Shrimpking
* @create 2023/6/4 12:39
*/
@Configuration
public class MyWebMvcConfig implements WebMvcConfigurer
{
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters)
{
FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
FastJsonConfig config = new FastJsonConfig();
config.setDateFormat("yyyy-MM-dd");
config.setCharset(StandardCharsets.UTF_8);
config.setSerializerFeatures(
SerializerFeature.WriteClassName,
SerializerFeature.WriteMapNullValue,
SerializerFeature.PrettyFormat,
SerializerFeature.WriteNullListAsEmpty,
SerializerFeature.WriteNullStringAsEmpty
);
converter.setFastJsonConfig(config);
converters.add(converter);
}
/*
在这个自动化配置中,
有一个WebMvcAutoConfiguration类提供了对Spring MVC最基本的配置,
如果某一项自动化配置不满足开发需求,
开发者可以针对该项自定义配置,
只需要实现WebMvcConfigurer接口即可
(在Spring 5.0之前是通过继承WebMvcConfigurerAdapter类来实现的),
代码如下:
*/
}