How to use Swagger API in Netbean 6.9.1?

2,313 views
Skip to first unread message

Mukesh Jha

unread,
Feb 14, 2014, 1:02:52 AM2/14/14
to swagger-sw...@googlegroups.com
Hi all,

I'm new to Swagger and I have created a Rest API(jax-rs + jersey) in Netbean 6.9.1, while searching for API documentation tool I came across Swagger API which seems nice. Now I want to how can I use Swagger API to document my existing Rest API. Please tell me step by step because I went through Swagger api on GIT but not able to use it. Please also tell me how to use Swagger-ui. 

I have following questions:
  • Steps to use/configure swagger?
  • Can I use Swagger for existing Rest API project?
  • Can I use it in non-maven project in Netbean or else where?
  • Which jars/binaries I need to use Swagger?
  • Where I'll find jars/binaries related to Swagger?
  • How to use Swagger-UI?
I want to go for server integration so that I can provide JSON dynamically.

Need a urgent help... any help will be highly appreciated.

Thanks

Mukesh.

Ron

unread,
Feb 14, 2014, 3:57:42 AM2/14/14
to swagger-sw...@googlegroups.com
Hello Mukesh,

You can integrate Swagger with your existing project without any issues - you can find the information you need here - https://github.com/wordnik/swagger-core/wiki/Java-JAXRS-Quickstart.

As for not using maven - if you don't use any dependency management tool, you'll end up running to various issues fast enough (this is unrelated with Swagger though). The list of all the dependencies swagger-jaxrs currently has are:

[INFO] com.wordnik:swagger-jaxrs_2.10:jar:1.3.2
[INFO] +- org.scalatest:scalatest_2.10:jar:1.9:test
[INFO] |  +- org.scala-lang:scala-actors:jar:2.10.0:test
[INFO] |  \- org.scala-lang:scala-reflect:jar:2.10.0:compile
[INFO] +- junit:junit:jar:4.8.1:test
[INFO] +- org.scala-lang:scala-library:jar:2.10.0:compile
[INFO] +- com.wordnik:swagger-core_2.10:jar:1.3.2:compile
[INFO] |  +- commons-lang:commons-lang:jar:2.4:compile
[INFO] |  +- org.slf4j:slf4j-api:jar:1.6.3:compile
[INFO] |  +- com.fasterxml.jackson.module:jackson-module-scala_2.10:jar:2.1.5:compile
[INFO] |  |  +- com.fasterxml.jackson.core:jackson-core:jar:2.1.5:compile
[INFO] |  |  +- com.fasterxml.jackson.core:jackson-databind:jar:2.1.5:compile
[INFO] |  |  \- com.thoughtworks.paranamer:paranamer:jar:2.3:compile
[INFO] |  +- com.fasterxml.jackson.core:jackson-annotations:jar:2.1.5:compile
[INFO] |  +- com.fasterxml.jackson.module:jackson-module-jsonSchema:jar:2.1.0:compile
[INFO] |  +- com.fasterxml.jackson.jaxrs:jackson-jaxrs-json-provider:jar:2.0.0:compile
[INFO] |  |  \- com.fasterxml.jackson.module:jackson-module-jaxb-annotations:jar:2.0.0:compile
[INFO] |  +- com.wordnik:swagger-annotations:jar:1.3.2:compile
[INFO] |  +- org.json4s:json4s-ext_2.10:jar:3.2.4:compile
[INFO] |  |  +- joda-time:joda-time:jar:2.1:compile
[INFO] |  |  \- org.joda:joda-convert:jar:1.2:compile
[INFO] |  +- org.json4s:json4s-native_2.10:jar:3.2.4:compile
[INFO] |  |  \- org.json4s:json4s-core_2.10:jar:3.2.4:compile
[INFO] |  |     +- org.json4s:json4s-ast_2.10:jar:3.2.4:compile
[INFO] |  |     \- org.scala-lang:scalap:jar:2.10.0:compile
[INFO] |  |        \- org.scala-lang:scala-compiler:jar:2.10.0:compile
[INFO] |  \- org.json4s:json4s-jackson_2.10:jar:3.2.4:compile
[INFO] +- javax.servlet:servlet-api:jar:2.5:provided
[INFO] +- javax.ws.rs:jsr311-api:jar:1.1.1:compile
[INFO] +- org.reflections:reflections:jar:0.9.8:compile
[INFO] |  +- com.google.guava:guava:jar:11.0.2:compile
[INFO] |  |  \- com.google.code.findbugs:jsr305:jar:1.3.9:compile
[INFO] |  +- javassist:javassist:jar:3.12.1.GA:compile
[INFO] |  \- dom4j:dom4j:jar:1.6.1:compile
[INFO] |     \- xml-apis:xml-apis:jar:1.0.b2:compile
[INFO] +- ch.qos.logback:logback-classic:jar:1.0.1:provided
[INFO] \- ch.qos.logback:logback-core:jar:1.0.1:provided

