Hello everybody,
I am trying to configure Swagger in an embedded application with REST API using jersey without web.xml.
I am expecting swagger to scrape the resources and return an appropriate JSON. Instead I am getting the following response in the browser:
{"apiVersion":"0.0","swaggerVersion":"1.2"}
I tried using BeanConfig for the swagger initialization without success. Obviously I am doing something wrong. If anybody worked on similar configuration, please any hint would be appreciated.
Thanks a lot.
Lokuma
I am using the following versions:
jax-rs-jersey v.2.6
swagger v1.3.12
embedded tomcat 7.0.47
These are the main components in configuring the swagger:
public class TomcatEmbeddedRunner {
private static final AcLogger AC_LOGGER = new AcLogger(TomcatEmbeddedRunner.class.getName());
private static final String ROOT_PATH = "/rest";
public static final String REST_VERSION = "2";
private static final String REST_VERSION_PATH = "/v2" + REST_VERSION;
private static final int DEFAULT_PORT = 4010;
/**
* Start the REST server.
* @throws LifecycleException
*/
public void startServer() throws LifecycleException {
Tomcat tomcat = new Tomcat();
tomcat.setPort(DEFAULT_PORT);
File base = new File(System.getProperty("java.io.tmpdir"));
Context rootCtx = tomcat.addContext(ROOT_PATH, base.getAbsolutePath());
try {
Tomcat.addServlet(rootCtx, "jersey-container-servlet", resourceConfig());
rootCtx.addServletMapping(REST_VERSION_PATH + "/*", "jersey-container-servlet");
} catch (Exception e) {
AC_LOGGER.logN(Level.SEVERE, e, "Unable to add servlet mapping for REST");
}
//specify the swagger configuration class
try {
Tomcat.addServlet(rootCtx, "SwaggerJaxrsConfig", aplicationConfig());
rootCtx.addServletMapping(REST_VERSION_PATH +"/swagger/*", "SwaggerJaxrsConfig");
} catch (Exception e) {
AC_LOGGER.logN(Level.SEVERE, e, "Unable to add swagger mapping for REST");
}
try {
tomcat.start();
} catch (Exception e) {
AC_LOGGER.logN(Level.SEVERE, e, "Unable to start Tomcat for REST");
}
AC_LOGGER.configN("Java REST v2 server is ready on port " + DEFAULT_PORT
+ "\n REST"
+ " v2 base URL = " + ROOT_PATH + REST_VERSION_PATH);
}
private ServletContainer resourceConfig() {
return new ServletContainer(new ResourceConfig(new ResourceLoader().getClasses()));
}
private ServletContainer aplicationConfig() {
return new ServletContainer(new ResourceConfig(new ApplicationConfig().getClasses()));
}
}
The applicationConfig() loads the resources:
@javax.ws.rs.ApplicationPath("api-docs")
public class ApplicationConfig extends Application {
private static final String RESOURCES_PKG = "com.services.api.rest.resources";
@Override
public Set<Class<?>> getClasses() {
final Set<Class<?>> resources = new HashSet<Class<?>>();
resources.add(com.wordnik.swagger.jaxrs.listing.ApiListingResource.class);
resources.add(com.wordnik.swagger.jaxrs.listing.ApiDeclarationProvider.class);
resources.add(com.wordnik.swagger.jaxrs.listing.ApiListingResourceJSON.class);
resources.add(com.wordnik.swagger.jaxrs.listing.ResourceListingProvider.class);
addRestResourceClasses(resources);
return resources;
}
/**
* Do not modify addRestResourceClasses() method.
* It is automatically populated with
* all resources defined in the project.
* If required, comment out calling this method in getClasses().
*/
private void addRestResourceClasses(Set<Class<?>> resources) {
resources.add(com.services.api.rest.resources.LoginResource.class);
}
The swagger servlet initialization. When I debugged the application I noticed the SwaggerJaxrsConfig servlet initialization is never called. Maybe that is why the scanner is not properly initialized.:
@WebServlet(name = "SwaggerJaxrsConfig", loadOnStartup = 2)
public class SwaggerJaxrsConfig extends HttpServlet {
@Override
public void init(ServletConfig servletConfig) {
try {
super.init(servletConfig);
SwaggerConfig swaggerConfig = new SwaggerConfig();
ConfigFactory.setConfig(swaggerConfig);
swaggerConfig.setApiVersion("2.0.0");
ScannerFactory.setScanner(new DefaultJaxrsScanner());
ClassReaders.setReader(new DefaultJaxrsApiReader());
} catch (ServletException e) {
System.out.println(e.getMessage());
}
}
}