Problem displaying JSON in swagger UI (and other questions)

3,652 views
Skip to first unread message

Srikanth Madduri

unread,
Jul 23, 2013, 1:54:19 PM7/23/13
to swagger-sw...@googlegroups.com
Hi,

I am trying out Swagger in one of my projects. I set up swagger successfully on my server and I see the JSON for my JAX-RS service fine in the browser.

I added the Bootstrap servlet to remove the .{format} for the APIs. I see the resource on my server at this URL (This is a local deployment):


Response: 

{
    "apiVersion": "1.0",
    "apis": {
        "description": "",
        "path": "/api-docs/v1/companies"
    },
    "basePath": "/private/rest",
    "swaggerVersion": "1.1"
}


Now my first question:

Why is it that I have to give .json format after api-docs in the URL for it to work. The Bootstrap servlet is supposed to remove the .{format} It works for my JAX-RS APIs but for resource listing, I still have to give the .json extension.

Now moving on to the UI part.

This is my config in the html:

<script type="text/javascript">
$(function () {
   window.swaggerUi = new SwaggerUi({
                discoveryUrl:"http://local.intuit.com:8080/cps/private/rest/api-docs.json",
                dom_id:"swagger-ui-container",
                supportHeaderParams: true,
                supportedSubmitMethods: ['get', 'post'],
                onComplete: function(swaggerApi, swaggerUi){
                if(console) {
                        console.log("Loaded SwaggerUI")
                        console.log(swaggerApi);
                        console.log(swaggerUi);
                    }
                  $('pre code').each(function(i, e) {hljs.highlightBlock(e)});
                },
                onFailure: function(data) {
                if(console) {
                        console.log("Unable to Load SwaggerUI");
                        console.log(data);
                    }
                },
                docExpansion: "none"
            });

            window.swaggerUi.load();
        });

    </script>


Now, when I load the UI, I get this error on the HTML page:

discoveryUrl basePath must be a URL.

Not sure what that means. Can you guys give some pointers to solve this issue for me?


Thanks,
Srikanth

tony tam

unread,
Jul 23, 2013, 2:00:51 PM7/23/13
to swagger-sw...@googlegroups.com
Hi, first off, see this thread from yesterday:


It will help you with your JSON containing single-element arrays.

Next, you're using the swagger 1.1 spec, which requires `basePath` in the API Declaration to be an absolute path.  You can get around it with one of the following:

1) Upgrade to swagger-core 1.3-RC2.  There are some signature changes in the code
2) Change your `basePath` to be absolute (I understand this is a pain, but it was a motivating factor in adding support for relative paths in 1.2 spec)
3) Upgrade your swagger-ui to develop-2.0 (https://github.com/wordnik/swagger-ui/blob/develop-2.0) and it will work with both 1.1 and 1.2 specs, meaning your existing code should work as-is

But do read that previous thread, I can see already that you will hit that issue (which is elegantly fixed in 1.3, I'll add).

Srikanth Madduri

unread,
Jul 23, 2013, 4:33:01 PM7/23/13
to swagger-sw...@googlegroups.com
Hi Tony,

Thanks for the reply. I will try what you suggested. Where can quickly grab the distro for 1.3 http://repo1.maven.org/maven2/com/wordnik/ this doesn't seem to have it.

If the only option is to build, I will have to postpone it for now as I have to wrap this up ASAP for a proof of concept.

Thanks,
Srikanth

tony tam

unread,
Jul 23, 2013, 4:37:54 PM7/23/13
to swagger-sw...@googlegroups.com
1.3-RC2 is in maven central, use this dependency:

    <dependency>
      <groupId>com.wordnik</groupId>
      <artifactId>swagger-jaxrs_2.9.1</artifactId>
      <version>1.3-RC2</version>
    </dependency>

Srikanth Madduri

unread,
Jul 23, 2013, 4:44:16 PM7/23/13
to swagger-sw...@googlegroups.com
I followed #3 and upgraded swagger-ui to develop-2.0. Now I don't see the error that I saw previously but the UI doesn't give me anything: I just displays the JSON like this:




base url: /private/rest , api version: 1.0 ]


I checked the server response and it is in fact the correct one:

{
    "apiVersion": "1.0",
    "apis": {
        "description": "",
        "path": "/api-docs/v1/companies"
    },
    "basePath": "/private/rest",
    "swaggerVersion": "1.1"
}

If you notice the path for the apis does not have the .json extension for api-docs. I suspect that is the problem but I maybe wrong.


Also, one thing that I don't understand is the why do I have to give .json extension for my resource listing http://...../api-docs.json when I used the bootstrap to remove the .{format}

Thanks,
Srikanth

On Tuesday, July 23, 2013 11:00:51 AM UTC-7, tony tam wrote:

tony tam

unread,
Jul 23, 2013, 4:47:47 PM7/23/13
to swagger-sw...@googlegroups.com
Your JSON is still not valid--did you see the suggestion in that other thread?  You need to add a JSON mapper like this:

Srikanth Madduri

unread,
Jul 23, 2013, 4:58:36 PM7/23/13
to swagger-sw...@googlegroups.com
Yes. I saw the other thread. I decided to upgrade to 1.3.0RC2 which you mentioned should be able to handle the JSON (per the other thread). I am trying with 1.3 RC2 now.

I will let you know if it works.

Thanks,
Srikanth

tony tam

unread,
Jul 23, 2013, 5:02:47 PM7/23/13
to swagger-sw...@googlegroups.com
OK great, and do note the differences in the web.xml, assuming you use one:


If you're not using web.xml, there's an example with apache CXF that shows integration without it.

Srikanth Madduri

unread,
Jul 24, 2013, 4:48:28 PM7/24/13
to swagger-sw...@googlegroups.com
Ok. I upgraded to 1.3.0RC2 and followed the web.xml in the link that you gave me. I cannot deploy now. I get a NoSuchMethodException like below:

Caused by: java.lang.NoSuchMethodError: scala.Predef$.augmentString(Ljava/lang/String;)Lscala/collection/immutable/StringOps;
at com.wordnik.swagger.jaxrs.JaxrsApiReader$class.$init$(JaxrsApiReader.scala:24)
at com.wordnik.swagger.jaxrs.reader.DefaultJaxrsApiReader.<init>(DefaultJaxrsReader.scala:14)
at com.wordnik.swagger.jaxrs.config.DefaultJaxrsConfig.init(DefaultJaxrsConfig.scala:18)
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1453)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1250)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5093)

