can we move some swagger configuration from web.xml

我与影子孤独终老i 提交于 2019-12-18 07:22:25

问题


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 properties swagger.api.basepath , api.version , swagger.version etc.

But in current version of swagger 2.10-1.3.0 this class is not present. Is there any way we can move above configurations from web.xml, I want to have them in property file instead of hard coding it in web.xml.

Thanks in advance.


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/18610416/can-we-move-some-swagger-configuration-from-web-xml

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!