1.What:什么是SpringBoot?

SpringBoot是由Pivotal團(tuán)隊(duì)提供的全新框架,其設(shè)計(jì)目的是用來簡化新Spring應(yīng)用的初始搭建以及開發(fā)過程。該框架使用了特定的方式來進(jìn)行配置,從而使開發(fā)人員不再需要定義樣板化的配置。
SpringBoot特性
1.創(chuàng)建獨(dú)立的Spring應(yīng)用程序
2.嵌入的Tomcat,無需部署WAR文件
3.簡化Maven配置
4.自動(dòng)配置Spring
5.提供生產(chǎn)就緒型功能,如指標(biāo),健康檢查和外部配置
6.開箱即用,沒有代碼生成,也無需XML配置。
SpringBoot特性理解
?為基于Spring的開發(fā)提供更快的入門體驗(yàn)
?開箱即用,沒有代碼生成,也無需XML配置。同時(shí)也可以修改默認(rèn)值來滿足特定的需求。
?提供了一些大型項(xiàng)目中常見的非功能特性,如嵌入式服務(wù)器、安全、指標(biāo),健康檢測(cè)、外部配置等。
?SpringBoot并不是對(duì)Spring功能上的增強(qiáng),而是提供了一種快速使用Spring的方式。
2.Why+How:既然SpringBoot中已經(jīng)集成jackson我們?yōu)槭惨肍astJson?如何操作呢?
{"age":18,"name":"小明同學(xué)","createTime":"2018-07-29T04:48:08.524+0000"}
通過查看Libraries我們可以得知,SpringBoot默認(rèn)使用的jackson解析Json?,F(xiàn)在我們要使用FastJson對(duì)jacjson進(jìn)行替換。
<!--FastJson依賴-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.15</version>
</dependency>
<!--這里要說下很重要的話,官方文檔說的1.2.10以后,會(huì)有兩個(gè)方法支持HttpMessageconvert,一個(gè)是FastJsonHttpMessageConverter,支持4.2以下的版本,一個(gè)是FastJsonHttpMessageConverter4支持4.2以上的版本,具體有什么區(qū)別暫時(shí)沒有深入研究。
這里也就是說:低版本的就不支持了,所以這里最低要求就是1.2.10+ -->
配置fastjson(支持兩種辦法)
第一種:
extends WebMvcConfigurationSupport()
override configureMessageConverters
注意:如果不配置中文亂碼處理,顯示效果會(huì)是醬紫的
{ "age":18, "createTime":"2018-07-29 14:54", "name":"灝忔槑鍚屽" }
@SpringBootApplication
public class SpringbootdemoApplication extends WebMvcConfigurationSupport {
@Override
protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
super.configureMessageConverters(converters);
// 1、需要先定義一個(gè) convert 轉(zhuǎn)換消息的對(duì)象;
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
//2、添加fastJson 的配置信息,比如:是否要格式化返回的json數(shù)據(jù);
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
//**處理中文亂碼問題
List<MediaType> fastMediaTypes = new ArrayList<>();
fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
fastConverter.setSupportedMediaTypes(fastMediaTypes);
//3、在convert中添加配置信息.
fastConverter.setFastJsonConfig(fastJsonConfig);
//4、將convert添加到converters當(dāng)中.
converters.add(fastConverter);
}
public static void main(String[] args) {
SpringApplication.run(SpringbootdemoApplication.class, args);
}
}
@JSONField(format = "yyyy-MM-dd HH:mm")
private Date createTime;
<!-- 瀏覽器顯示的json 時(shí)間格式已更改-->
{
"age":18,
"createTime":"2018-07-29 14:46",
"name":"小明同學(xué)"
}
第二種:
使用 @Bean注入 fastJsonHttpMessageConvert
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters() {
// 1、需要先定義一個(gè) convert 轉(zhuǎn)換消息的對(duì)象;
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
//2、添加fastJson 的配置信息,比如:是否要格式化返回的json數(shù)據(jù);
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
//同樣要處理中文亂碼問題
List<MediaType> fastMediaTypes = new ArrayList<>();
fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
fastConverter.setSupportedMediaTypes(fastMediaTypes);
//3、在convert中添加配置信息.
fastConverter.setFastJsonConfig(fastJsonConfig);
HttpMessageConverter<?> converter = fastConverter;
return new HttpMessageConverters(converter);
}
所以為什么要換成fastjson呢?或許這就是愛吧~haha