问题描述
在尝试将招摇入我的API时,我遇到了一些问题.
When trying to incorporate swagger into my API I'm running into some issues.
我的API使用的是Jersey 2.22,语言是Scala.我在pom中添加了以下依赖项:
My API is using Jersey 2.22 and the language is Scala. I've added the following dependencies to my 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编译时,会发生以下错误:
When I'm running mvn compile, the following error occurs:
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 case类中的字段未生成:
If I leave out the scala-swagger module, compile works fine and I'm getting a valid json (all resource descriptions are generated perfectly), but the fields in the scala case classes don't get generated:
@ApiModel("some model")
case class HelloModel(@(ApiModelProperty @field)(dataType = "Int", value = "some int", name = "tries") tries: Int)
结果:
"definitions" : {
"some model" : {
"type" : "object"
}
}
有人知道吗?
推荐答案
好吧,我在经过大量调试之后才弄清楚了.看来我正在使用(3.1.1)的swagger-maven-plugin使用的是swagger-core 1.5.4.这种招摇的核心依赖项依赖于Jackson 2.4.5.
Well I figured it out after some heavy debugging. It appears the swagger-maven-plugin which I was using (3.1.1) was using swagger-core 1.5.4. This swagger-core dependency had a dependency to Jackson 2.4.5.
swagger-scala-module_2.11使用的是Jackson 2.8.7. 无法从最终类继承"错误来自TypeReference类,该类显然已更改/获得了2.8.7的引入.
The swagger-scala-module_2.11 is using Jackson 2.8.7. The "Cannot inherit from final class" error came from the TypeReference class, which apparently changed/got introduced in 2.8.7.
解决方案是通过使用依赖性块将swagger-scala-module_2.11包含在swagger-maven-plugin内:
The solution was including the swagger-scala-module_2.11 inside of the swagger-maven-plugin by using the dependencies block:
<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核心1.5.12而不是所提供的1.5.9.
This will make the swagger-maven-plugin use swagger core 1.5.12 instead of the provided 1.5.9.
这篇关于提供程序io.swagger.scala.converter.SwaggerScalaModelConverter无法实例化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!