I looked at the web.xml and I realized that the Bootstrap from the previous version has been replaced with this one.

This ring any bells?

Srikanth

Srikanth Madduri

unread,
Jul 24, 2013, 5:12:54 PM7/24/13
to swagger-sw...@googlegroups.com
I would like to add some detail to my question. In the previous versions, the basePath needed to be set before the Jersey servlet kicks in. In this new web.xml I see it is happening after.

Srikanth Madduri

unread,
Jul 24, 2013, 5:15:14 PM7/24/13
to swagger-sw...@googlegroups.com
By the way, I have scalabeans-0.3.jar and scala-library-2.10.2.jar in my classpath.

tony tam

unread,
Jul 24, 2013, 5:19:48 PM7/24/13
to swagger-sw...@googlegroups.com
Yes, the RC2 version is scala 2.9.1.  Are you using scala for anything else in your project?  They are binary incompatible, but for release the 2.10 version will be deployed.

Srikanth Madduri

unread,
Jul 24, 2013, 5:30:10 PM7/24/13
to swagger-sw...@googlegroups.com
I added scala only for swagger. I am not using it anywhere else. I will update scala.

Srikanth Madduri

unread,
Jul 24, 2013, 5:33:27 PM7/24/13
to swagger-sw...@googlegroups.com
Also, I am curious which version of the swagger jaxrs should I take. I found this 

/maven2/com/wordnik/swagger-jersey-jaxrs_2.9.1/1.3.0-RC2/ and this

/maven2/com/wordnik/swagger-jaxrs_2.9.1/


the generic jaxrs repo seems to have lot more utilities and the DefaultJaxrsConfig class that is in the web.xml. That is what gave me the error during deployment. But I was tempted to get the 1st repo jersey-jaxrs jar and it is missing a whole bunch of classes.

tony tam

unread,
Jul 24, 2013, 5:40:16 PM7/24/13
to swagger-sw...@googlegroups.com
Are you not using maven or other dependency manager?  scala, etc should come in automatically.  I strongly suggest you use maven as such:

    <dependency>
      <groupId>com.wordnik</groupId>
      <artifactId>swagger-jaxrs_2.9.1</artifactId>
      <version>1.3.0-RC2</version>
      <scope>compile</scope>
    </dependency>

That will bring in scala, etc.

