springboot默認自帶json解析框架,默認使用jackson,若是使用fastjson,能夠按照下列方式配置使用java
<dependencies> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.15</version> </dependency> </dependencies>
配置方式一(經過繼承的方式)
一、啓動類繼承WebMvcConfigurerAdapter
二、重寫configureMessageConverters方法web
@Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { super.configureMessageConverters(converters); /* * 一、須要先定義一個convert轉換消息的對象 * 二、添加fastJson的配置信息,好比:是否要格式化返回json數據 * 三、在convert中添加配置信息 * 四、將convert添加到converters當中 * */ //一、須要先定義一個·convert轉換消息的對象; FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter(); //二、添加fastjson的配置信息,好比 是否要格式化返回json數據 FastJsonConfig fastJsonConfig = new FastJsonConfig(); fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); //三、在convert中添加配置信息. fastConverter.setFastJsonConfig(fastJsonConfig); //四、將convert添加到converters當中. converters.add(fastConverter); }
配置方式二(經過@Bean注入的方式)spring
@Bean public HttpMessageConverters fastJsonHttpMessageConverters() { // 一、須要先定義一個 convert 轉換消息的對象; FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter(); //二、添加fastJson 的配置信息,好比:是否要格式化返回的json數據; FastJsonConfig fastJsonConfig = new FastJsonConfig(); fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); //三、在convert中添加配置信息. fastConverter.setFastJsonConfig(fastJsonConfig); HttpMessageConverter<?> converter = fastConverter; return new HttpMessageConverters(converter); }
準備一個Controllerjson
import java.util.Date; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import com.xujie.pojo.Demo; @RestController // 包括了@ResponseBody和Controller public class UserController { @GetMapping("/test") public Demo test() { Demo demo = new Demo(); demo.setId(1); demo.setCreateTime(new Date()); demo.setName("demo22"); demo.setRemarks("這裏是備註信息,不該該返回"); return demo; } }
啓動類寫法瀏覽器
import java.util.List; 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 com.alibaba.fastjson.serializer.SerializerFeature; import com.alibaba.fastjson.support.config.FastJsonConfig; import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter; @SpringBootApplication public class UserApplication extends WebMvcConfigurerAdapter { public static void main(String[] args) { SpringApplication.run(UserApplication.class, args); } @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { //一、須要先定義一個·convert轉換消息的對象; FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter(); //二、添加fastjson的配置信息,好比 是否要格式化返回json數據 FastJsonConfig fastJsonConfig = new FastJsonConfig(); fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); //三、在convert中添加配置信息. fastConverter.setFastJsonConfig(fastJsonConfig); //四、將convert添加到converters當中. converters.add(fastConverter); } }
pojo類springboot
import java.util.Date; import com.alibaba.fastjson.annotation.JSONField; public class Demo { private int 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 String getRemarks() { return remarks; } public void setRemarks(String remarks) { this.remarks = remarks; } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } 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; } }
瀏覽器測試app
輸入地址:http://localhost:8080/test框架
返回:
{ "createTime":"2018-02-23 12:06", "id":1, "name":"demo22" }
ide