In the application.properties of Spring Boot
springdoc.swagger-ui.path=/swagger-ui-custom.html
in your case it will be
springdoc.swagger-ui.path=/myapi/swagger-ui.html
Answer from Anil Dabas on Stack OverflowIn the application.properties of Spring Boot
springdoc.swagger-ui.path=/swagger-ui-custom.html
in your case it will be
springdoc.swagger-ui.path=/myapi/swagger-ui.html
if for some reason you don't want redirect to /swagger-ui.html you can set itself contents as home view, setting an index.html at resources/static/index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Welcome to another awesome Microservice</title>
</head>
<body>
<script>
document.body.innerHTML = '<object type="text/html" data="/swagger-ui.html" style="overflow:hidden;overflow-x:hidden;overflow-y:hidden;height:100%;width:100%;position:absolute;top:0px;left:0px;right:0px;bottom:0px"></object>';
</script>
</body>
</html>
then accessing to your http://localhost:8080/ you will see your swagger docs.
finally you can customize path and .html file using:
registry.addViewController("/swagger").setViewName("forward:/index.html");
like suggests this answer
Change default API spec URL in Swagger UI - Stack Overflow
c# - How do I change the Swagger default URL and use a custom one? - Stack Overflow
default url in swagger UI
Default swagger UI to current instance - Swagger - ServiceStack Customer Forums
I found the solution to this issue:
In the Configure section of Startup.cs I did the following:
First I added the folowing variable:
private readonly string swaggerBasePath = "api/app";
Next I configured the path using UseSwagger and UseSwaggerUI to use the swaggerBasePath variable:
app.UseSwagger(c =>
{
c.RouteTemplate = swaggerBasePath+"/swagger/{documentName}/swagger.json";
});
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint($"/{swaggerBasePath}/swagger/v1/swagger.json", $"APP API - {version}");
c.RoutePrefix = $"{swaggerBasePath}/swagger";
});
Finally, I modified launchSettings.json to point to the new base path:
"launchUrl": "api/app/swagger",
Then is was able to hit the Swagger page using:
https://localhost/api/app/swagger/index.html
I testing this with Nginx and it was able to route to the correct container.
I can easily tweak the base path (for instance to add the API version number) by simply modifying the swaggerBasePath variable and tweaking the launchSettings.json file to match the value of the variable.
Hopefully, this will help someone in the future.
(I am using .Net 6)
I needed this because I was facing issue in api gateway. So, What I did, I left my launch settings as it is.
launchsettings
"launchUrl": "studentservice/api/swagger",
I made these two changes in my Startup.cs file.
Note: Your launchsettings url and routeprefix url should match.
- In
ConfigureService Method, I have added SwaggerDoc
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("student", new OpenApiInfo { Title = "Student Service Api", Version = "1.0" });
}
- In
Configure Method, I have added SwaggerDoc
app.UseSwagger();
// Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.)
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/student/swagger.json", "Student Services Api");
c.RoutePrefix = "studentservice/api/swagger";
});
}
