Swagger UI Not Loading
Try to copy the /dist directory in vendor/swagger-api/swagger-ui inside your project. I'm not sure about the proper way, but I was facing the same issue and it worked for me. Also, try to provide more details of the issue you are facing, maybe code snippets too.
Alternatively, try the following :
- Check if all your controller methods have [http] tag. If they all do and still doesn't work go to step 2
- In your configure function to ensure that you have app.UseStaticFiles(); If it still doesn't work go to step 3
- Uninstall and reinstall swagger. If it doesn't work go to step 4 (Core Only)
- If you are using Core Install Microsoft.AspNetCore.StaticFiles and reference it in your project.
Swagger ui is not loading swagger api json
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);
});
Swagger not loading - Failed to load API definition: Fetch error undefined
So after a lot of troubleshooting it came down to basically two things, but I feel that in general this could be helpful to someone else in the future so I'm posting an answer.
First- if ever your stuck with the aforementioned error the best way to actually see whats going on is by adding the following line to your Configure() method
app.UseDeveloperExceptionPage();
Now if you navigate to the 'swagger/v1/swagger.json' page you should see some more information which will point you in useful direction.
Second- now for me the error was something along the lines of
'Multiple operations with path 'some_path' and method 'GET' '
However these API were located inside of dependency libraries so I was unable to apply a solution at the point of definition. As a workaround I found that adding the following line to your ConfigureServices() method resolved the issue
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info { Title = "API WSVAP (WebSmartView)", Version = "v1" });
c.ResolveConflictingActions(apiDescriptions => apiDescriptions.First()); //This line
});
Finally- After all that I was able to generate a JSON file but still I wasn't able to pull up the UI. In order to get this working I had to alter the end point in Configure()
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("./v1/swagger.json", "My API V1"); //originally "./swagger/v1/swagger.json"
});
I'm not sure why this was necessary, although it may be worth noting the web application's virtual directory is hosted on IIS which might be having an effect.
NOTE: Navigating to swagger/v1/swagger.json will give you more details, for me it was causing issue due to undecorated action. This information is mentioned in comment by @MarkD
Hope this helps someone in the future.
Swagger UI not displaying when deploying API on IIS
You need to temporarily add the production clause in your condition before you can see the swagger in the production environment. See the yellow highlighted section in the attached image. env.IsProduction()
SwaggerUI is defaulting to version 2 when a Springboot project is generated using openapi generator cli jar
I guess you didnt C&P correctly.
I just setup a test project with your file and used openapi-generator-cli.jar
With the command:
java -jar openapi-generator-cli.jar generate -i stack.yml -g spring -p java8=true
I created the project and the Error class is showing up:
package org.openapitools.model;
import java.util.Objects;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonCreator;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import org.openapitools.jackson.nullable.JsonNullable;
import javax.validation.Valid;
import javax.validation.constraints.*;
/**
* Error
*/
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.SpringCodegen", date = "2022-04-05T08:34:53.094779300+02:00[Europe/Berlin]")
public class Error {
@JsonProperty("id")
private String id;
@JsonProperty("message")
private String message;
public Error id(String id) {
this.id = id;
return this;
}
/**
* Unique id to represent a type of error
* @return id
*/
@ApiModelProperty(example = "bad_request", value = "Unique id to represent a type of error")
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Error message(String message) {
this.message = message;
return this;
}
/**
* Meaningful message about what went wrong
* @return message
*/
@ApiModelProperty(example = "dealer id already exists", value = "Meaningful message about what went wrong")
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
@Override
public boolean equals(java.lang.Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Error error = (Error) o;
return Objects.equals(this.id, error.id) &&
Objects.equals(this.message, error.message);
}
@Override
public int hashCode() {
return Objects.hash(id, message);
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("class Error {\n");
sb.append(" id: ").append(toIndentedString(id)).append("\n");
sb.append(" message: ").append(toIndentedString(message)).append("\n");
sb.append("}");
return sb.toString();
}
/**
* Convert the given object to string with each line indented by 4 spaces
* (except the first line).
*/
private String toIndentedString(java.lang.Object o) {
if (o == null) {
return "null";
}
return o.toString().replace("\n", "\n ");
}
}
Moreover I was able to start and receive the schema:
Related Topics
How to Ask the Selenium-Webdriver to Wait for Few Seconds in Java
Determining If an Object Is of Primitive Type
What Is an Object's Hash Code If Hashcode() Is Not Overridden
Org.Apache.Tomcat.Util.Bcel.Classfile.Classformatexception: Invalid Byte Tag in Constant Pool: 15
How to Re-Run Failed Junit Tests Immediately
Why Does the Behavior of the Integer Constant Pool Change at 127
In Java How to Sort One List Based on Another
Java List.Contains(Object with Field Value Equal to X)
How to Convert Milliseconds to "Hh:Mm:Ss" Format
How to Split a Comma-Separated String
What Data Type to Use for Money in Java
How to Convert Long to Byte[] and Back in Java
Is It Wrong to Use Deprecated Methods or Classes in Java
What Is This Spring.Jpa.Open-In-View=True Property in Spring Boot
How to Parse Date String to Date
How to Determine the Class of a Generic Type