首页 > 解决方案 > Elastic4S - Jackson 的 ScalaObjectMapper 类未找到并抛出 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:无法初始化类 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 !")
    }
  }

任何帮助,将不胜感激

标签: scalajackson-databindelastic4s

解决方案


似乎还有一些其他的依赖关系涵盖了这一点。例如,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


推荐阅读