java – 在swagger中处理多个基准
我正在使用swagger-ui为我们的客户端提供REST API的良好文档.
在内部我们有两个不同的环境jenkin构建项目. 例如. swagger.json可以在两个环境中访问: http://www.myhost.com/xyz/rest/swagger.json https://www.myhost2.com/rest/swagger.json 文档可用: web.xml中的swagger api basepath是: <init-param> <param-name>swagger.api.basepath</param-name> <param-value>/rest</param-value> </init-param> 问题: 它适用于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> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |