I am very new to swagger. I have created a sample JAX-RS project in which I have integrated the swagger 2.0 documentation. After a lot of unsuccessful tries, I am still not able to figure out how will I migrate my swagger 2.0 documentation to swagger 3.0. Also, what is the easiest way to host the swagger UI in my project?
So, in short, I have 2 question
1) How do I migrate this to swagger 3.0 ?
2) What is the easiest way to self-host the swagger UI in this project?
Below the sample code for my project's main files for reference. I would really appreciate the help. Thank you so much!!!
==================== pom.xml File ====================
<!-- Swagger -->
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>1.5.18</version>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-jersey2-jaxrs</artifactId>
<version>1.5.18</version>
</dependency>
==================== web.xml File ====================
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Starbucks Web Application</display-name>
<filter>
<filter-name>guiceFilter</filter-name>
<filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>guiceFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
==================== Main App File ====================
package com.starbucks.app;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Module;
import com.google.inject.Stage;
import com.google.inject.servlet.ServletModule;
import com.starbucks.cors.SimpleCorsFilter;
import com.starbucks.guice.ApiBaseModule;
import com.starbucks.request.ObjectMapperContextResolver;
import io.swagger.jaxrs.config.BeanConfig;
import io.swagger.jaxrs.listing.ApiListingResource;
import io.swagger.jaxrs.listing.SwaggerSerializers;
import org.glassfish.hk2.api.ServiceLocator;
import org.glassfish.jersey.jackson.JacksonFeature;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.ServerProperties;
import org.jvnet.hk2.guice.bridge.api.GuiceBridge;
import org.jvnet.hk2.guice.bridge.api.GuiceIntoHK2Bridge;
import javax.inject.Inject;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.HttpMethod;
import javax.ws.rs.core.HttpHeaders;
@ApplicationPath("api")
public class App extends ResourceConfig {
@Inject
public App(final ServiceLocator serviceLocator) {
// Packages to Scan for Jersey Resources
packages(true, "com.starbucks.api");
packages(true, "com.starbucks.exception.mapper");
// Jackson
register(ObjectMapperContextResolver.class);
register(JacksonFeature.class);
// HK2-Guice Bridge
Injector injector = createGuiceInjector();
initGuiceIntoHK2Bridge(serviceLocator, injector);
// Register Filters
// CORSFilter
register(SimpleCorsFilter.newBuilder()
.allowOriginDomain("localhost")
.allowCredentials()
.allowMethod(HttpMethod.GET)
.allowMethod(HttpMethod.OPTIONS)
.allowMethod(HttpMethod.POST)
.allowMethod(HttpMethod.PUT)
.allowHeader(HttpHeaders.CONTENT_TYPE)
.build());
// Jersey Event Listener
// Swagger
register(ApiListingResource.class);
register(SwaggerSerializers.class);
setUpSwagger();
//Tracing
property(ServerProperties.TRACING, "ON_DEMAND");
property(ServerProperties.TRACING_THRESHOLD, "VERBOSE");
//Jersey BeanValidation
property(ServerProperties.BV_SEND_ERROR_IN_RESPONSE, true);
}
private void initGuiceIntoHK2Bridge(final ServiceLocator serviceLocator, final Injector injector) {
GuiceBridge.getGuiceBridge().initializeGuiceBridge(serviceLocator);
GuiceIntoHK2Bridge guiceBridge = serviceLocator.getService(GuiceIntoHK2Bridge.class);
guiceBridge.bridgeGuiceInjector(injector);
}
private boolean isMac() {
return "mac os x".equalsIgnoreCase(System.getProperty("os.name"));
}
private Module[] getBaseModule() {
return new Module[]{
new ServletModule(),
new ApiBaseModule()
};
}
private Injector createGuiceInjector() {
if (isMac()) {
return Guice.createInjector(getBaseModule());
} else {
return Guice.createInjector(Stage.PRODUCTION, getBaseModule());
}
}
private BeanConfig setUpSwagger() {
BeanConfig beanConfig = new BeanConfig();
beanConfig.setVersion("1.0.2");
beanConfig.setSchemes(new String[]{"http"});
beanConfig.setHost("localhost:8080");
beanConfig.setBasePath("/api");
beanConfig.setResourcePackage("com.starbucks.api");
beanConfig.setDescription("Provides the list of APIs for Starbucks backend");
beanConfig.setTitle("Starbucks Backend API");
beanConfig.setScan(true);
return beanConfig;
}
}
==================== Resource File ====================
package com.starbucks.api;
import com.starbucks.config.SharedConfig;
import com.starbucks.service.PingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
@Api(value = "Health Check")
@Path("v1")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class PingApiResource {
private SharedConfig config;
private PingService pingService;
@Inject
public PingApiResource(final SharedConfig config, final PingService pingService) {
this.config = config;
this.pingService = pingService;
}
@GET
@Path("/ping")
@ApiOperation(value = "Ping API",
notes = "Simple Ping API for health check",
response = String.class)
@ApiResponses(value = {
@ApiResponse(code = 200, message = "OK"),
@ApiResponse(code = 404, message = "PING NOT FOUND"),
@ApiResponse(code = 500, message = "INTERNAL SERVER ERROR")
})
public Response ping() {
String res = pingService.getPingResponse();
System.out.println(config.getList("whiteListedDomains"));
return Response.ok().entity("{\"data\":\"" + res + " from " + config.appName("appName") + "\"}").build();
}
}
I would really appreciate the help. Thank you so much!!!