天天看點

一知半解-springboot2.x內建Gson

  1. 官網原文:

    Auto-configuration for Gson is provided. When Gson is on the classpath a Gson bean is automatically configured. Several spring.gson.* configuration properties are provided for customizing the configuration. To take more control, one or more GsonBuilderCustomizer beans can be used.

    2.翻譯:

    預設提供了Gson的自動配置。當Gson位于類路徑中時,會自動裝配Gson bean。提供了spring.gson.* 的部配置設定置屬性用于自定義配置。要擷取更多的控制權限,可以使用 GsonBuilderCustomizer。

    3.pom(不需要排除Jackson相關依賴)

    <dependency>
         <groupId>com.google.code.gson</groupId>
         <artifactId>gson</artifactId>
         <version>${gson.version}</version>
     </dependency>
               

4.基本配置 (配置檔案)

一知半解-springboot2.x內建Gson

5.用類實作配置

package com.dog.it.config;
import com.google.gson.GsonBuilder;
import org.springframework.boot.autoconfigure.gson.GsonBuilderCustomizer;
import org.springframework.stereotype.Component;
@Component
public class GsonConfig implements GsonBuilderCustomizer {
    @Override
    public void customize(GsonBuilder gsonBuilder) {
        //設定項
        gsonBuilder.setDateFormat("yyyy-MM-dd HH:mm:ss");
        gsonBuilder.serializeNulls();
    }
}

           

繼續閱讀