1. 程式人生 > >【Spring-boot 】FastJson對json資料進行解析(型別轉換)

【Spring-boot 】FastJson對json資料進行解析(型別轉換)

序列化

序列化就是指 把JavaBean物件轉成JSON格式的字串。

將Map轉成JSON

將List<Map>轉成JSON。

自定義JavaBean User轉成JSON。

String objJson = JSON.toJSONString(Object object);

FastJSON可以直接對日期型別格式化,在預設的情況下,FastJSON會將Date轉成long。

FastJSON將java.util.Date轉成long。

 String dateJson = JSON.toJSONString(new Date());
    
 System.out.println(dateJson);      //1401370199040
String dateJson = JSON.toJSONString(new Date(), SerializerFeature.WriteDateUseDateFormat); //指定輸出日期格式。
System.out.println(dateJson);  //"2014-05-29 21:36:24"
String listJson = JSON.toJSONString(list, SerializerFeature.UseSingleQuotes);//使用單引號
String listJson = JSON.toJSONString(list, true);  //顯示的樣子格式化
String listJson = JSON.toJSONString(list, SerializerFeature.PrettyFormat);
//顯示的樣子格式化


我們使用SpringBoot會直接將輸出的資料轉換成json格式。

但是我們需要將指定的資料轉換成特定的格式呢?比如講日期格式的轉換顯示。

主要有兩種方式:

第一種:Spring boot的啟動類繼承  WebMvcConfigurerAdapter 重寫configureMessageConverters方法

第二種:同樣是繼承WebMvcConfigurerAdapter 類,自己定義轉換的方法,通過@Bean註解進行注入

(來操作一下)

步驟一:建立maven專案,在pom.xml中新增fastjson依賴

          <!-- 引入fastjson依賴 -->
		<dependency>
		    <groupId>com.alibaba </groupId>
		    <artifactId>fastjson</artifactId>
		    <version>1.2.15</version>
		</dependency>

步驟二:第一種方式(Spring boot的啟動類繼承  WebMvcConfigurerAdapter 重寫configureMessageConverters方法)

@SpringBootApplication
@ComponentScan("com.springboot")
@MapperScan("com.springboot.*") //掃描mybatis的配置資訊
public class App extends WebMvcConfigurerAdapter{
  
	
	public static void main(String[] args) {
	 SpringApplication.run(App.class, args);
  }
   
   @Override
	public void configureMessageConverters( List<HttpMessageConverter<?>> converters) {
		super.configureMessageConverters(converters);
//1.需要定義一個convert轉換訊息的物件
		FastJsonHttpMessageConverter fastConverter=new FastJsonHttpMessageConverter();
		//2.新增fashJson的配置訊息,比如:是否要格式化返回的json資料
		FastJsonConfig fastJsonConfig=new FastJsonConfig();
		//3.在convert中新增配置資訊
		fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
		//4.將convert新增到converters當中
		fastConverter.setFastJsonConfig(fastJsonConfig);
		converters.add(fastConverter);
	}

第二種方式(繼承WebMvcConfigurerAdapter 類,自己定義轉換的方法,通過@Bean註解進行注入)

@SpringBootApplication
@ComponentScan("com.springboot")
@MapperScan("com.springboot.*") //掃描mybatis的配置資訊
public class App extends WebMvcConfigurerAdapter{
  
	
	public static void main(String[] args) {
	 SpringApplication.run(App.class, args);
  }

