Swagger UI hangs on "fetching resource list"

7,715 views
Skip to first unread message

Sahar

unread,
Jul 22, 2013, 4:30:14 PM7/22/13
to swagger-sw...@googlegroups.com
Hi all, 

I'm trying to use Swagger for our RESTful web services documentation. I am able to generate the resource listing and  API decl; however, I cannot make the Swagger UI work. I have downloaded the Swagger UI files and changed the discoveryURL  in the HTML file  to point to the resource listing. When I run the UI, it hangs on fetching resource list. I used chrome developer tools and I can see that the network response to get the "resource" is 200 OK, but I don't know why nothing changes on the UI.

SwaggerTest is the name of the app and the ws path is "/samplehub/sample/insert"

The Resource Listing 
{
"apiVersion":"1.0",
"apis":{"description":"",
"path":"/resources/samplehub/sample/insert"},
"basePath":"http://localhost:8080",
"swaggerVersion":"1.1"
}

The API Decl
{"apiVersion":"1.0","apis":{"description":"Inserts a sample and retruns a SAM_ID","operations":{"httpMethod":"GET","nickname":"insertSample","notes":"Validates and inserts the sample data","parameters":[{"allowMultiple":"false","dataType":"string","description":"Name of the inventory","name":"inventory_name","paramType":"query","required":"true"},{"allowMultiple":"false","dataType":"string","description":"ID of the inventory lot","name":"inventory_lot_id","paramType":"query","required":"true"},{"allowMultiple":"false","dataType":"string","description":"ID of the project","name":"project_id","paramType":"query","required":"true"},{"allowMultiple":"false","dataType":"string","description":"Name of the lab","name":"lab","paramType":"query","required":"true"},{"allowMultiple":"false","dataType":"string","description":"well ID","name":"well_id","paramType":"query","required":"true"},{"allowMultiple":"false","dataType":"string","description":"Label of the sample","name":"label","paramType":"query","required":"false"},{"allowMultiple":"false","dataType":"string","description":"Unix ID of the user","name":"login","paramType":"query","required":"true"},{"allowMultiple":"false","dataType":"string","description":"ID of the treatment","name":"treatment_id","paramType":"query","required":"true"},{"allowMultiple":"false","dataType":"string","description":"Sample preparation method","name":"sample_prep","paramType":"query","required":"false"},{"allowMultiple":"false","dataType":"string","description":"Sample extraction type","name":"extraction_type","paramType":"query","required":"true"},{"allowMultiple":"false","dataType":"float","description":"Concentration of the sample","name":"concentration","paramType":"query","required":"false"},{"allowMultiple":"false","dataType":"float","description":"Volume of the sample","name":"volume","paramType":"query","required":"false"},{"allowMultiple":"false","dataType":"string","description":"User comments","name":"comments","paramType":"query","required":"false"}],"responseClass":"sample_hub","summary":"Inserts a sample"},"path":"/samplehub/sample/insert"},"basePath":"http://localhost:8080","models":null,"resourcePath":"/samplehub/sample/insert","swaggerVersion":"1.1"}

Modified part of Swagger UI HTML file

 

