1、到入jar包
<!-- 添加fastjson 依賴包. --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.15</version> </dependency>
2、在啟動程序main方法中加入
@Bean public HttpMessageConverters fastHttpMessageConverters() { // 1、需要先定義一個 convert 轉換消息的對象; FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter(); // 2、添加fastJson 的配置信息,比如:是否要格式化返回的json數據; FastJsonConfig fastJsonConfig = new FastJsonConfig(); fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); fastJsonConfig.setCharset(Charset.forName("UTF8")); // 3、在convert中添加配置信息. fastConverter.setFastJsonConfig(fastJsonConfig); HttpMessageConverter<?> converter = fastConverter; return new HttpMessageConverters(converter); }
3、測試例子
@JSONField(format="yyyy-MM-dd HH:mm") 主要是處理時間 private Date createTime;
其他:
有的時候會碰到亂碼的,在controller類的action方法中解決亂碼問題
@RequestMapping(value="modelJson",produces = "text/html;charset=UTF-8")


