I currently have implemented Swagger and I've noticed that the base url for resources is hardcoded in the JSON resource files, ideally I would like to give the user the capability to change the base url for different json verbages. So, for example, give them the capability to submit a get from one environment and a put from another on the same page since I'm working with multiple environments and otherwise they'd have to alter the JSON in every one of their resources each time they want to use a new environment. Does anyone know if this is possible?
You can change default swagger-ui path programmatically using ApplicationListener<ApplicationPreparedEvent> . The idea is simple - override springdoc. swagger-ui. path=/custom/path before your Spring Boot application starts.
By default, Swagger UI is accessible at /q/swagger-ui .
REST APIs have a base URL to which the endpoint paths are appended. The base URL is defined by schemes , host and basePath on the root level of the API specification.
By default, Swagger UI uses BaseLayout , which is built into the application. You can specify a different layout to be used by passing the layout's name as the layout parameter to Swagger UI. Be sure to provide your custom layout as a component to Swagger UI.
This is possible, and I implement this the following way:
In swaggerui, I programmatically declare my url by fetching the url via javascript:
// Get the url:
theUrl = window.location.protocol+"//" + window.location.host+"/docs";
window.swaggerUi = new SwaggerUi({
url: theUrl,..other parameters...})
In my json files, where I declare a resource, I just declare my basepath as a "/" as shown below:
{
"apiVersion": "1.0.0",
"swaggerVersion": "1.2",
"basePath": "/",
"resourcePath": "/api/myapi".......
Hope that helps!
To change dynamically the hostname (and override the value from JSON file) of the targeted server (where the REST request is sent) :
$.each(window.swaggerUi.api.apis, function(key, val) {
window.swaggerUi.api.apis[key].basePath = "http://target:port";
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With