kotlin spring boot jackson google gson。杰克逊还在用

kotlin springboot jackson google gson. Jacshon still used

我知道关于这个主题已经提出了几个问题,但是为什么 Jackson 仍然被用来序列化和反序列化 JSON 而我已经从任何地方排除它:在 springboot 应用程序中,在 build.gradle.kts 并在 application.properties

中设置首选 Json 序列化程序

当我出于测试驱动目的“崩溃”此 HTTP 请求时,我可以看到崩溃是由于 jackson 转换器

class IronMaidenSourceApi {
    companion object {
        fun fetchIronMaidenSource(): ResponseEntity<DTOIronMaidenAPi> {
            val uri = "http://localhost:3004/ironmaiden"
            return RestTemplate().getForEntity(uri, DTOIronMaidenAPi::class.java) // <-- JACKSON still used here
        }
    }
}

错误是:

at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType

那我怎么能完全排除杰克逊呢?

不在依赖关系中

dependencies {
    implementation("org.springframework.boot:spring-boot-starter-data-jpa")
    implementation("org.springframework.boot:spring-boot-starter-web")
    implementation("org.jetbrains.kotlin:kotlin-reflect")
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
    developmentOnly("org.springframework.boot:spring-boot-devtools")
    runtimeOnly("org.postgresql:postgresql")
    testImplementation("org.springframework.boot:spring-boot-starter-test")
    implementation ("com.google.code.gson:gson:2.9.0")
}

preferred 在application.properties

中设置为GSon
spring.jpa.open-in-view=false
server.port=7070
spring.http.converters.preferred-json-mapper=gson

在主应用程序中排除

@SpringBootApplication(exclude = [JacksonAutoConfiguration::class])

你需要像这样排除传递依赖:

dependencies {
implementation('commons-beanutils:commons-beanutils:1.9.4') {
    exclude group: 'commons-collections', module: 'commons-collections'
}

查看您的依赖关系树,了解哪些依赖关系将其拉入并将其从每个依赖关系中排除。