Java – handling multiple benchmarks in swagger
I'm using swagger UI to provide good documentation of rest APIs for our clients
Documents available: http://www.myhost.com/xyz/dist/index.html https://www.myhost2.com/dist/index.html
web. The swagger API basepath in XML is:
<init-param> <param-name>swagger.api.basepath</param-name> <param-value>/rest</param-value> </init-param>
Question: I tried to use the trial feature on the document page The corresponding request URLs of the two hosts are as follows: http://www.myhost.com/rest/getAUser https://www.myhost2.com/rest/getAUser
It works for host2 because it is hitting the correct URL But it should be called for host1 http://www.myhost.com/xyz/rest/getAUser , but it's clicking on the URL http://www.myhost.com/rest/getAUser.
Is there any way to specify multiple baselines for different web sites
My swagger UI HTML looks 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",...... ...... }
Solution
I can solve this problem by configuring swagger using bean config instead of on the web Using servlets in XML
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); } }
And on the web In 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>
I changed my POM XML, start using the latest stable version of swagger-jersey y2-jaxrs:
<dependency> <groupId>io.swagger</groupId> <artifactId>swagger-jersey2-jaxrs</artifactId> <version>1.5.3</version> </dependency>