gradle 由于 jackson 依赖问题,运行时依赖导致测试失败

gradle runtime dependency makes test fail because of jackson dependencies issue

在 Wildfly 15 上的 JEE 项目 运行ning 上,我试图将日志配置为 json 格式。复制我为最近的一些 Spring 引导项目所做的数十次,我将 net.logstash.logback:logstash-logback-encoder:6.6 添加为 runtimeOnly 依赖项,因为我计划有一个 logback.xml 配置文件.

在构建我的应用程序和 运行 测试时,添加此依赖项会使某些测试失败:某些控制器无法再序列化 LocalDateTime。

Caused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Java 8 date/time type `java.time.LocalDateTime` not supported by default: add Module "com.fasterxml.jackson.datatype:jackson-datatype-jsr310" to enable handling (through reference chain: de.gefa.tankkarte.common.port.adapter.restapi.RestServiceErrorResponseEntity["timestamp"])
    at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:77)
    at com.fasterxml.jackson.databind.SerializerProvider.reportBadDefinition(SerializerProvider.java:1276)
    at com.fasterxml.jackson.databind.ser.impl.UnsupportedTypeSerializer.serialize(UnsupportedTypeSerializer.java:35)
    at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:728)
    at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:770)
    at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:178)
    at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider._serialize(DefaultSerializerProvider.java:480)
    at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:319)
    at com.fasterxml.jackson.databind.ObjectWriter$Prefetch.serialize(ObjectWriter.java:1514)
    at com.fasterxml.jackson.databind.ObjectWriter.writeValue(ObjectWriter.java:1006)
    at org.jboss.resteasy.plugins.providers.jackson.ResteasyJackson2Provider.writeTo(ResteasyJackson2Provider.java:328)
    at org.jboss.resteasy.core.interception.AbstractWriterInterceptorContext.writeTo(AbstractWriterInterceptorContext.java:137) 

我不明白为什么应该用于日志记录的依赖项会干扰 REST 响应的构建方式...和 ​​jackson-datatype-jsr310 IS 的一部分类路径。当我 运行 依赖项任务时,我得到这个:

runtimeClasspath - Runtime classpath of source set 'main'.
...
+--- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.10.4 -> 2.12.0
|    +--- com.fasterxml.jackson.core:jackson-annotations:2.12.0
|    |    \--- com.fasterxml.jackson:jackson-bom:2.12.0
|    |         +--- com.fasterxml.jackson.core:jackson-annotations:2.12.0 (c)
|    |         +--- com.fasterxml.jackson.core:jackson-core:2.12.0 (c)
|    |         +--- com.fasterxml.jackson.core:jackson-databind:2.12.0 (c)
|    |         \--- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.12.0 (c)
|    +--- com.fasterxml.jackson.core:jackson-core:2.12.0
|    |    \--- com.fasterxml.jackson:jackson-bom:2.12.0 (*)
|    +--- com.fasterxml.jackson.core:jackson-databind:2.12.0
|    |    +--- com.fasterxml.jackson.core:jackson-annotations:2.12.0 (*)
|    |    +--- com.fasterxml.jackson.core:jackson-core:2.12.0 (*)
|    |    \--- com.fasterxml.jackson:jackson-bom:2.12.0 (*)
|    \--- com.fasterxml.jackson:jackson-bom:2.12.0 (*)
...
+--- net.logstash.logback:logstash-logback-encoder:6.6
|    \--- com.fasterxml.jackson.core:jackson-databind:2.12.0 (*)

我真的不明白这种冲突是如何以及为什么...

我想我不明白原因,但在尝试了不同的事情之后,只有将版本从 6.6 降级到 6.4 才有帮助。现在依赖关系看起来像这样:

runtimeClasspath - Runtime classpath of source set 'main'.
...
+--- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.10.4
|    +--- com.fasterxml.jackson.core:jackson-annotations:2.10.4 -> 2.11.0
|    +--- com.fasterxml.jackson.core:jackson-core:2.10.4 -> 2.11.0
|    \--- com.fasterxml.jackson.core:jackson-databind:2.10.4 -> 2.11.0
|         +--- com.fasterxml.jackson.core:jackson-annotations:2.11.0
|         \--- com.fasterxml.jackson.core:jackson-core:2.11.0
...
+--- net.logstash.logback:logstash-logback-encoder:6.4
|    \--- com.fasterxml.jackson.core:jackson-databind:2.11.0 (*)
     

我不明白为什么在那种情况下 jackson-datatype-jsr310:2.10.4 没有自动升级到 2.11.0,而它之前已升级到 2.12.0。

目前还可以,但将来有点可怕:下次有人尝试升级此应用程序的版本时,很可能会出现一些问题..

如果我排除传递依赖项似乎也有效 - 那么,我什至可以使用最新的 logback-encoder 版本:

runtimeOnly ('net.logstash.logback:logstash-logback-encoder:7.0.1') {
    exclude group: 'com.fasterxml.jackson.core', module: 'jackson-databind'
}

因此,如果有人了解正在发生的事情以及如何使其在未来更加可靠,请不要犹豫,提出建议!