【问题标题】:Provider io.swagger.scala.converter.SwaggerScalaModelConverter could not be instantiated提供程序 io.swagger.scala.converter.SwaggerScalaModelConverter 无法实例化
【发布时间】:2017-04-21 22:14:35
【问题描述】:

在尝试将 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"
    }
  }

有人知道吗?

【问题讨论】:

    标签: scala swagger jersey-2.0


    【解决方案1】:

    好吧,经过一些繁重的调试,我想通了。看来我使用的 swagger-maven-plugin (3.1.1) 正在使用 swagger-core 1.5.4。这个 swagger-core 依赖依赖于 Jackson 2.4.5。

    swagger-scala-module_2.11 使用的是 Jackson 2.8.7。 “不能从最终类继承”错误来自 TypeReference 类,该类显然已在 2.8.7 中更改/引入。

    解决方案是通过使用依赖块将 swagger-scala-module_2.11 包含在 swagger-maven-plugin 中:

    <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。

    【讨论】:

      【解决方案2】:

      我在 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"

      到依赖项,所有工作都像魅力一样。

      【讨论】:

        猜你喜欢
        • 2020-01-17
        • 2018-10-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-15
        相关资源
        最近更新 更多