Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Could not initialize class io.swagger.converter.ModelConverters #18

Open
aclowkey opened this issue Dec 11, 2023 · 1 comment
Open

Could not initialize class io.swagger.converter.ModelConverters #18

aclowkey opened this issue Dec 11, 2023 · 1 comment

Comments

@aclowkey
Copy link

Hi,

i'm trying to use swagger-play, but reached this exception when adding classOf, if I remove 'response' it does return a JSON.

I suspect this might be because of jackson incompatibility. There's a another lib I use which has jackson: 2.14 as transitive dependency. As I understand swagger-play would only support up to jackson 2.10.x.
Is there a way to overcome this?

Versions:

// build.sbt
scalaVersion := "2.12.16"
// plugins.sbt
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.7.9") 
// output of dependencyTree..
// various jackson-databind evicted by.
com.fasterxml.jackson.core:jackson-databind: 2.14.2

My code

 @ApiResponses(Array(
    new ApiResponse(
      code = 200,
      message = "The name of the pet",
      response = classOf[String]
    ),
  ))

The error:

[error] p.c.s.n.PlayRequestHandler - Cannot invoke the action
java.lang.RuntimeException: java.lang.NoClassDefFoundError: Could not initialize class io.swagger.converter.ModelConverters
	at play.api.mvc.ActionBuilder$$anon$10.apply(Action.scala:424)
	at play.api.mvc.Action.$anonfun$apply$2(Action.scala:95)
	at play.api.libs.streams.StrictAccumulator.$anonfun$mapFuture$4(Accumulator.scala:181)
	at scala.util.Try$.apply(Try.scala:213)
	at play.api.libs.streams.StrictAccumulator.$anonfun$mapFuture$3(Accumulator.scala:181)
	at scala.Function1.$anonfun$andThen$1(Function1.scala:57)
	at scala.Function1.$anonfun$andThen$1(Function1.scala:57)
	at scala.Function1.$anonfun$andThen$1(Function1.scala:57)
	at play.api.libs.streams.StrictAccumulator.run(Accumulator.scala:216)
	at play.api.libs.streams.FlattenedAccumulator.$anonfun$run$2(Accumulator.scala:240)
Caused by: java.lang.NoClassDefFoundError: Could not initialize class io.swagger.converter.ModelConverters
	at play.modules.swagger.PlayReader.parseMethod(PlayReader.java:566)
	at play.modules.swagger.PlayReader.read(PlayReader.java:147)
	at play.modules.swagger.PlayReader.read(PlayReader.java:76)
	at play.modules.swagger.PlayReader.read(PlayReader.java:70)
	at play.modules.swagger.ApiListingCache.$anonfun$listing$1(ApiListingCache.scala:17)
	at scala.collection.mutable.HashMap.getOrElseUpdate(HashMap.scala:86)
	at play.modules.swagger.ApiListingCache.listing(ApiListingCache.scala:13)
	at controllers.SwaggerBaseApiController.getResourceListing(ApiHelpController.scala:69)
	at controllers.SwaggerBaseApiController.getResourceListing$(ApiHelpController.scala:55)
	at controllers.ApiHelpController.getResourceListing(ApiHelpController.scala:30)
[error] application - 

! @83odeke85 - Internal server error, for (GET) [/swagger.json] ->
 
play.api.http.HttpErrorHandlerExceptions$$anon$1: Execution exception[[RuntimeException: java.lang.NoClassDefFoundError: Could not initialize class io.swagger.converter.ModelConverters]]
	at play.api.http.HttpErrorHandlerExceptions$.throwableToUsefulException(HttpErrorHandler.scala:344)
	at play.api.http.DefaultHttpErrorHandler.onServerError(HttpErrorHandler.scala:263)
	at play.core.server.netty.PlayRequestHandler$$anonfun$play$core$server$netty$PlayRequestHandler$$handleAction$1.applyOrElse(PlayRequestHandler.scala:303)
	at play.core.server.netty.PlayRequestHandler$$anonfun$play$core$server$netty$PlayRequestHandler$$handleAction$1.applyOrElse(PlayRequestHandler.scala:300)
	at scala.concurrent.Future.$anonfun$recoverWith$1(Future.scala:417)
	at scala.concurrent.impl.Promise.$anonfun$transformWith$1(Promise.scala:41)
	at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:64)
	at play.api.libs.streams.Execution$trampoline$.execute(Execution.scala:70)
	at scala.concurrent.impl.CallbackRunnable.executeWithValue(Promise.scala:72)
	at scala.concurrent.impl.Promise$DefaultPromise.$anonfun$tryComplete$1(Promise.scala:288)
Caused by: java.lang.RuntimeException: java.lang.NoClassDefFoundError: Could not initialize class io.swagger.converter.ModelConverters
	at play.api.mvc.ActionBuilder$$anon$10.apply(Action.scala:424)
	at play.api.mvc.Action.$anonfun$apply$2(Action.scala:95)
	at play.api.libs.streams.StrictAccumulator.$anonfun$mapFuture$4(Accumulator.scala:181)
	at scala.util.Try$.apply(Try.scala:213)
	at play.api.libs.streams.StrictAccumulator.$anonfun$mapFuture$3(Accumulator.scala:181)
	at scala.Function1.$anonfun$andThen$1(Function1.scala:57)
	at scala.Function1.$anonfun$andThen$1(Function1.scala:57)
	at scala.Function1.$anonfun$andThen$1(Function1.scala:57)
	at play.api.libs.streams.StrictAccumulator.run(Accumulator.scala:216)
	at play.api.libs.streams.FlattenedAccumulator.$anonfun$run$2(Accumulator.scala:240)
Caused by: java.lang.NoClassDefFoundError: Could not initialize class io.swagger.converter.ModelConverters
	at play.modules.swagger.PlayReader.parseMethod(PlayReader.java:566)
	at play.modules.swagger.PlayReader.read(PlayReader.java:147)
	at play.modules.swagger.PlayReader.read(PlayReader.java:76)
	at play.modules.swagger.PlayReader.read(PlayReader.java:70)
	at play.modules.swagger.ApiListingCache.$anonfun$listing$1(ApiListingCache.scala:17)
	at scala.collection.mutable.HashMap.getOrElseUpdate(HashMap.scala:86)
	at play.modules.swagger.ApiListingCache.listing(ApiListingCache.scala:13)
	at controllers.SwaggerBaseApiController.getResourceListing(ApiHelpController.scala:69)
	at controllers.SwaggerBaseApiController.getResourceListing$(ApiHelpController.scala:55)
	at controllers.ApiHelpController.getResourceListing(ApiHelpController.scala:30)
@dwickern
Copy link
Owner

So it failed to initialize io.swagger.converter.ModelConverters. The class is there on the classpath but the class constructor threw an exception. It would help if you can find the exception which occurred, maybe using your debugger.

Your third-party library pulled in jackson-databind 2.14.2 but you still have other Jackson libs on version 2.9. This is a problem. You shouldn't mix different versions.

Here are some things you can do:

  1. Downgrade to Jackson 2.9 by excluding the Jackson 2.14 dependency. This might cause an issue for the third-party library. Also you don't really want to be on Jackson 2.9...
  2. Upgrade all Jackson libs to 2.14. This might cause an issue for Play and/or Swagger.
  3. Find an alternative to your third-party lib
  4. Upgrade your Play version, use a recent version of Jackson, then introduce swagger-play

Swagger-play doesn't care about the Jackson version, but Play Framework and swagger-core both do. See the compatibility table in the README.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants