Kotlin Springboot Jackson Google Gson。 Jacshon仍然使用

发布于 2025-01-18 14:01:50 字数 1601 浏览 0 评论 0原文

我知道这个话题已经提出了几个问题,但是为什么杰克逊仍然被用来序列化和挑选JSON,而我将其排除在任何地方:在Springboot应用程序中,在build.gradle.kts中,并在<<代码> application.properties

当我“崩溃”此HTTP请求以进行测试驱动目的时,我可以看到崩溃是由于Jackson Converter

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

那么我该如何完全排除Jackson?

不在依赖项中

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")
}

首选是在application.properties

spring.jpa.open-in-view=false
server.port=7070
spring.http.converters.preferred-json-mapper=gson

中设置为gson

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

I know several issues have been raised on this topic, but why Jackson is still used to serialize and deserialize JSON whereas I've excluded it from everywhere : in the springboot application, in build.gradle.kts and set the preferred Json serializer in application.properties

When I "crash" this HTTP request for test driven purpose, I can see that the crash is due to jackson converter

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
        }
    }
}

error is :

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

So how the hell can I totally exclude Jackson ?

Not in dependencies

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 is set to GSon in application.properties

spring.jpa.open-in-view=false
server.port=7070
spring.http.converters.preferred-json-mapper=gson

exclude in main application

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你列表最软的妹 2025-01-25 14:01:50

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

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

查看依赖项树以查看哪些依赖项将其拉入并将其从每个依赖项中排除。

You need to exclude the transitive dependency like this:

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

Look at your dependency tree to see which dependencies are pulling it in and exclude it from each.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文