Installing the Api Version with the Swagger Interface

I have a REST API developed using Jersey and we document the REST API through swagger-ui. Unfortunately, we did not start versioning the API from day one. Now we are trying to add version control to the API.

The first step I'm taking is trying to update the version of the API that appears on the dynamically generated swagger (html) page. I traced the call flow to the swagger-ui.js file, but I cannot figure out how to change the displayed version of the API at the bottom of the dynamically generated page.

The default, which is currently displayed below, is "API VERSION: 1.0.0".

I read something about ServiceStack here , but unfortunately the code base I'm working on doesn't use anything like that.

Can anyone wish to tell me where / what I need to change / update in order to update the displayed API version number?

+2
source share
3 answers

You can add the Bootstrap servlet to set the parameters for the configuration of the Swagger bean, as described here -

https://github.com/swagger-api/swagger-core/wiki/Swagger-Core-Jersey-2.X-Project-Setup-1.5

+1
source

The version of the API displayed at the bottom of the Swagger interface comes from a Swagger document.

Here is an example Swagger doc:

{ "swagger": "2.0", "info": { "description": "This is a sample server Petstore server.", "version": "1.0.0", "title": "Swagger Petstore", ... 

"version": "1.0.0" is the default value, but you can change it using the Swagger @Info annotation:

 @SwaggerDefinition( info = @Info( description = "This is a sample server Petstore server.", version = "1.0.1", title = "Swagger Petstore" 

This document can be added to any class scanned during the Swagger auto-configuration process, according to the Swagger Wiki page :

An annotation can be in any class scanned during the Swagger automatic configuration process, i.e. it should not be in the JAX-RS API class, but may just be in the token / configuration interface

Here you can find some examples: https://github.com/swagger-api/swagger-samples/tree/master/java . Some use jersey and install an API version.

+2
source

This is pretty straight forward -

 1. Add a servlet to set the Swagger Bootstrap properties in your deployment descriptior file. <servlet> <servlet-name>SwaggerBootstrap</servlet-name> <servlet-class>com.example.util.SwaggerBootstrap</servlet-class> <init-param> <description>URL Pattern Mapping</description> <param-name>paramName</param-name> <param-value>uri value</param-value> </init-param> <load-on-startup>2</load-on-startup> </servlet> 2. Create a servlet and set the Bean properties as below -- public void init(ServletConfig servletConfig) { try { // Setting the BeanConfig for start-up page BeanConfig bean = new BeanConfig(); bean.setScan(true); bean.setResourcePackage("com.example.util"); bean.setBasePath("yourBasePath")); bean.setVersion("1.0"); bean.setTitle("title")); bean.setDescription("description"); } catch (IOException e) { e.printStackTrace(); } } 
+1
source

Source: https://habr.com/ru/post/1246834/


All Articles