java – 在swagger中处理多个基准

前端之家收集整理的这篇文章主要介绍了java – 在swagger中处理多个基准前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用swagger-ui为我们的客户端提供REST API的良好文档.
在内部我们有两个不同的环境jenkin构建项目.
例如. swagger.json可以在两个环境中访问:
http://www.myhost.com/xyz/rest/swagger.json
https://www.myhost2.com/rest/swagger.json

文档可用:
http://www.myhost.com/xyz/dist/index.html
https://www.myhost2.com/dist/index.html

web.xml中的swagger api basepath是:

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

问题:
我试图在文档页面上使用“试用”功能.
两个主机的相应请求URL如下:
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.

有没有办法为不同的网址指定多个基准线.

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

$(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配置swagger来解决这个问题,而不是在web.xml中使用Servlet

BeanConfig类:

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

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

<dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-jersey2-jaxrs</artifactId>
            <version>1.5.3</version>
        </dependency>
原文链接:https://www.f2er.com/java/124016.html

猜你在找的Java相关文章