can we move some swagger configuration from web.xml

前端 未结 2 724
攒了一身酷
攒了一身酷 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

            <dependency>
                <groupId>io.swagger</groupId>
                <artifactId>swagger-jersey2-jaxrs</artifactId>
                <version>1.5.0</version>
            </dependency>
            <dependency>
                <groupId>javax</groupId>
                <artifactId>javaee-api</artifactId>
                <version>7.0</version>
                <scope>provided</scope>
            </dependency>
    

    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

    <web-app id="WebApp_ID" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>Restful Web Application</display-name>
    
    <servlet>
        <servlet-name>jersey-serlvet</servlet-name>
        <servlet-class>
                     com.sun.jersey.spi.container.servlet.ServletContainer
                </servlet-class>
        <init-param>
             <param-name>com.sun.jersey.config.property.packages</param-name>
             <param-value>io.swagger.jaxrs.listing,com.ibm.api</param-value>
        </init-param>
        <init-param>
            <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
            <param-value>true</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>jersey-serlvet</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
    
    <servlet>
        <servlet-name>SwaggerBootstrap</servlet-name>
        <servlet-class>com.ibm.api.SwaggerConfigReader</servlet-class>
        <init-param>            
            <param-name>scan.all.resources</param-name>
            <param-value>true</param-value>
        </init-param>
        <load-on-startup>2</load-on-startup>
    </servlet>
    </web-app>
    

    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

    0 讨论(0)
  • 2020-12-20 11:10

    There's a thread explaining how to do this on the Swagger google group.

    Basically, in Swagger 1.3, you need to use the SwaggerConfig class, like so:

    SwaggerConfig config = new SwaggerConfig();
    config.setBasePath(yourBasePathVariable);
    ConfigFactory.setConfig(config);
    

    However, you need this to occur after Swagger loads and sets the default basePath, because otherwise (if your basePath gets set first) it will be overwritten.

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