As you can see, not something that can be easily managed manually. You'll find all those jars in maven central (from which you can download the dependencies manually).

As for Swagger-UI - if you follow the wiki link from before, you should end up with your own json files being served by your server. Then you can follow the directions here - https://github.com/wordnik/swagger-ui#use - and point Swagger-UI to your own files.


--
You received this message because you are subscribed to the Google Groups "Swagger" group.
To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggers...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Mukesh Jha

unread,
Feb 14, 2014, 7:51:56 AM2/14/14
to swagger-sw...@googlegroups.com
Hello Ron,

I have setup a new project using maven in Netbean and did the same as given in link: https://github.com/wordnik/swagger-core/wiki/Java-JAXRS-Quickstart., but I'm not able to build that project. I'm getting following build error:

NetBeans: Executing 'D:\Java\apache-maven-3.1.1\bin\mvn.bat -Dnetbeans.deploy=true -Dnetbeans.execution=true package'
NetBeans:      JAVA_HOME=C:\Program Files\Java\jdk1.7.0
Scanning for projects...
                                                                        
------------------------------------------------------------------------
Building mavenJaxRs Java EE 6 Webapp 1.0-SNAPSHOT
------------------------------------------------------------------------

--- maven-resources-plugin:2.6:resources (default-resources) @ mavenJaxRs ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
skip non existing resourceDirectory C:\Users\mjha\Documents\NetBeansProjects\mavenJaxRs\src\main\resources

--- maven-compiler-plugin:2.0.2:compile (default-compile) @ mavenJaxRs ---
Nothing to compile - all classes are up to date

--- maven-resources-plugin:2.6:testResources (default-testResources) @ mavenJaxRs ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
skip non existing resourceDirectory C:\Users\mjha\Documents\NetBeansProjects\mavenJaxRs\src\test\resources

--- maven-compiler-plugin:2.0.2:testCompile (default-testCompile) @ mavenJaxRs ---
Nothing to compile - all classes are up to date

--- maven-surefire-plugin:2.12.4:test (default-test) @ mavenJaxRs ---
No tests to run.

--- maven-war-plugin:2.1-beta-1:war (default-war) @ mavenJaxRs ---
Packaging webapp
------------------------------------------------------------------------
BUILD FAILURE
------------------------------------------------------------------------
Total time: 1.212s
Finished at: Fri Feb 14 18:11:27 IST 2014
Final Memory: 7M/18M
------------------------------------------------------------------------
[ERROR]Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.1-beta-1:war (default-war) on project mavenJaxRs: Execution default-war of goal org.apache.maven.plugins:maven-war-plugin:2.1-beta-1:war failed: Cannot construct org.apache.maven.plugin.war.util.WebappStructure as it does not have a no-args constructor : Cannot construct org.apache.maven.plugin.war.util.WebappStructure as it does not have a no-args constructor
[ERROR]---- Debugging information ----
[ERROR]message             : Cannot construct org.apache.maven.plugin.war.util.WebappStructure as it does not have a no-args constructor
[ERROR]cause-exception     : com.thoughtworks.xstream.converters.reflection.ObjectAccessException
[ERROR]cause-message       : Cannot construct org.apache.maven.plugin.war.util.WebappStructure as it does not have a no-args constructor
[ERROR]class               : org.apache.maven.plugin.war.util.WebappStructure
[ERROR]required-type       : org.apache.maven.plugin.war.util.WebappStructure
[ERROR]path                : /webapp-structure
[ERROR]line number         : 1
[ERROR]-------------------------------
[ERROR]-> [Help 1]
[ERROR]
[ERROR]To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR]Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR]For more information about the errors and possible solutions, please read the following articles:

