本文介绍了我们可以从 web.xml 中移动一些招摇的配置吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 swagger 1.2.9-1.2.3 或旧版本中,我们有配置阅读器 com.wordnik.swagger.jaxrs.ConfigReader 类,我们可以扩展这个类,我们可以声明 swagger 属性 swagger.api.basepath 、 api.version 、 swagger.version 等

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.

但是在 swagger 2.10-1.3.0 的当前版本中,这个类不存在.有什么方法可以从 web.xml 中移动以上配置,我想将它们放在属性文件中,而不是在 web.xml 中硬编码.

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.

提前致谢.

推荐答案

以下是如何将 swagger 配置移动到自定义代码如果您将微服务托管在 Cloud Foundry 或 IBM Bluemix 或某些 PaaS 云解决方案上,这将特别有用

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

        <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 配置

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 条目

<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>

注意 com.ibm.api.SwaggerConfigReader 注册为 swagger 配置和 com.ibm.api 注册在要扫描其余 API 的包中.还要注意 beanConfig.setBasePath(contextPath + "/rest");

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");

现在您的 swagger.JSON 配置将显示为 http://localhost:8080/jax_rs/rest/swagger.json.将 swagger UI 指向此 URL,您将看到 swagger 文档.

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.

代码在这里:https://github.com/sanketsw/jax_rs_REST_Example

这篇关于我们可以从 web.xml 中移动一些招摇的配置吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 18:20