Swagger-ui: Can't read swagger JSON (Swagger 2.0 case)

Created on 5 May 2015  路  7Comments  路  Source: swagger-api/swagger-ui

I have read other issues related to this but can't fix my system.
I am working with Swagger 2.0 which has a single file swagger.json to bring up Swagger UI. The code for Swagger UI (including index.html) is packaged inside a JAR and deployed on the server. The swagger.json file however is outside the JAR but is in the class path.
When I hit the URL : http://IP:port/index.html it gives me the error "Can't read swagger JSON".
The IP mentioned is the server on which all the files and JAR is present.

I am using Spring-boot in my setup and have configured:
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/ApiDoc.json").addResourceLocations("file:localhost://usr/local/brs/lib/restserver/");
registry.addResourceHandler("/index.html").addResourceLocations("classpath:/static/");
}

support

Most helpful comment

As you know index.html in Swagger UI points to the URL for the master json file (ApiDoc.json in my case), when I hit http://IP:port/index.html, the browser tries to lookup for http://IP:port/ApiDoc.json in order to load the content from the JSON in the html page. But it fails saying "Can't read swagger JSON from http://IP:port/ApiDoc.json". I have tried to hit URL http://IP:port/ApiDoc.json separately, it doesnt work too.

So the problem is that my application is not able to access ApiDoc.json (it probably looking inside the packaged JAR)

All 7 comments

I would expect http://IP:port/index.html to give you an html file and not a json one. Not sure where your swagger definition is hosted though. Perhaps http://IP:port/ApiDoc.json? First try to make sure you can open the JSON file from the URL in your browser, then configure the UI to load that URL.

As you know index.html in Swagger UI points to the URL for the master json file (ApiDoc.json in my case), when I hit http://IP:port/index.html, the browser tries to lookup for http://IP:port/ApiDoc.json in order to load the content from the JSON in the html page. But it fails saying "Can't read swagger JSON from http://IP:port/ApiDoc.json". I have tried to hit URL http://IP:port/ApiDoc.json separately, it doesnt work too.

So the problem is that my application is not able to access ApiDoc.json (it probably looking inside the packaged JAR)

Well, if your application can't access the ApiDoc.json, it's not really a swagger-ui issue. Since I don't know how you generated that json, nor how you packaged your application and how you host that file, I'm not sure how I can help you.

i am suspecting that this is an access issue only. I have given details of my setup in my very first comment. I wanted to confirm through this discussion as to whether it could be some other problem ? The generated JSON is a valid one though.... i checked using Swagger Editor.

As people on this community are working on Swagger regularly I thought I could get help. Any help is greatly appreciated.

I have tried to hit URL http://IP:port/ApiDoc.json separately, it doesnt work too.

Right, this is an access issue, but not for swagger-ui but for your application. That code snippet alone is not enough to help you with, and it's also not a swagger-ui issue.

Alright I ll post it in StackOverflow.
Thanks!

I also have this problem. I am using a sample Eclipse project and it worked fine before, but after making a working copy and changing the name I ran into this as well. Please let me know your Stack Overflow link since they weren't able to solve the problem here.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

fehguy picture fehguy  路  3Comments

DavidBiesack picture DavidBiesack  路  4Comments

ankon picture ankon  路  4Comments

zilongl picture zilongl  路  3Comments

sgyang picture sgyang  路  4Comments