SpringBoot で gson使用

Jackson も、Google gsonspring-boot-starter-json で利用するための構成があります。
SpringBoot で、gson だけに使用を絞りたい時はどうすればいいのでしょう。

まずは、Gsonを使うための設定
for Gradle build.gradle

dependencies {
    compile group: 'com.google.code.gson', name: 'gson', version: '2.8.5'
}

for Maven pom.xml

<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.8.6</version>
</dependency>

application.properties

# aaa
spring.http.converters.preferred-json-mapper=gson

その他、application.properties に設定できる属性

# Format to use when serializing Date objects.
spring.gson.date-format= yyyy-MM-dd HH:mm:ss
 
# Whether to disable the escaping of HTML characters such as '<', '>', etc.
spring.gson.disable-html-escaping= true
 
# 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= 

これで、Autowireも使えるわけです。

@Autowire
private Gson gson;

Jasckson 使用を除外するためには以下が必要です
project dependencies 除外

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-json</artifactId>
        </exclusion>
    </exclusions>
</dependency>

SpringBootApplication.java

@SpringBootApplication(exclude = {JacksonAutoConfiguration.class})
public class SpringBootApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(SpringBootApplication.class, args);
    }
}