Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can the base URL change in Swagger?

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?

like image 988
CodingPadawan Avatar asked Sep 11 '14 21:09

CodingPadawan


People also ask

How do I change my Swagger URL?

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.

What is the default URL for swagger UI?

By default, Swagger UI is accessible at /q/swagger-ui .

What is base URL in REST API?

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.

Can we customize swagger UI?

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.


2 Answers

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!

like image 133
kgaekwad Avatar answered Sep 29 '22 06:09

kgaekwad


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";
});
like image 42
Mop So Avatar answered Sep 29 '22 06:09

Mop So