個人使用比較習慣的json框架是fastjson,所以spring boot默認的jackjson使用起來就很陌生了,所以很自然我就想我能不能使用fastjson進行json解析呢?
引入fastjson依賴庫:
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.15</version>
</dependency>
這裏要說下很重要的話,官方文檔說的1.2.10以後,會有兩個方法支持HttpMessageconvert,一個是FastJsonHttpMessageConverter,支持4.2以下的版本,一個是FastJsonHttpMessageConverter4支持4.2以上的版本,具體有什麼區別暫時沒有深入研究。這裏也就是說:低版本的就不支持了,所以這裏最低要求就是1.2.10+。
配置fastjon
支持兩種方法:
第一種方法就是:
(1)啓動類繼承extends WebMvcConfigurerAdapter
(2)覆蓋方法configureMessageConverters
具體代碼如下:
/**
*
* @author Angel --守護天使
* @version v.0.1
* @date 2016年7月29日下午7:06:11
*/
@SpringBootApplication
public class ApiCoreApp extends WebMvcConfigurerAdapter {
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
super.configureMessageConverters(converters);
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(
SerializerFeature.PrettyFormat
);
fastConverter.setFastJsonConfig(fastJsonConfig);
converters.add(fastConverter);
}
}
第二種方法:
(1)在App.java啓動類中,注入Bean : HttpMessageConverters
具體代碼如下:
package com.kfit;
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;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
/**
*
* @author Angel --守護天使
* @version v.0.1
* @date 2016年7月29日下午7:06:11
*/
@SpringBootApplication
public class ApiCoreApp {
@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);
}
public static void main(String[] args) {
SpringApplication.run(ApiCoreApp.class, args);
}
}
那麼這時候在實體類中使用@JSONField(serialize=false),是不是此字段就不返回了,如果是的話,那麼恭喜你配置成功了,其中JSONField的包路徑是:com.alibaba.fastjson.annotation.JSONField。
我們從控制檯或是打印出來的json都看不出它到底使用了fastjson還是jackson來解析的,所我們在實體類在添加字段來區別它;
package zking.spring_boot_hello1;
import java.util.Date;
import com.alibaba.fastjson.annotation.JSONField;
/**
* 實體類
* @author Administrator
*
*/
public class Dome {
private Integer id;
private String name;
//這個註解是使用了com.alibaba.fastjson.annotation.JSONField
@JSONField(format="yyyy-MM-dd HH:mm")
private Date createTime;//時間
/**
* 在說一個註解就是
* serialize:是否需要序列化屬性.
* 註解之後不返回的該屬性的值,在項目中我們有些字段是不想返回,就可以用這個註解來實現
*/
@JSONField(serialize=false)
private String remarks;//備註
public Dome(){
}
public Dome(Integer id, String name) {
super();
this.id = id;
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public String getRemarks() {
return remarks;
}
public void setRemarks(String remarks) {
this.remarks = remarks;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
然後到controller給這個對象去賦值:
package zking.spring_boot_hello1;
import java.util.Date;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 在這裏我們使用RestController (等價於 @Controller 和 @RequestBody)
* @author Administrator
*
*/
@RestController
@RequestMapping("/demo")
public class HelloController {
@RequestMapping("/hello")
public String hello(){
return "hello";
}
/**
* 返回demo數據:
* 請求地址:http://127.0.0.1:8080/demo/getDemo
* @return
*/
@RequestMapping("/getDome")
public Dome getDome(){
Dome dome=new Dome();
dome.setId(1);
dome.setName("張三");
dome.setCreateTime(new Date());
dome.setRemarks("這是一個備註信息");
return dome;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
去App.class運行,效果如圖:
我們可以看到我們成功的使用fastjson解析好了,而且我們的備註信息也沒有。這說明我們解析成功了。