将swagger-play2升级到1.5.0后出错

时间:2016-01-06 12:58:48

标签: playframework-2.0 swagger swagger-2.0

我试图在现有的Play Framework 2.4应用程序中将swagger从1.3升级到1.5.x。

我已将依赖项更改为

"io.swagger" % "swagger-jaxrs" % "1.5.4" 
"io.swagger" %% "swagger-play2" % "1.5.0"

并将导入从com.wordnik.swagger更改为io.swagger并添加了

play.modules.enabled += play.modules.swagger.SwaggerModule 

到application.conf。

这一切都编译并运行但是当我尝试访问api-docs时,我得到以下异常。

com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: io.swagger.models.Swagger["paths"])
at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:210)
at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:177)
at com.fasterxml.jackson.databind.ser.std.StdSerializer.wrapAndThrow(StdSerializer.java:199)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:683)
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:157)
at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:130)
at com.fasterxml.jackson.databind.ObjectWriter$Prefetch.serialize(ObjectWriter.java:1387)
at com.fasterxml.jackson.databind.ObjectWriter._configAndWriteValue(ObjectWriter.java:1088)
at com.fasterxml.jackson.databind.ObjectWriter.writeValueAsString(ObjectWriter.java:960)
at io.swagger.util.Json.pretty(Json.java:23)
at controllers.SwaggerBaseApiController.toJsonString(ApiHelpController.scala:197)
at controllers.ApiHelpController$$anonfun$getResource$1.apply(ApiHelpController.scala:89)
at controllers.ApiHelpController$$anonfun$getResource$1.apply(ApiHelpController.scala:83)
at play.api.mvc.ActionBuilder$$anonfun$apply$16.apply(Action.scala:408)
at play.api.mvc.ActionBuilder$$anonfun$apply$16.apply(Action.scala:407)
at play.api.mvc.Action$.invokeBlock(Action.scala:533)
at play.api.mvc.Action$.invokeBlock(Action.scala:530)
at play.api.mvc.ActionBuilder$$anon$1.apply(Action.scala:493)
at play.api.mvc.Action$$anonfun$apply$1$$anonfun$apply$4$$anonfun$apply$5.apply(Action.scala:105)
at play.api.mvc.Action$$anonfun$apply$1$$anonfun$apply$4$$anonfun$apply$5.apply(Action.scala:105)
at play.utils.Threads$.withContextClassLoader(Threads.scala:21)
at play.api.mvc.Action$$anonfun$apply$1$$anonfun$apply$4.apply(Action.scala:104)
at play.api.mvc.Action$$anonfun$apply$1$$anonfun$apply$4.apply(Action.scala:103)
at scala.Option.map(Option.scala:146)
at play.api.mvc.Action$$anonfun$apply$1.apply(Action.scala:103)
at play.api.mvc.Action$$anonfun$apply$1.apply(Action.scala:96)
at play.api.libs.iteratee.DoneIteratee$$anonfun$mapM$2.apply(Iteratee.scala:741)
at play.api.libs.iteratee.DoneIteratee$$anonfun$mapM$2.apply(Iteratee.scala:741)
at scala.concurrent.impl.Future$PromiseCompletingRunnable.liftedTree1$1(Future.scala:24)
at scala.concurrent.impl.Future$PromiseCompletingRunnable.run(Future.scala:24)
at akka.dispatch.TaskInvocation.run(AbstractDispatcher.scala:39)
at akka.dispatch.ForkJoinExecutorConfigurator$AkkaForkJoinTask.exec(AbstractDispatcher.scala:399)
at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260)
at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339)
at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979)
at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107)
Caused by: java.lang.NullPointerException
at scala.collection.convert.Wrappers$JMapWrapperLike$$anon$2.<init>(Wrappers.scala:281)
at scala.collection.convert.Wrappers$JMapWrapperLike$class.iterator(Wrappers.scala:280)
at scala.collection.convert.Wrappers$JMapWrapper.iterator(Wrappers.scala:298)
at scala.collection.IterableLike$class.foreach(IterableLike.scala:72)
at scala.collection.AbstractIterable.foreach(Iterable.scala:54)
at scala.collection.MapLike$FilteredKeys.foreach(MapLike.scala:231)
at scala.collection.TraversableOnce$class.size(TraversableOnce.scala:105)
at scala.collection.AbstractTraversable.size(Traversable.scala:104)
at scala.collection.convert.Wrappers$MapWrapper.size(Wrappers.scala:169)
at java.util.AbstractMap$1.size(AbstractMap.java:349)
at java.util.AbstractCollection.toArray(AbstractCollection.java:136)
at java.util.ArrayList.addAll(ArrayList.java:577)
at io.swagger.models.Swagger.getPaths(Swagger.java:266)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:654)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:675)
        ... 32 more
[error] 12:42:04.504 swagger - api listing for path /booking not found

看起来问题是io.swagger.models.Swagger.paths为null但是每个@Api带注释的类应该包含一个条目。如何让SwaggerModule正确初始化路径?

1 个答案:

答案 0 :(得分:0)

对我有用的解决方案是明确声明对jackson 2.8.2的依赖:

"com.fasterxml.jackson.core" % "jackson-databind" % "2.8.2",
"com.fasterxml.jackson.module" % "jackson-module-scala_2.11" % "2.8.2"

据我所知,它不适用于2.7.x和2.6.x。