大摇大摆地处理多个基本路径

Handling multiple basepath in swagger(大摇大摆地处理多个基本路径)

本文介绍了大摇大摆地处理多个基本路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 swagger-ui 为我们的客户提供很好的 REST API 文档.在内部,我们有两个不同的环境 jenkin 构建项目.例如.swagger.json 在两种环境中都可以访问:<代码>http://www.myhost.com/xyz/rest/swagger.jsonhttps://www.myhost2.com/rest/swagger.json

I am using swagger-ui to provide nice documentation for REST APIs to our clients. Internally we have two different environments jenkin builds the project to. E.g. swagger.json is accessible on both environment as: http://www.myhost.com/xyz/rest/swagger.json https://www.myhost2.com/rest/swagger.json

文档可用于:<代码>http://www.myhost.com/xyz/dist/index.htmlhttps://www.myhost2.com/dist/index.html

web.xml 中的 swagger api basepath 是:

swagger api basepath in web.xml is:

<init-param>       
     <param-name>swagger.api.basepath</param-name>
     <param-value>/rest</param-value>
</init-param>

问题:我正在尝试使用文档页面上的试用"功能.两个主机各自的请求 url 如下:<代码>http://www.myhost.com/rest/getAUserhttps://www.myhost2.com/rest/getAUser

ISSUE: I am trying to use "Try it out" feature on documentation page. The respective request url for both hosts are as follows: http://www.myhost.com/rest/getAUser https://www.myhost2.com/rest/getAUser

它适用于 host2,因为它访问了正确的 url.但是,它应该为 host1 命中 http://www.myhost.com/xyz/rest/getAUser 但它正在命中 url http://www.myhost.com/rest/getAUser.

It works for host2 as it is hitting the correct url. However it should have hit http://www.myhost.com/xyz/rest/getAUser for host1 but it is hitting the url http://www.myhost.com/rest/getAUser.

有没有一种方法可以为不同的 url 指定多个基本路径.

Is there a way I can specify multiple basepath for different urls.

我的 swagger-ui html 看起来像这样.

My swagger-ui html looks something like this.

$(function () {
var href = window.location.href;
var url = href.substring(0, href.lastIndexOf("/dist"));
console.log(url);
// Pre load translate...
if(window.SwaggerTranslator) {
window.SwaggerTranslator.translate();
}
window.swaggerUi = new SwaggerUi({
url: url + "/rest/swagger.json",
dom_id: "swagger-ui-container",
......
......
}

推荐答案

我能够通过使用 BeanConfig 而不是在 web.xml 中使用 Servlet 配置 swagger 来解决这个问题

I was able to resolve this issue by configuring swagger using BeanConfig instead of using Servlet in web.xml

BeanConfig 类:

BeanConfig class:

public class SwaggerBootstrap extends DefaultJaxrsConfig {

    /**
     *
     */
    private static final long serialVersionUID = myAutoGeneratedID;

    @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("My API Documentation");
        beanConfig.setSchemes(new String[] {
                "http", "https"
        });
        beanConfig
        .setResourcePackage("com.example.my.rest.api.package");

        beanConfig.setBasePath(contextPath + "/rest");
        beanConfig.setScan(true);
    }
}

在 web.xml 中:

and in web.xml:

<servlet>
        <servlet-name>SwaggerBootstrap</servlet-name>
        <servlet-class>my.package.to.SwaggerBootstrap</servlet-class>
        <init-param>
            <!-- This make sure that all resources are scanned whether or not they use Swagger Annotations. 
            https://github.com/swagger-api/swagger-samples/tree/master/java/java-jaxrs-no-annotations -->
            <param-name>scan.all.resources</param-name>
            <param-value>true</param-value>
        </init-param>
        <load-on-startup>2</load-on-startup>
    </servlet>

我更改了我的 pom.xml 以开始使用最新的稳定版本的 swagger-jersey2-jaxrs:

And I changed my pom.xml to start using latest stable version of swagger-jersey2-jaxrs:

<dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-jersey2-jaxrs</artifactId>
            <version>1.5.3</version>
        </dependency>

这篇关于大摇大摆地处理多个基本路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:大摇大摆地处理多个基本路径

基础教程推荐