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
Here is how you can move swagger configuration to a customization code This is particularly helpful if you are hosting your micro service on cloud foundry or IBM Bluemix or Some PaaS Cloud solution
io.swagger
swagger-jersey2-jaxrs
1.5.0
javax
javaee-api
7.0
provided
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);
}
}
Restful Web Application
jersey-serlvet
com.sun.jersey.spi.container.servlet.ServletContainer
com.sun.jersey.config.property.packages
io.swagger.jaxrs.listing,com.ibm.api
com.sun.jersey.api.json.POJOMappingFeature
true
1
jersey-serlvet
/rest/*
SwaggerBootstrap
com.ibm.api.SwaggerConfigReader
scan.all.resources
true
2
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");
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.
The code is here: https://github.com/sanketsw/jax_rs_REST_Example