<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # Spring Boot 2 與 Gson > 原文: [https://howtodoinjava.com/spring-boot2/gson-with-spring-boot/](https://howtodoinjava.com/spring-boot2/gson-with-spring-boot/) 了解將 **[Gson](https://howtodoinjava.com/gson/gson/)** 配置為 Spring Boot 應用程序作為首選 json 映射器。 默認情況下,Spring Boot 為此使用 Jackson。 ## 1\. 包括 Gson 依賴 通過添加適當的[ Gson 依賴項](https://howtodoinjava.com/gson/gson-installation-maven-gradle-jar/)在 Spring Boot 應用程序中包含 gson。 `build.gradle` ```java dependencies { compile group: 'com.google.code.gson', name: 'gson', version: '2.8.5' } ``` `pom.xml` ```java <dependencies> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.5</version> </dependency> </dependencies> ``` ## 2\. Gson 自動配置 通過包含依賴關系,Spring Boot 可以檢測到對類路徑的 Gson 依賴關系,并創建一個具有合理默認值的`Gson` bean。 #### 2.1. 默認 Gson 實例 Spring Boot 會檢測到`Gson.class`的存在,并使用 [GsonAutoConfiguration](https://github.com/spring-projects/spring-boot/blob/master/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/gson/GsonAutoConfiguration.java) 來配置 Gson 實例。 如果我們需要直接訪問`Gson`實例,則可以直接在 spring bean 中自動裝配: `Autowire Gson instance` ```java @Autowire private Gson gson; ``` #### 2.2. 自定義 Gson 實例 要自定義 Gson 實例的默認行為,請從下面的列表中配置相應的屬性。 `GsonAutoConfiguration`在初始化`Gson`實例時使用這些屬性。 `application.properties` ```java # Format to use when serializing Date objects. spring.gson.date-format= # Whether to disable the escaping of HTML characters such as '<', '>', etc. spring.gson.disable-html-escaping= # Whether to exclude inner classes during serialization. spring.gson.disable-inner-class-serialization= # Whether to enable serialization of complex map keys (i.e. non-primitives). spring.gson.enable-complex-map-key-serialization= # Whether to exclude all fields from consideration for serialization or deserialization that do not have the "Expose" annotation. spring.gson.exclude-fields-without-expose-annotation= # Naming policy that should be applied to an object's field during serialization and deserialization. spring.gson.field-naming-policy= # Whether to generate non executable JSON by prefixing the output with some special text. spring.gson.generate-non-executable-json= # Whether to be lenient about parsing JSON that doesn't conform to RFC 4627. spring.gson.lenient= # Serialization policy for Long and long types. spring.gson.long-serialization-policy= # Whether to output serialized JSON that fits in a page for pretty printing. spring.gson.pretty-printing= # Whether to serialize null fields. spring.gson.serialize-nulls= ``` ## 3\. 使 Gson 成為首選 json 映射器 要將 Spring Boot 配置為使用`Gson`作為首選 Jackson 映射器,請在`application.properties`文件中使用此屬性。 `application.properties` ```java spring.http.converters.preferred-json-mapper=gson ``` 現在,即使 Jackson 在類路徑中可用,Spring 運行也會使用 Gson 來[序列化和反序列化 JSON 載荷](https://howtodoinjava.com/gson/gson-serialize-deserialize-json/)。 ## 4\. 完全排除 Jackson 如果我們想將 Jackson 完全排除在應用程序運行時之外,可以通過排除`spring-boot-starter-json`來實現。 #### 4.1. 從項目依賴項中排除 Jackson ```java <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <!-- Exclude the default Jackson dependency --> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-json</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.5</version> </dependency> </dependencies> ``` #### 4.2. 禁用自動配置 如果我們只想在 SpringBoot 配置中排除 Jackson,可以通過禁用[自動配置](https://howtodoinjava.com/spring-boot/springbootapplication-auto-configuration/)類`JacksonAutoConfiguration`來排除它。 `SpringBootApplication.java` ```java @SpringBootApplication(exclude = {JacksonAutoConfiguration.class}) public class SpringBootApplication { public static void main(String[] args) { SpringApplication.run(SpringBootApplication.class, args); } } ``` 請問您有關**使用 Spring Boot 2** 配置 Gson 的問題。 學習愉快!
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看