      @Bean
     public HttpMessageConverters fastJsonHttpMessageConverters(){
    	//1.需要定義一個convert轉換訊息的物件
    	 FastJsonHttpMessageConverter fastConverter=new FastJsonHttpMessageConverter();
    	//2.新增fashJson的配置訊息
    	 FastJsonConfig fastJsonConfig=new FastJsonConfig();
    	 fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
    	 
    	//3.在convert中新增配置資訊
    	 fastConverter.setFastJsonConfig(fastJsonConfig);
    	 
    	 HttpMessageConverter<?> converter=fastConverter;
    	 return new HttpMessageConverters(converter);
    	 
     }
}

步驟三:直接在我們需要轉換的資料的DTO類中新增@JSONField註解進行格式化


相關推薦

Spring-boot FastJsonjson資料進行解析型別轉換

序列化序列化就是指 把JavaBean物件轉成JSON格式的字串。將Map轉成JSON將List<Map>轉成JSON。自定義JavaBean User轉成JSON。String objJson = JSON.toJSONString(Object object)

Spring-boot使用filterrequest body引數進行校驗

@Slf4j public class ParameterCheckServletRequestWrapper extends HttpServletRequestWrapper { private byte[] requestBody; pr

spring BootSpring中@Controller和@RestController之間的區別

處理 public 不同 esp 舉例 rest control tro adding spring Boot入手的第一天,看到例子中的@RestController ............. 相同點:都是用來表示Spring某個類的是否可以接收HTTP請求 不同點:@C

spring Boot1.創建第一個springBoot項目

web項目 conf star pen sin ocs frame parent mave 入手springBoot,搭建第一個springBoot項目。 看官方文檔還是有點別扭。 https://docs.spring.io/spring-boot/docs/curren

spring Boot2.在Myecplise上把spring Boot項目打包 war包和jar包

aps let failed htm 報錯 聲明 執行 spa oss ========================================================第一部分=========================================

spring boot3.spring boot項目,綁定資源文件為bean並使用

display fig 屬性綁定 factor pin none rand actor tag 整個例子的結構目錄如下: 1.自定義一個資源文件 com.sxd.name = 申九日木 com.sxd.secret = ${random.value} com.sx

spring Bootspring boot獲取資源文件的三種方式兩種情況下

不用 ash 12c ica pla bsp foreach set 使用 首先聲明一點,springboot獲取資源文件,需要看是   1》從spring boot默認的application.properties資源文件中獲取   2》還是從自定義的資源文件中獲取

Spring Boot 2、Spring Boot CLI

pac sam nbsp ref tps 項目 gin 1.8 com 1.配置Spring的環境變量在環境變量Path 添加: D:\Program Files\spring-boot-cli-1.5.8.RELEASE-bin\spring-1.5.8.RELEASE\

spring boot配置文件 application.properties 屬性解析

date hiberna mage ida str 數據丟失 art rop 就會 1.JPA命名策略 spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.DefaultNamingStrategy 有兩種值

Spring boot第一個項目 Springboot + mysql + hibernate

tle jpa serializa pla factor sta ant plain hibernate 今天公司要做一個小項目,好久沒碰項目了(刷題好累。。。),聽說spring boot很火,決定試一試。暫時就從mysql裏面讀數據好了,使用hiberant

springspring mvcspring boot獲取spring cloud項目中所有spring mvc的請求資源

sea ams other figure upd ring false 調用 tom 實現的方法: 1.在父級項目中 或者 每個微服務都引用的項目中添加實體類Resource 2.在父級項目中 或者 每個為服務都引用的項目中寫一個工具類,作用是用來獲取請求資源 3.在每一個

Spring Boot19Spring Boot嵌入式Servlet容器自動配置原理

    其中EmbeddedServletContainerAutoConfiguration是嵌入式Servlet容器的自動配置類,該類在spring-boot-autoconfigure-xxx.jar中的web模組可以找到。 @AutoConfig

Spring Boot18Spring Boot配置嵌入式Servlet容器

Spring Boot預設使用Tomcat作為嵌入式的Servlet容器,只要引入了spring-boot-start-web依賴,則預設是用Tomcat作為Servlet容器: 1、定製和修改Servlet容器的相關配置 1)、修改和server有關的配置(ServerProper

Spring Boot15Spring Boot錯誤處理機制

1、Spring Boot預設的錯誤處理機制 如果是瀏覽器,則返回一個預設的錯誤頁面: 如果是其他測試工具,如Postman,則返回一個json資料: 原理: ​ 可以參照ErrorMvcAutoConfiguration,錯誤處理的自動配置

Spring Boot24Spring Boot中使用快取之Spring快取

1、快取依賴 只要新增如下依賴,即可使用快取功能。 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter

Spring Boot23Spring Boot整合Mybatis

首先新增mybatis依賴: <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</

Spring Boot22Spring Boot啟動配置原理

啟動配置原理 重要的事件回撥機制: ApplicationContextInitializer SpringApplicationRunListener ApplicationRunner CommandLineRunner前兩者需要配置在META-INF/spring.f

Spring Boot21Spring Boot使用外接的Servlet容器

嵌入式Servlet容器: ​ 優點:簡單,便攜; ​ 缺點:預設不支援jsp,優化定製比較複雜; 使用外接Servlet容器的步驟: ​ 1)、必須建立一個war專案,需要建立好web專案的目錄結構,特別是webapp/WEB-INF/web.xml; ​ 2)、嵌入式的To

Spring Boot31、使用SpringBoot傳送mail郵件

1、前言 傳送郵件應該是網站的必備拓展功能之一,註冊驗證,忘記密碼或者是給使用者傳送營銷資訊。正常我們會用JavaMail相關api來寫傳送郵件的相關程式碼,但現在springboot提供了一套更簡易使用的封裝。   2、Mail依賴 <dependency>

Spring Boot30、SpringBoot整合RabbitMQ

1、安裝 1.1、Erlang: Erlang下載地址,下載後安裝即可。 1.2、RabbitMQ安裝 RabbitMQ下載地址,下載後安裝即可。 注意:Erlang的版本要與RabbitMQ版本需要匹配才行。 RabbitMQ Mini