Kotlin Springboot Jackson Google Gson。 Jacshon仍然使用
我知道这个话题已经提出了几个问题,但是为什么杰克逊仍然被用来序列化和挑选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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要像这样排除传递依赖项:
查看依赖项树以查看哪些依赖项将其拉入并将其从每个依赖项中排除。
You need to exclude the transitive dependency like this:
Look at your dependency tree to see which dependencies are pulling it in and exclude it from each.