Can't read swagger JSON from http://localhost:

2019-05-05 18:06发布

问题:

The api-docs was working fine earlier. It stopped working and now I get

Can't read swagger JSON from http://localhost:9000/api-docs/

If I change src/main/webapp/swagger-ui/index.html

var apiUrl = "http://localhost:8080/swagger-ui/index.html";

I get "Can't read from server. It may not have the appropriate access-control-origin settings."

回答1:

I ran into the same problem and found out the the generated classes have the package com.mycompany.myapp.config.apidoc but they actually are under com.mycompany.myapp.apidoc.

You can solve this by moving the java files to the correct package com.mycompany.myapp.config.apidoc.



回答2:

See Jerome's fix on https://github.com/jhipster/generator-jhipster/issues/277 by changing gruntfile.js to include below lines in connect/proxies section

,{
context: '/api-docs',
host: 'localhost',
port: 8080,
https: false,
changeOrigin: false
}


回答3:

You have to enable CORS for that sake just add these 3 res.header lines into your API then CORS will be enabled when your accessing that API so that you will not get error like access-control-origin settings.

app.get('/swaggerJson', function(req, res){  
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "Cache-Control, Pragma, Origin, Authorization, Content-Type, X-Requested-With");
  res.header("Access-Control-Allow-Methods", "GET, PUT, POST, OPTIONS");      
  res.json(swaggerJSON);
});

Hope this will help you..