Spring Boot fastJSON的使用


springBoot,默認使用的json解析框架是Jackson。

雖然jackson能夠滿足json的解析,如果想使用熟悉的alibaba的fastjon,我們只需要在pom文件中配置maven依賴就好。
<!-- fastjson依賴庫-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.15</version>
</dependency>
版本可根據自己需要查詢,網址:http://mvnrepository.com/
Maven依賴完成之后,我們可以通過兩種方式配置fastjon
方法一:啟動類繼承extends WebMvcConfigurerAdapter,且覆蓋configureMessageConverters。
方法二:在啟動類中,注入Bean:HttpMessageConverters。

代碼如下:
package org.lzm.springbootnew;
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.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.ArrayList;
import java.util.List;

@EnableScheduling //定時任務
@SpringBootApplication
public class SpringbootNewApplication extends WebMvcConfigurerAdapter {

  1. public static void main(String[] args) {
  2. SpringApplication.run(SpringbootNewApplication.class, args);
  3. }
  4. /*
  5. * // 方法一:extends WebMvcConfigurerAdapter
  6. */
  7. @Override
  8. public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
  9. super.configureMessageConverters(converters);
  10. //1、先定義一個convert轉換消息的對象
  11. FastJsonHttpMessageConverter fastConverter=new FastJsonHttpMessageConverter();
  12. //2、添加fastjson的配置信息,比如是否要格式化返回的json數據;
  13. FastJsonConfig fastJsonConfig=new FastJsonConfig();
  14. fastJsonConfig.setSerializerFeatures(
  15. //是否需要格式化
  16. SerializerFeature.PrettyFormat
  17. );
  18. //附加:處理中文亂碼(后期添加)
  19. List<MediaType> mediaTypeList=new ArrayList<MediaType>();
  20. mediaTypeList.add(MediaType.APPLICATION_JSON_UTF8);
  21. fastConverter.setSupportedMediaTypes(mediaTypeList);
  22. //3、在convert中添加配置信息
  23. fastConverter.setFastJsonConfig(fastJsonConfig);
  24. //4、將convert添加到converters
  25. converters.add(fastConverter);
  26. }
  27. /*
  28. * 方法二:在啟動類中,注入Bean:HttpMessageConverters
  29. */
  30. @Bean
  31. public HttpMessageConverters fastJsonHttpMessageConverters(){
  32. //1、先定義一個convert轉換消息的對象
  33. FastJsonHttpMessageConverter fastConverter=new FastJsonHttpMessageConverter();
  34. //2、添加fastjson的配置信息,比如是否要格式化返回的json數據;
  35. FastJsonConfig fastJsonConfig=new FastJsonConfig();
  36. fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
  37. //附加:處理中文亂碼
  38. List<MediaType> fastMedisTypes = new ArrayList<MediaType>();
  39. fastMedisTypes.add(MediaType.APPLICATION_JSON_UTF8);
  40. fastConverter.setSupportedMediaTypes(fastMedisTypes);
  41. //3、在convert中添加配置信息
  42. fastConverter.setFastJsonConfig(fastJsonConfig);
  43. HttpMessageConverter<?> converter=fastConverter;
  44. return new HttpMessageConverters(converter);
  45. }

}
其實代碼的核心是相同的,這是調取的方式不同而已。兩種方式都可以滿足我們對於fastjson的依賴使用。

下面使用@JSONField()注解在實體類中進行驗證
代碼如下。
@JSONField(format = “yyyy-MM-dd”)
private Date birthday;
Controller中代碼如下。
@RequestMapping(“/save”)
public Student save(){
Student student=new Student();
student.setName(“婷婷”);
student.setAge(23);
student.setSex(“女”);
student.setBirthday(new Date());
studentService.save(student);
return student;
}
瀏覽器返回結果:
{
“age”:23,
“birthday”:”2018-07-10”,
“id”:97,
“name”:”婷婷”,
“sex”:”女”
}
除此之外,我們還可以通過@JSONField(serialize = false)來決定字段的顯示與否。設置如下。
@JSONField(serialize = false)
private Date birthday;
如果這樣設置瀏覽器返回結果如下,birthday將不再顯示。
{
“age”:23,
“id”:97,
“name”:”婷婷”,
“sex”:”女”
}

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM