2017-02-21 82 views
0

当试图将swagger并入我的API时,我遇到了一些问题。供应商io.swagger.scala.converter.SwaggerScalaModelConverter无法实例化

我的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编译,出现以下错误:

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类中的字段不会生成:

@ApiModel("some model") 
case class HelloModel(@(ApiModelProperty @field)(dataType = "Int", value = "some int", name = "tries") tries: Int) 

结果:

"definitions" : { 
    "some model" : { 
     "type" : "object" 
    } 
    } 

任何人都得到了线索?

回答

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。 “无法从最终类继承”错误来自TypeReference类,该类在2.8.7中明显改变/引入。

该溶液包括招摇-行家-插件的内部招摇-阶-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> 

这将使招摇-行家-插件使用招摇核心1.5 .12而不是提供的1.5.9。

0

我在Play Framework 2.4.6项目中使用SBT时遇到了同样的问题。 GieJay提供的答案在我的案例中也证明是正确的,我只是想在SBT环境中指出我的解决方案;在我libraryDependencies我有

"io.swagger" %% "swagger-play2" % "1.5.1"

这在使用播放2.4.8其他项目工作正常,但我遇到了一个老的打版中这一问题。 我所要做的就是添加

"io.swagger" % "swagger-scala-module_2.11" % "1.0.3"

的依赖和所有的工作就像一个魅力。