can we move some swagger configuration from web.xml

前端 未结 2 739
攒了一身酷
攒了一身酷 2020-12-20 10:05

In swagger 1.2.9-1.2.3 or old versions we have config reader com.wordnik.swagger.jaxrs.ConfigReader class, we can extend this class and we can declare swagger

2条回答
  •  清歌不尽
    2020-12-20 10:57

    Here is how you can move swagger configuration to a customization code This is particularly helpful if you are hosting your micro service on cloud foundry or IBM Bluemix or Some PaaS Cloud solution

    Dependency in your pom.xml

            
                io.swagger
                swagger-jersey2-jaxrs
                1.5.0
            
            
                javax
                javaee-api
                7.0
                provided
            
    

    Swagger configuration

    package com.ibm.api;
    
    import javax.servlet.ServletConfig;
    import javax.servlet.ServletException;
    
    import io.swagger.jaxrs.config.BeanConfig;
    import io.swagger.jaxrs.config.DefaultJaxrsConfig;
    
    public class SwaggerConfigReader extends DefaultJaxrsConfig {
    
    
    
        /**
         * 
         */
        private static final long serialVersionUID = 1638783798880874518L;
    
        @Override
        public void init(ServletConfig config) throws ServletException {
    
            super.init(config);
            //contextPath will be null for host2 and /xyz for host1.
            String contextPath = config.getServletContext().getContextPath();
    
            BeanConfig beanConfig = new BeanConfig();
            beanConfig.setVersion("1.0.0");
            beanConfig.setTitle(Result.IMPLEMENTATION + " API Documentation");
            beanConfig.setSchemes(new String[] {
                    "http", "https"
            });
            beanConfig
            .setResourcePackage("com.ibm.api");
    
            beanConfig.setBasePath(contextPath + "/rest");
            beanConfig.setScan(true);
        }
    }
    

    Web.xml entries

    
    Restful Web Application
    
    
        jersey-serlvet
        
                     com.sun.jersey.spi.container.servlet.ServletContainer
                
        
             com.sun.jersey.config.property.packages
             io.swagger.jaxrs.listing,com.ibm.api
        
        
            com.sun.jersey.api.json.POJOMappingFeature
            true
        
        1
    
    
    
        jersey-serlvet
        /rest/*
    
    
    
        SwaggerBootstrap
        com.ibm.api.SwaggerConfigReader
                    
            scan.all.resources
            true
        
        2
    
    
    

    Note the com.ibm.api.SwaggerConfigReader registered as swagger configuration and com.ibm.api registered in packages to be scanned for rest APIs. Also note beanConfig.setBasePath(contextPath + "/rest");

    Now your swagger.JSON configuration will show up as http://localhost:8080/jax_rs/rest/swagger.json. Point swagger UI to this URL and you will see swagger documentation.

    The code is here: https://github.com/sanketsw/jax_rs_REST_Example

提交回复
热议问题