简体   繁体   中英

Swagger ui is not loading swagger api json

I have made api documention with the help of swagger-editor . Now I want see it graphically with the help of swagger-ui . I have deployed my json here https://powerful-escarpment-92284.herokuapp.com/swagger.json

Now When I am trying to access it then I am getting an error

Failed to load spec.

在此处输入图片说明

I solved this by setting this header in the response of swagger.json file.

app.get('/:file(*)', function(req, res, next){
    var file = req.params.file, path = __dirname + '/' + file;

    console.log('.');
    res.header('Access-Control-Allow-Origin', '*');

    res.download(path);
});

I was getting this error in ac# project, due to a problem with a controller I added. I could get more information by trying to browse to the swagger.json It turned out I was missing the routing attribute on a controller action.

After many exercises for me apparently, it helped just to serve a file with flask server [ How to serve static files in Flask . and (important) add "?" to the end of the url, ie http://127.0.0.1/js/swagger.json ?

The URL is not shown, so I assume if you access the swagger directly via "index.html" or any other way, the URL format must be like this.

http://<host>:<port>/<project_name>/_swagger-ui/index.html?url=http://<host>:<port>/<project_name>/<path>/swagger.json

like: http://localhost:8080/counter/_swagger-ui/index.html?url=http://localhost:8080/counter/webapi/swagger.json

I hope, it helps :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM