problem while Integrating Swagger with java jax-rs jersey

3,564 views
Skip to first unread message

yeshwanth kumar

unread,
Dec 12, 2013, 2:07:59 AM12/12/13
to swagger-sw...@googlegroups.com
hi,

i am trying to integrate swagger with jersey jax-rs.
here's my web.xml

<servlet>
<servlet-name>jersey</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.rest.server;com.wordnik.swagger.jersey.listing</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.config.property.classnames</param-name>
<param-value>com.wordnik.swagger.jaxrs.listing</param-value>
</init-param>
</servlet>
<servlet>
<servlet-name>DefaultJaxrsConfig</servlet-name>
<servlet-class>com.wordnik.swagger.jaxrs.config.DefaultJaxrsConfig</servlet-class>
<init-param>
<param-name>api.version</param-name>
<param-value>1.0.0</param-value>
</init-param>
<init-param>
<param-name>swagger.api.basepath</param-name>
<param-value>http://localhost:9080/api</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>


and here's my dependency in pom.xml

              <dependency>
<groupId>com.wordnik</groupId>
<artifactId>swagger-jersey-jaxrs_2.10</artifactId>
<version>1.3.0</version>
</dependency>

files are not generating in web-app folder.
i am following this tutorial as it is 


i couldn't get it done
am i missing anything, please someone help me to get this thing work.

Thanks.

Ron R

unread,
Dec 12, 2013, 3:03:55 AM12/12/13
to swagger-sw...@googlegroups.com
Hi there,

In general, Swagger doesn't generate files in the file system unless specifically told to using the maven/ant plugins for the job.
The Swagger files are generated dynamically at runtime and served on the REST API call that is generated by Swagger.
What happens when you direct your browser to http://localhost:9080/api (assuming you're running it on your local machine)?

yeshwanth kumar

unread,
Dec 12, 2013, 4:36:11 AM12/12/13
to swagger-sw...@googlegroups.com
hi ron,
earlier i was using enunciate which generates files for Documentation of Rest API's,
i thought swagger works in the same way.

any way i configured swagger, now its working but i am unable to access the web page for rest-api's

here's my new pom.xml and web.xml

                <dependency>
<groupId>com.github.kongchen</groupId>
<artifactId>swagger-maven-plugin</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>com.wordnik</groupId>
<artifactId>swagger-codegen_2.9.1</artifactId>
<version>2.0.11</version>
</dependency>
<dependency>
<groupId>com.wordnik</groupId>
<artifactId>swagger-jaxrs_2.9.1</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>com.github.spullara.mustache.java</groupId>
<artifactId>compiler</artifactId>
<version>0.8.13</version>
</dependency>

<plugin>
<groupId>com.github.kongchen</groupId>
<artifactId>swagger-maven-plugin</artifactId>
<version>1.1.1</version>
<configuration>
<apiSources>
<apiSource>
<locations>com.serendio.penton.rest.server</locations>
<apiVersion>v1</apiVersion>
<basePath>http://localhost:9080/docs</basePath>
<outputPath>${project.build.outputDirectory}/docs.html</outputPath>
<withFormatSuffix>false</withFormatSuffix>
<swaggerDirectory>src/main/webapp/apidocs</swaggerDirectory>
</apiSource>
</apiSources>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>


as specified swagger generated two json PET.JSON and SERVICE.JSON files in src/main/webapp/apidocs directory and docs.html in target/classes/ directory.

when i opened docs.html using chrome it gave this without any UI
i don't know how to access UI for documentation

"""""""""""

#API Document ## BasePath: http://localhost:9080/docs ## Api Version: v1 ## Resources ### 1. /pet #### Overview Operations about pets #### 1.1 `/petadd` ##### 1.1.1 ruleAdd **GET** `/petadd` Find pet by ID More notes about this method ###### URL http://localhost:9080/docs/petadd ###### Parameters ###### Response [](#) ###### Errors
Status CodeReason
- - - ## Data Types

"""""""""

i know i missed out something, can u help me to figure that out.

Thanks.

Ron R

unread,
Dec 12, 2013, 5:50:08 AM12/12/13
to swagger-sw...@googlegroups.com
Hello again,

Swagger-core generates JSON files that describe the REST API based on the Swagger Spec.
In order to take those JSON files and make them 'visible' please take a look at the https://github.com/wordnik/swagger-ui project.
Please let me know if you have any issues with it.

yeshwanth kumar

unread,
Dec 12, 2013, 7:04:04 AM12/12/13
to swagger-sw...@googlegroups.com
i am getting this warning

"""""""
WARN:oejs.ResourceCache:Could not load jar:file: .m2/repository/org/eclipse/jetty/jetty-util/8.1.13.v20130916/jetty-util-8.1.13.v20130916.jar!/jetty-dir.css true 1386842110000 text/css
""""""""

swagger won't generate UI automatically?

Ron R

unread,
Dec 12, 2013, 8:58:57 AM12/12/13
to swagger-sw...@googlegroups.com
I think you're confusing a few things.

In its basis - Swagger-Core is used to open and generate a Swagger-Spec of your REST API at *runtime*.
You've included in your pom the swagger-maven-plugin which does manual processing and generates the Swagger-Spec for offline usage (that is, even if your application is not running).
I'm not familiar with the plugin at all.