$(function () {

    window.swaggerUi = new SwaggerUi({

    discoveryUrl:"http://localhost:8080/SwaggerTest/resources",

    dom_id:"swagger-ui-container",

    supportHeaderParams: true,

    supportedSubmitMethods: ['get', 'post', 'put'],


...
}

I'd appreciate your help.
Thanks,
Sahar

tony tam

unread,
Jul 22, 2013, 4:35:37 PM7/22/13
to swagger-sw...@googlegroups.com
Hi, looks to me like your base path is not configured correctly.  The UI is trying to connect to basePath + path.  That this api declaration (from your json) should respond to this:


I believe from your post you have your app mounted here:

http://localhost:8080/SwaggerTest/resources

which means, you're missing "SwaggerTest" in your setup.

Can you confirm that?  What server integration, if any, are you using?

Sahar

unread,
Jul 22, 2013, 4:44:24 PM7/22/13
to swagger-sw...@googlegroups.com
My app is mounted at:


resources is the in fact resources.json (the resource listing), but I removed the .json following the tutorial.

I'm using Java JAX-RS and this is my web.xml

<web-app>

<display-name>Swagger Test Web Application</display-name>

<servlet>

<servlet-name>Bootstrap</servlet-name>

<servlet-class>com.genentech.bioinformatics.swaggertest.Bootstrap</servlet-class>

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

</servlet>

<servlet>

<servlet-name>Jersey Web Application </servlet-name>

<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>

<init-param>

<param-name>com.sun.jersey.config.property.resourceConfigClass</param-name>

<param-value>com.sun.jersey.api.core.PackagesResourceConfig</param-value>

</init-param>


<init-param>

<param-name>com.sun.jersey.config.property.packages</param-name>

<param-value>com.genentech.bioinformatics.swaggertest;com.wordnik.swagger.jaxrs.listing</param-value>

</init-param>

<init-param>

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

<param-value>http://localhost:8080</param-value>

</init-param>

<init-param>

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

<param-value>1.0</param-value>

</init-param>

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

</servlet>

<servlet-mapping>

<servlet-name>Jersey Web Application</servlet-name>

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

</servlet-mapping>


</web-app>



On Monday, July 22, 2013 1:30:14 PM UTC-7, Sahar wrote:

tony tam

unread,
Jul 22, 2013, 4:46:29 PM7/22/13
to swagger-sw...@googlegroups.com
Looks like you need to change this:

<init-param>

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

  <param-value>http://localhost:8080</param-value>

</init-param>


to this:

<init-param>

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

  <param-value>http://localhost:8080/SwaggerTest</param-value>

</init-param>

Sahar

unread,
Jul 22, 2013, 4:59:37 PM7/22/13
to swagger-sw...@googlegroups.com
Thanks. I changed it but the UI still hangs although I have:

{"apiVersion":"1.0","apis":
{"description":"",
"path":"/resources/samplehub/sample/insert"},
"basePath":"http://localhost:8080/SwaggerTest",
"swaggerVersion":"1.1"}



On Monday, July 22, 2013 1:30:14 PM UTC-7, Sahar wrote:

tony tam

unread,
Jul 22, 2013, 5:04:48 PM7/22/13
to swagger-sw...@googlegroups.com
This is one of the most irritating problems when integrating with swagger, and it is addressed in the next version of the spec.  However, to sort this out, I believe we can find the cause if you use chrome, open the developer tools, and show the "network" tab when loading the page.  Something is off, either a CORS or 404 issue.

Can you do that, take a screenshot of that section, and post it back?

Sahar

unread,
Jul 22, 2013, 5:14:13 PM7/22/13
to swagger-sw...@googlegroups.com

Attached is the screen shot ...


On Monday, July 22, 
Screen shot 2013-07-22 at 2.11.13 PM (2).jpg

Sahar

unread,
Jul 22, 2013, 5:50:22 PM7/22/13
to swagger-sw...@googlegroups.com
Thanks for your quick responses Tony, but I'm wondering if the problem can be solved. 

tony tam

unread,
Jul 22, 2013, 7:50:41 PM7/22/13
to swagger-sw...@googlegroups.com
Hi Sahar, can you join #swagger in irc.freenode.net so we can sort this out?  My irc handle is tonytam

tony tam

unread,
Jul 22, 2013, 8:53:52 PM7/22/13
to swagger-sw...@googlegroups.com

Hi Sahar,

I swear the same issue has come up a million times, and I have yet to detect it right away.  The issue is very simple--your JSON mapper isn't properly configured.  You can see why like this.

if you look at your /resources, it looks like such:

{

  "apiVersion": "1.0",

  "apis": {

    "description": "",

    "path": "/resources/samplehub/sample/insert"

  },

  "basePath": "http://localhost:8080",

  "swaggerVersion": "1.1"

}

the `apis` element is actually an array.  You are hitting the dreadful "single-element array bug" that is built into most versions of Jersey (some say it's not a bug, but they probably haven't had to troubleshoot it much).  If you were to add just another @Api resource, you'd see it turn into an array:

{

  "apiVersion": "1.0",

  "apis": [

    {

      "description": "",

      "path": "/resources/samplehub/sample/insert"

    },

    {

      "description": "",

      "path": "/resources/samplehub/sample/somethingElse"

    }

  ],

  "basePath": "http://localhost:8080",

  "swaggerVersion": "1.1"

}

The same goes for your api declaration.  Note the `parameters` are in an array, but the `apis` are not.

The solution is simple, configure Jackson to render the JSON like in this example:

https://github.com/wordnik/swagger-core/blob/master/samples/java-jaxrs/src/main/java/com/wordnik/swagger/sample/util/JacksonJsonProvider.java

And life will be good.


Side note--this is one of the issues addressed in the 1.3 release.  We render the JSON ourselves so you don't have to monkey with your default JSON mapper.  There's always the off chance that you want arrays with one element to be treated like this.

Sahar

unread,
Jul 23, 2013, 12:57:43 PM7/23/13
to swagger-sw...@googlegroups.com
Hi Tony,

I used the class in the example and it works now. Thank you so much! 

Now that it's working, I came across another question. My web service response is in xml format and when I run the ws it produces xml; however, the Response Content Type on the UI is application/json. Is it possible to change that? 

Thanks again,
Sahar

tony tam

unread,
Jul 23, 2013, 1:09:06 PM7/23/13
to swagger-sw...@googlegroups.com

In the new version of swagger, yes:

https://github.com/wordnik/swagger-core/blob/1.3-RC2/samples/java-jaxrs/src/main/java/com/wordnik/swagger/sample/resource/PetResource.java#L29

the @Produces annotations are picked up and you'll get the appropriate drop-down in the response type:

http://petstore.swagger.wordnik.com/#!/pet/getPetById_get_0

In the one you're on, it's a bit more work.

Glad it's working!

Sahar

unread,
Jul 23, 2013, 1:15:25 PM7/23/13
to swagger-sw...@googlegroups.com
Ok, Great! Thanks.

Bipin Kumar

unread,
May 5, 2016, 8:19:33 AM5/5/16
to Swagger
I am not able to access the above URL "page not found"
Reply all
Reply to author
Forward
0 new messages