Search code examples
scalajackson-databindelastic4s

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


I'm having an issue while using Elastic4S in a Scala project. The following error is thrown :

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

Followed by :

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

Here are the dependencies used :

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

And the only bit of code launched from Elastic4s is in this method :

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

Any help would be appreciated


Solution

  • There seem to be some other dependencies that are covering this one. For eg, the swagger-akka-http uses another version of jackson-scala-module and can't be included along without some tweaking in the build.sbt. Here would be the configuration to put in such case :

    ("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")),
    

    see more about sbt dependencies' exclusion rules here : https://www.scala-sbt.org/release/docs/Library-Management.html#Exclude+Transitive+Dependencies