As for the warning, that tells me ferry little (other than some jetty util jar not being loaded) but that's only a partial log, and you're not saying where it's coming from. What do you do when you get this error? Do you compile your application using maven? Try to run your application? Do you get this from Swagger-UI?

yeshwanth kumar

unread,
Dec 13, 2013, 1:03:40 AM12/13/13
to swagger-sw...@googlegroups.com
hi ron,

i am totally confused can you give me the steps to generate swagger documentation,
i am using maven with jetty plugin and jersey JAXrs.
swagger plugin is executing by mvn install
and i am running jetty by mvn jetty:run
and am getting that error when i am accessing docs.html file specified in pom.xml 
swagger is generating json files in the specified directory in pom.xml---> src/main/webapp/apidocs.
i didn't tried swagger UI, i think maven swagger plugin will do the work.
i will look into swagger UI

i am attaching that file as reference, please take a look.

Thanks a ton,
Yeshwanth
docs.html

tony tam

unread,
Dec 13, 2013, 1:24:13 AM12/13/13
to swagger-sw...@googlegroups.com
Hi Yeshwanth,
Ron is correct.  There are different ways to integrate with swagger, one is at run-time using swagger-jaxrs, per the quickstart here: https://github.com/wordnik/swagger-core/wiki/Java-JAXRS-Quickstart.  The other is making offline files with the com.github.kongchen plugin.  You have to choose one.

I suggest you download the samples and work from there.  If you're working in java, start with this sample:


Modify the sample to get it closer to what you're after.

yeshwanth kumar

unread,
Dec 13, 2013, 1:44:26 AM12/13/13
to swagger-sw...@googlegroups.com
hi tony,

i tried that runtime swagger-jaxrs generation tutorial which u specified...
i got stuck with the runtime exception while running jetty server.

WARN:oejs.Holder:
java.lang.ClassNotFoundException: com.wordnik.swagger.jersey.config.JerseyJaxrsConfig
at org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy.loadClass(SelfFirstStrategy.java:50)
at org.codehaus.plexus.classworlds.realm.ClassRealm.unsynchronizedLoadClass(ClassRealm.java:259)
at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass(ClassRealm.java:235)
at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass(ClassRealm.java:227)
at org.eclipse.jetty.webapp.WebAppClassLoader.loadClass(WebAppClassLoader.java:430)
at org.eclipse.jetty.webapp.WebAppClassLoader.loadClass(WebAppClassLoader.java:383)
at org.eclipse.jetty.util.Loader.loadClass(Loader.java:100)
at org.eclipse.jetty.util.Loader.loadClass(Loader.java:79)
at org.eclipse.jetty.servlet.Holder.doStart(Holder.java:107)
at org.eclipse.jetty.servlet.ServletHolder.doStart(ServletHolder.java:300)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64)
at org.eclipse.jetty.servlet.ServletHandler.initialize(ServletHandler.java:786)
at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:265)
at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1242)
at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:717)
at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:494)
at org.mortbay.jetty.plugin.JettyWebAppContext.doStart(JettyWebAppContext.java:298)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64)
at org.eclipse.jetty.server.handler.HandlerCollection.doStart(HandlerCollection.java:229)
at org.eclipse.jetty.server.handler.ContextHandlerCollection.doStart(ContextHandlerCollection.java:172)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64)
at org.eclipse.jetty.server.handler.HandlerCollection.doStart(HandlerCollection.java:229)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64)
at org.eclipse.jetty.server.handler.HandlerWrapper.doStart(HandlerWrapper.java:95)
at org.eclipse.jetty.server.Server.doStart(Server.java:282)
at org.mortbay.jetty.plugin.JettyServer.doStart(JettyServer.java:65)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64)
at org.mortbay.jetty.plugin.AbstractJettyMojo.startJetty(AbstractJettyMojo.java:520)
at org.mortbay.jetty.plugin.AbstractJettyMojo.execute(AbstractJettyMojo.java:365)
at org.mortbay.jetty.plugin.JettyRunMojo.execute(JettyRunMojo.java:523)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:106)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:208)
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:84)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:59)
at org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.java:183)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:161)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:317)
at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:152)
at org.apache.maven.cli.MavenCli.execute(MavenCli.java:555)
at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:214)
at org.apache.maven.cli.MavenCli.main(MavenCli.java:158)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
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)

so i moved to com.github.kongchen plugin where i was able to generate json files for api's but unable to generate UI.
going through swagger UI now,
i tried to resolve that exception but didn't found luck.... 
can u help me with that.

Thanks,
yeshwanth.

tony tam

unread,
Dec 13, 2013, 1:48:58 AM12/13/13
to swagger-sw...@googlegroups.com
Hi Yeshwanth, I'm positive that the samples run fine, they are part of our build process and we cannot even publish the project if they don't.

That said, can you please follow these steps and post back your results?  Alternatively you can jump on irc.freenode.net  #swagger to chat.  I'll be around for another 30 minutes.

Tony

cd samples/java-jaxrs

mvn clean package jetty:run &


then


gives you this:

{
  "apiVersion": "1.0.0",
  "swaggerVersion": "1.2",
  "apis": [
    {
      "path": "/pet",
      "description": "Operations about pets"
    },
    {
      "path": "/user",
      "description": "Operations about user"
    }
  ]
}
Reply all
Reply to author
Forward
0 new messages