My web.xml file is attached.


Thanks

To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggersocket+unsub...@googlegroups.com.
web.xml

Ron

unread,
Feb 14, 2014, 7:59:58 AM2/14/14
to swagger-sw...@googlegroups.com
The maven error has nothing to do with the web.xml but more with the pom.xml. Try following the directions in the link and if that doesn't work please attach the pom.xml.


To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggers...@googlegroups.com.
Message has been deleted

Mukesh Jha

unread,
Feb 17, 2014, 2:56:18 AM2/17/14
to swagger-sw...@googlegroups.com
Hello Ron,
 Thanks for your suggestion. I tried it and it worked. I have made following changes in pom.xml:
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.1.1</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>

and changed jdk version to 1.7. It worked like charm.
I have also implemented Swagger UI. It show all rest api listing and its detail, but when I click to try it, its not working, I have added the break-point on one API, its called and also sending response but it never comes to HTML page, on HTML page only progress bar is showing..... How to resolve that?

One more thing that I want to share with you that I have created a separate web project to create REST API and added necessary binaried/jars(swagger-jaxrs_2.10-1.3.2.jar, swagger-core_2.10-1.3.2.jar, swagger-annotation-1.3.2.jar) of Swagger into lib and also configured web.xml accordingly but when I run it, it throws following exception:


HTTP Status 500 -


type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: Servlet.init() for servlet RestService threw exception
	org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
	org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
	org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:859)
	org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:579)
	org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1555)
	java.lang.Thread.run(Thread.java:722)

root cause

java.lang.NoClassDefFoundError: scala/Function1
	java.lang.Class.getDeclaredConstructors0(Native Method)
	java.lang.Class.privateGetDeclaredConstructors(Class.java:2404)
	java.lang.Class.getConstructors(Class.java:1476)
	com.sun.jersey.server.impl.modelapi.annotation.IntrospectionModeller.createResource(IntrospectionModeller.java:104)
	com.sun.jersey.server.impl.application.WebApplicationImpl.getAbstractResource(WebApplicationImpl.java:570)
	com.sun.jersey.server.impl.application.WebApplicationImpl.processRootResources(WebApplicationImpl.java:1082)
	com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:912)
	com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:589)
	com.sun.jersey.spi.container.servlet.ServletContainer.initiate(ServletContainer.java:403)
	com.sun.jersey.spi.container.servlet.ServletContainer$InternalWebComponent.initiate(ServletContainer.java:252)
	com.sun.jersey.spi.container.servlet.WebComponent.load(WebComponent.java:550)
	com.sun.jersey.spi.container.servlet.WebComponent.init(WebComponent.java:201)
	com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:307)
	com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:470)
	javax.servlet.GenericServlet.init(GenericServlet.java:212)
	org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
	org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
	org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:859)
	org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:579)
	org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1555)
	java.lang.Thread.run(Thread.java:722)

root cause

java.lang.ClassNotFoundException: scala.Function1
	org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1516)
	org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1361)
	java.lang.Class.getDeclaredConstructors0(Native Method)
	java.lang.Class.privateGetDeclaredConstructors(Class.java:2404)
	java.lang.Class.getConstructors(Class.java:1476)
	com.sun.jersey.server.impl.modelapi.annotation.IntrospectionModeller.createResource(IntrospectionModeller.java:104)
	com.sun.jersey.server.impl.application.WebApplicationImpl.getAbstractResource(WebApplicationImpl.java:570)
	com.sun.jersey.server.impl.application.WebApplicationImpl.processRootResources(WebApplicationImpl.java:1082)
	com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:912)
	com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:589)
	com.sun.jersey.spi.container.servlet.ServletContainer.initiate(ServletContainer.java:403)
	com.sun.jersey.spi.container.servlet.ServletContainer$InternalWebComponent.initiate(ServletContainer.java:252)
	com.sun.jersey.spi.container.servlet.WebComponent.load(WebComponent.java:550)
	com.sun.jersey.spi.container.servlet.WebComponent.init(WebComponent.java:201)
	com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:307)
	com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:470)
	javax.servlet.GenericServlet.init(GenericServlet.java:212)
	org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
	org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
	org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:859)
	org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:579)
	org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1555)
	java.lang.Thread.run(Thread.java:722)

