Elastic4S - 未找到 Jackson 的 class ScalaObjectMapper 并抛出 NoSuchMethodError

Elastic4S - Jackson's class ScalaObjectMapper isn't found and throws a NoSuchMethodError

我在 Scala 项目中使用 Elastic4S 时遇到问题。抛出以下错误:

java.lang.NoSuchMethodError: com.fasterxml.jackson.module.scala.experimental.ScalaObjectMapper.$init$(Lcom/fasterxml/jackson/module/scala/experimental/ScalaObjectMapper;)V

其次是:

java.lang.NoClassDefFoundError: Could not initialize class com.sksamuel.elastic4s.json.JacksonSupport$

以下是使用的依赖项:

  "com.sksamuel.elastic4s"       %% "elastic4s-core"              % "6.7.3",
  "com.sksamuel.elastic4s"       %% "elastic4s-http"              % "6.7.3",
  "com.github.swagger-akka-http" %% "swagger-akka-http"           % "2.0.4",
  "com.github.swagger-akka-http" %% "swagger-scala-module"        % "2.0.5",
  ...
  ),
  assemblyMergeStrategy in assembly := { _ => MergeStrategy.first }

从 Elastic4s 启动的唯一代码是在这个方法中:

  def testClusterUp(log: LoggingAdapter): Unit = {
    val response: Response[NodeInfoResponse] = client.execute(
      nodeInfo()
    ).await

    if (response.isError) {
      log.error(s"[ERROR]-[ELASTICSEARCH] $response")
      throw new ExceptionInInitializerError(s"an error occurred during Elastic connector initialization : ${response.error}")
    } else if (response.isSuccess) {
      log.info("Cluster started successfully !")
    }
  }

如有任何帮助,我们将不胜感激

似乎还有一些其他的依赖关系覆盖了这个。例如,swagger-akka-http 使用了另一个版本的 jackson-scala-module,如果不在 build.sbt 中进行一些调整,就无法将其包含在内。这是在这种情况下的配置:

("com.github.swagger-akka-http" %% "swagger-akka-http"    % "2.0.4") excludeAll(ExclusionRule(organization = "com.fasterxml.jackson.module")),
("com.github.swagger-akka-http" %% "swagger-scala-module" % "2.0.5") excludeAll(ExclusionRule(organization = "com.fasterxml.jackson.module")),

在此处查看更多关于 sbt 依赖项排除规则的信息: https://www.scala-sbt.org/release/docs/Library-Management.html#Exclude+Transitive+Dependencies