当试图将swagger合并到我的API中时,我遇到了一些问题。
我的API使用的是Jersey 2.22,语言是Scala。我已将以下依赖项添加到我的pom中:
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>1.5.12</version>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-scala-module_2.11</artifactId>
<version>1.0.3</version>
</dependency>
我的插件配置如下:
<plugin>
<groupId>com.github.kongchen</groupId>
<artifactId>swagger-maven-plugin</artifactId>
<version>${kongchen.plugin.version}</version>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<apiSources>
<apiSource>
<locations>${swagger.locations}</locations>
<springmvc>false</springmvc>
<host>${swagger.host}</host>
<basePath>${base.path}</basePath>
<info>
<title>${swagger.title}</title>
<version>${project.version}</version>
<description>${swagger.description}</description>
</info>
<swaggerDirectory>${project.build.directory}/</swaggerDirectory>
<outputFormats>yaml,json</outputFormats>
</apiSource>
</apiSources>
</configuration>
</plugin>
当我运行mvn compile时,会发生以下错误:
Exception in thread "main" java.util.ServiceConfigurationError: io.swagger.converter.ModelConverter: Provider io.swagger.scala.converter.SwaggerScalaModelConverter could not be instantiated
at java.util.ServiceLoader.fail(ServiceLoader.java:232)
at java.util.ServiceLoader.access$100(ServiceLoader.java:185)
at java.util.ServiceLoader$LazyIterator.nextService(ServiceLoader.java:384)
at java.util.ServiceLoader$LazyIterator.next(ServiceLoader.java:404)
at java.util.ServiceLoader$1.next(ServiceLoader.java:480)
at io.swagger.converter.ModelConverters.<clinit>(ModelConverters.java:114)
at com.github.kongchen.swagger.docgen.AbstractDocumentSource.loadModelModifier(AbstractDocumentSource.java:179)
at com.github.kongchen.swagger.docgen.mavenplugin.ApiDocumentMojo.execute(ApiDocumentMojo.java:71)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:134)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:207)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)
at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:307)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:193)
at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:106)
at org.apache.maven.cli.MavenCli.execute(MavenCli.java:863)
at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:288)
at org.apache.maven.cli.MavenCli.main(MavenCli.java:199)
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 org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
Caused by: java.lang.VerifyError: Cannot inherit from final class
如果我省略scala-swagger模块,编译工作正常,我得到一个有效的json(所有资源描述都生成完美),但scala案例类中的字段不会生成:
@ApiModel("some model")
case class HelloModel(@(ApiModelProperty @field)(dataType = "Int", value = "some int", name = "tries") tries: Int)
结果:
"definitions" : {
"some model" : {
"type" : "object"
}
}
任何人都有线索?
答案 0 :(得分:2)
经过一番繁重的调试,我发现了它。看来我使用的swagger-maven-plugin(3.1.1)使用的是swagger-core 1.5.4。这种摇摆不定的依赖性依赖于Jackson 2.4.5。
swagger-scala-module_2.11正在使用Jackson 2.8.7。 &#34;不能继承最后的班级&#34;错误来自TypeReference类,它明显改变/在2.8.7中引入。
解决方案是使用依赖项块在swagger-maven-plugin中包含swagger-scala-module_2.11:
<build>
<plugins>
<plugin>
<groupId>com.github.kongchen</groupId>
<artifactId>swagger-maven-plugin</artifactId>
<version>${swagger-scala-maven-plugin}</version>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-scala-module_2.11</artifactId>
<version>1.0.3</version>
</dependency>
</dependencies>
<configuration>
<apiSources>
<apiSource>
<locations>
<location>${swagger.locations}</location>
</locations>
<springmvc>false</springmvc>
<host>${swagger.host}</host>
<basePath>${base.path}</basePath>
<info>
<title>${swagger.title}</title>
<version>${project.version}</version>
<description>${swagger.description}</description>
</info>
<swaggerDirectory>${project.build.directory}/</swaggerDirectory>
<outputFormats>json</outputFormats>
</apiSource>
</apiSources>
</configuration>
</plugin>
</plugins>
</build>
这将使swagger-maven-plugin使用swagger core 1.5.12而不是提供的1.5.9。
答案 1 :(得分:0)
我在Play Framework 2.4.6项目中使用SBT遇到了同样的问题。 GieJay提供的答案在我的案例中也证明是正确的,我只是想在SBT环境中指出我的解决方案;在我的libraryDependencies
我有
"io.swagger" %% "swagger-play2" % "1.5.1"
使用Play 2.4.8在另一个项目中工作正常但我在较旧的Play版本中遇到了这个问题。 我所要做的只是添加
"io.swagger" % "swagger-scala-module_2.11" % "1.0.3"
依赖关系,所有工作都像魅力。