note The full stack trace of the root cause is available in the Apache Tomcat/6.0.26 logs.


Apache Tomcat/6.0.26



How to resolve this? 
Can I implement Swagger the way I have described above? If yes, then what changes I need to do in web.xml or else in project.
For your reference I'm attaching web.xml file.
I'm asking you this because, I think this is most convenient way to setup Rest Project and Swagger. 


Thanks,

Mukesh
To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggersocket+unsubscri...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.
web.xml

Ron

unread,
Feb 17, 2014, 4:30:45 AM2/17/14
to swagger-sw...@googlegroups.com
Regarding the first issue, it can be any set of reasons. If invoke the API directly, does it work? You can also try debugging using the console of the different browsers (Chrome, FF, ...).

As for the second issue, this is again an dependency issue. As stated originally, not using a dependency manager and trying to do it manually will lead you to a whole set of issues. In this case, you're missing the scala libraries but once you add that, you'll encounter other errors. If you insist on not using a dependency manager, you can copy all the jars from your /lib dir from the maven-based project. Keep in mind that this is not future-proof and when upgrading versions of swagger, you may need to modify the dependencies again manually.


To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggers...@googlegroups.com.

Mukesh Jha

unread,
Feb 17, 2014, 5:27:54 AM2/17/14
to swagger-sw...@googlegroups.com
Hello Ron,
When I invoke url directly from browser or Rest Client its working but it not working when I try to invoke API from Swagger UI interface. What I observed is difference in both url. When I invoke "http://localhost:8084/mavenJaxRs/api/register/" url from browser or Rest client it working, but when I try to invoke same API from Swagger it reutrns "
HTTP Status 404 - Not Found" and reuest url changed to "http://localhost:8084/api/register/", with following setting in web.xml
        <init-param>
            <param-name>swagger.api.basepath</param-name>
            <param-value>http://localhost:8084/api</param-value>
        </init-param>
which is relative to Swagger basepath url.

When I've following setting in web.xml
  <init-param>
            <param-name>swagger.api.basepath</param-name>
            <param-value>http://localhost:8084/mavenJaxRs/api</param-value>
        </init-param>

I get same result in browser or rest client, but again in Swagger UI it never show any response, it show never ending progress.

Where I'm doing wrong?

Thanks

Ron

unread,
Feb 17, 2014, 6:04:08 AM2/17/14
to swagger-sw...@googlegroups.com
Can you possibly share your project somehow? Via github or otherwise?


To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggers...@googlegroups.com.

Mukesh Jha

unread,
Feb 17, 2014, 6:43:24 AM2/17/14
to swagger-sw...@googlegroups.com
Sure Ron..
Can I have your email please so that I can share my project on Google Drive to you?

Ron

unread,
Feb 17, 2014, 6:46:15 AM2/17/14
to swagger-sw...@googlegroups.com
You should see it in the google group.


To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggers...@googlegroups.com.

Mukesh Jha

unread,
Feb 17, 2014, 7:02:10 AM2/17/14
to swagger-sw...@googlegroups.com
Please get Project from here

Ron

unread,
Feb 17, 2014, 9:32:52 AM2/17/14
to swagger-sw...@googlegroups.com
Mukesh,

The issue is that you declare the method returning the application/json mimetype (@Produces(MediaType.APPLICATION_JSON)) but in fact you return a plain text. The Swagger UI tries to parse it as JSON but fails. Instead of giving you an error, it just looks like it keeps on processing.
Either return a JSON object from your methods or change your mimetype to text/plain.


To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggers...@googlegroups.com.

Mukesh Jha

unread,
Feb 18, 2014, 12:36:57 AM2/18/14
to swagger-sw...@googlegroups.com
Thanks Ron, its worked.
Thanks lot, you help me lot.
Nice guy...
Reply all
Reply to author
Forward
0 new messages