Spring Boot 完美支持阿里里巴巴 FastJSON:
- 第一種實(shí)現(xiàn)方式
- Application 繼承 WebMvcConfigurerAdapter愤诱;
- 重寫 configureMessageConverters 方法;
- 完整代碼示例:
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
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 java.util.List;
@SpringBootApplication
public class DemoApplication extends WebMvcConfigurerAdapter {
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
super.configureMessageConverters(converters);
// 1. 定義一個(gè) converter 轉(zhuǎn)換消息的對(duì)象居兆;
FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
// 2. 添加 FastJSON 的配置信息吁讨,比如是否要格式化返回的 JSON 數(shù)據(jù);
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
// 3. 在 converter 中添加配置配置信息;
fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
// 4. 將 converter 添加到 converters 當(dāng)中除抛;
converters.add(fastJsonHttpMessageConverter);
}
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
- 第二種實(shí)現(xiàn)方式
- 直接使用
@Bean
注入第三方 JSON 解析框架:
- 完整代碼:
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
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;
@SpringBootApplication
public class DemoApplication {
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters() {
// 1. 定義一個(gè) converter 轉(zhuǎn)換消息的對(duì)象;
FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
// 2. 添加 FastJSON 的配置信息母截,比如是否要格式化返回的 JSON 數(shù)據(jù)到忽;
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
// 3. 在 converter 中添加配置配置信息;
fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
HttpMessageConverter<?> converter = fastJsonHttpMessageConverter;
return new HttpMessageConverters(converter);
}
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
-
@com.alibaba.fastjson.annotation.JSONField
注解
// 格式化返回的數(shù)據(jù)
@JSONField(format = "yy/MM/dd HH:mm")
private Date createDate;
// 是否序列化微酬,默認(rèn)為 true绘趋,則可以返回到前端
@JSONField(serialize = false)
private String remarks;
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者