Provider io.swagger.scala.converter.SwaggerScalaModelConverter could not be instantiated

泄露秘密 提交于 2019-12-12 02:26:13

问题


When trying to incorporate swagger into my API I'm running into some issues.

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> 

My plugin is configured as follows:

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

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

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)

Results in:

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

Anyone got a clue?


回答1:


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.

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.

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>

This will make the swagger-maven-plugin use swagger core 1.5.12 instead of the provided 1.5.9.




回答2:


I had the same problem using SBT in a Play Framework 2.4.6 project. The answer provided by GieJay has proven correct in my case as well, I just wanted to point out my solution in an SBT environment; in my libraryDependencies I had

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

which works fine in another project using Play 2.4.8 but I encountered this issue within an older Play version. All I had to do was add

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

to the dependencies and all works like a charm.



来源:https://stackoverflow.com/questions/42361850/provider-io-swagger-scala-converter-swaggerscalamodelconverter-could-not-be-inst

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!