Setting the Api Version with Swagger UI

后端 未结 3 913
醉酒成梦
醉酒成梦 2020-12-19 22:21

I have a REST API developed through the use of Jersey and we document the REST API through swagger-ui. Unfortunately, we did not start versioning the API from day 1. We are

相关标签:
3条回答
  • 2020-12-19 22:50

    It's 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();
            }
        }
    
    0 讨论(0)
  • 2020-12-19 22:55

    You can add a Bootstrap servlet to set parameters for the Swagger config bean as described here -

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

    0 讨论(0)
  • 2020-12-19 23:11

    API version displayed at the bottom of the Swagger UI is coming from the Swagger document.

    Here is an example Swagger document:

    {
        "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 as per the Swagger Wiki page:

    The annotation can be on any class scanned during the Swagger auto-configuration process, i.e. it does not have to be on a JAX-RS API class but could just be on a marker/config interface

    You can find some samples here: https://github.com/swagger-api/swagger-samples/tree/master/java. Some are using Jersey and setting the API version.

    0 讨论(0)
提交回复
热议问题