The jersey version ties you to JAXRS + jersey, which makes a lot of sense to me (some don't use jersey for JAXRS).

Srikanth Madduri

unread,
Jul 24, 2013, 5:53:10 PM7/24/13
to swagger-sw...@googlegroups.com
I am not using maven in my project. But I will create a new project just to pull all the related dependencies. That will make my life easier.

Srikanth Madduri

unread,
Jul 24, 2013, 7:40:45 PM7/24/13
to swagger-sw...@googlegroups.com
I downloaded the sources and built using maven. I am looking at the java-jaxrs sample project and the web.xml looks like follows: Will this even work?

<?xml version="1.0" encoding="ISO-8859-1"?>

<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"

  xmlns:j2ee="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">


  <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.wordnik.swagger.jaxrs.listing;com.wordnik.swagger.sample.resource;com.wordnik.swagger.sample.util</param-value>

    </init-param>

    <init-param>

      <param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>

      <param-value>com.sun.jersey.api.container.filter.PostReplaceFilter</param-value>

    </init-param>

    <init-param>

      <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>

      <param-value>true</param-value>

    </init-param>

    <init-param>

      <param-name>api.version</param-name>

      <param-value>0.1</param-value>

    </init-param>

    <init-param>

      <param-name>swagger.version</param-name>

      <param-value>1.1</param-value>

    </init-param>

    <init-param>

      <param-name>swagger.api.basepath</param-name>

      <param-value>http://localhost:8002/api</param-value>

    </init-param>

    <init-param>

      <param-name>swagger.security.filter</param-name>

      <param-value>com.wordnik.swagger.sample.util.ApiAuthorizationFilterImpl</param-value>

    </init-param>

    <load-on-startup>1</load-on-startup>

  </servlet>


  <servlet-mapping>

    <servlet-name>jersey</servlet-name>

    <url-pattern>/*</url-pattern>

  </servlet-mapping>

  <filter>

    <filter-name>ApiOriginFilter</filter-name>

    <filter-class>com.wordnik.swagger.sample.util.ApiOriginFilter</filter-class>

  </filter>

  <filter-mapping>

    <filter-name>ApiOriginFilter</filter-name>

    <url-pattern>/*</url-pattern>

  </filter-mapping>

</web-app>


Also, I got the swagger-core, annotations, jaxrs, jersey-jaxrs, jaxrs-utils jars all of them version 2.9.1_1.2.5


When I include this in my web.xml it cannot find the DefaultJaxrsConfig class. There is no package called com.wordnik.swagger.jaxrs.config.*. Am I missing something here? The web.xml generated by maven build and the one on github are different. Each got it's own problems.


<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:8002/api</param-value>
    </init-param>
    <init-param>
      <param-name>swagger.filter</param-name>
      <param-value>com.wordnik.swagger.sample.util.ApiAuthorizationFilterImpl</param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
  </servlet>

tony tam

unread,
Jul 24, 2013, 8:13:39 PM7/24/13
to swagger-sw...@googlegroups.com
You need to pull the dependencies for 1.3.0-RC2 if you're going to use that.  Then the missing class will be found.

Srikanth Madduri

unread,
Jul 29, 2013, 12:37:28 PM7/29/13
to swagger-sw...@googlegroups.com
I tried both #2 and #3 but no change. I gave the absolute path and also used swagger ui 2.0 like you mentioned in #3 but I don't see anything in the UI. I see that the response from the server is correct though.


On Tuesday, July 23, 2013 11:00:51 AM UTC-7, tony tam wrote:

tony tam

unread,
Jul 29, 2013, 1:48:18 PM7/29/13
to swagger-sw...@googlegroups.com
Hi, you'll need to provide more info than that, I don't know what state you're in.

Srikanth Madduri

unread,
Jul 29, 2013, 1:54:41 PM7/29/13
to swagger-sw...@googlegroups.com
Here is the screenshot of what I am talking about: The response is good from the server. But I don't see anything on the UI. This is the develop 2.0 UI and my html looks like this:

<script type="text/javascript">
    $(function () {
      window.swaggerUi = new SwaggerUi({
      dom_id: "swagger-ui-container",
      supportHeaderParams: true,
      supportedSubmitMethods: ['get', 'post'],
      onComplete: function(swaggerApi, swaggerUi){
        if(console) {
          console.log("Loaded SwaggerUI")
        }
        $('pre code').each(function(i, e) {hljs.highlightBlock(e)});
      },
      onFailure: function(data) {
        if(console) {
          console.log("Unable to Load SwaggerUI");
          console.log(data);
        }
      },
      docExpansion: "none"
    });

    $('#input_apiKey').change(function() {
      var key = $('#input_apiKey')[0].value;
      console.log("key: " + key);
      if(key && key.trim() != "") {
        console.log("added key " + key);
        window.authorizations.add("key", new ApiKeyAuthorization("api_key", key, "query"));
      }
    })
    window.swaggerUi.load();
  });

  </script>

Inline image 1

Srikanth Madduri


--
You received this message because you are subscribed to a topic in the Google Groups "Swagger" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/swagger-swaggersocket/zzgOFMWnUfM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to swagger-swaggers...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Screen Shot 2013-07-29 at 10.52.48 AM.png

Srikanth Madduri

unread,
Jul 29, 2013, 1:57:14 PM7/29/13
to swagger-sw...@googlegroups.com
This screenshot is the UI from non 2.0 for the same response. (Whatever that version is. I am not sure. It just says UI master when I downloaded the zip from git)

Also I gave the absolute base path in my web.xml for the cases.

Inline image 1

Srikanth Madduri
Screen Shot 2013-07-29 at 10.52.48 AM.png
Screen Shot 2013-07-29 at 10.55.14 AM.png

tony tam

unread,
Jul 29, 2013, 2:29:09 PM7/29/13
to swagger-sw...@googlegroups.com
Hi, can you jump on irc.freenode.net #swagger to chat?  Would make this troubleshooting process much faster.

Srikanth Madduri



Srikanth Madduri


To unsubscribe from this group and all its topics, send an email to swagger-swaggersocket+unsub...@googlegroups.com.

Srikanth Madduri

unread,
Jul 29, 2013, 2:34:36 PM7/29/13
to swagger-sw...@googlegroups.com
I am there.

Srikanth Madduri


To unsubscribe from this group and all its topics, send an email to swagger-swaggers...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages