diff --git a/.gitignore b/.gitignore index ac0e92f..6316438 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,6 @@ *.iml target dependency-reduced-pom.xml +/.classpath +/.project +/.settings/ diff --git a/README.md b/README.md index aa0031a..a89f297 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,11 @@ $ mvn install $ java -jar target/gwizard-example-1.0-SNAPSHOT.jar test.yml ``` -The most exciting endpoint is probably: http://localhost:8081/fun/headers +The most exciting endpoints are probably: + +* http://localhost:8081/fun/headers +* http://localhost:8081/swagger.json +* http://localhost:8081/swagger.yaml (Note that test.yml changes the port from the default of 8080, just to demonstrate) diff --git a/pom.xml b/pom.xml index 43e1e67..4cbdad9 100644 --- a/pom.xml +++ b/pom.xml @@ -71,6 +71,11 @@ gwizard-healthchecks ${gwizard.version} + + org.gwizard + gwizard-swagger + ${gwizard.version} + io.dropwizard dropwizard-jackson diff --git a/src/main/java/com/example/app/ExampleConfig.java b/src/main/java/com/example/app/ExampleConfig.java index 1167df7..f0916d0 100644 --- a/src/main/java/com/example/app/ExampleConfig.java +++ b/src/main/java/com/example/app/ExampleConfig.java @@ -4,6 +4,7 @@ import org.gwizard.healthchecks.HealthChecksConfig; import org.gwizard.hibernate.DatabaseConfig; import org.gwizard.logging.LoggingConfig; +import org.gwizard.swagger.SwaggerConfig; import org.gwizard.web.WebConfig; /** @@ -23,4 +24,6 @@ public class ExampleConfig { private WebConfig web = new WebConfig(); private DatabaseConfig database = new DatabaseConfig(); private HealthChecksConfig healthChecks = new HealthChecksConfig(); + private SwaggerConfig swagger = new SwaggerConfig(); + } diff --git a/src/main/java/com/example/app/ExampleModule.java b/src/main/java/com/example/app/ExampleModule.java index 0f473a3..bf7674d 100644 --- a/src/main/java/com/example/app/ExampleModule.java +++ b/src/main/java/com/example/app/ExampleModule.java @@ -1,5 +1,12 @@ package com.example.app; +import javax.inject.Singleton; + +import org.gwizard.hibernate.DatabaseConfig; +import org.gwizard.logging.LoggingConfig; +import org.gwizard.swagger.SwaggerConfig; +import org.gwizard.web.WebConfig; + import com.example.app.resource.FunResource; import com.example.app.resource.ThingsResource; import com.example.app.services.ExampleService; @@ -7,11 +14,8 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.google.inject.AbstractModule; import com.google.inject.Provides; + import io.dropwizard.jackson.Jackson; -import org.gwizard.hibernate.DatabaseConfig; -import org.gwizard.logging.LoggingConfig; -import org.gwizard.web.WebConfig; -import javax.inject.Singleton; /** *

Among the duties of your application module(s), you must explicitly bind every JAXRS resource class. @@ -50,4 +54,10 @@ public WebConfig webConfig(ExampleConfig cfg) { public DatabaseConfig databaseConfig(ExampleConfig cfg) { return cfg.getDatabase(); } + + @Provides + public SwaggerConfig swaggerConfig(ExampleConfig cfg) { + return cfg.getSwagger(); + } + } diff --git a/src/main/java/com/example/app/Main.java b/src/main/java/com/example/app/Main.java index 815a821..f18dd46 100644 --- a/src/main/java/com/example/app/Main.java +++ b/src/main/java/com/example/app/Main.java @@ -1,7 +1,7 @@ package com.example.app; -import com.google.inject.Guice; -import com.google.inject.Injector; +import java.io.File; + import org.gwizard.config.ConfigModule; import org.gwizard.healthchecks.HealthChecksModule; import org.gwizard.hibernate.HibernateModule; @@ -9,7 +9,10 @@ import org.gwizard.metrics.MetricsModule; import org.gwizard.rest.RestModule; import org.gwizard.services.Run; -import java.io.File; +import org.gwizard.swagger.SwaggerModule; + +import com.google.inject.Guice; +import com.google.inject.Injector; /** * Set up the injector and start all services @@ -29,7 +32,8 @@ public static void main(String[] args) throws Exception { new RestModule(), new HibernateModule(), new MetricsModule(), - new HealthChecksModule()); + new HealthChecksModule(), + new SwaggerModule()); injector.getInstance(Run.class).start(); } diff --git a/src/main/java/com/example/app/resource/ExampleApiConfig.java b/src/main/java/com/example/app/resource/ExampleApiConfig.java new file mode 100644 index 0000000..65539d8 --- /dev/null +++ b/src/main/java/com/example/app/resource/ExampleApiConfig.java @@ -0,0 +1,34 @@ +package com.example.app.resource; + +import io.swagger.annotations.Contact; +import io.swagger.annotations.ExternalDocs; +import io.swagger.annotations.Info; +import io.swagger.annotations.License; +import io.swagger.annotations.SwaggerDefinition; +import io.swagger.annotations.Tag; + +@SwaggerDefinition( + info = @Info( + description = "Example Gwizard App", + version = "V1.0.1", + title = "The GWizard Example API", + termsOfService = "", + contact = @Contact( + name = "Gandalf", + email = "into.shadow@middleearth.org", + url = "" + ), + license = @License( + name = "Apache 2.0", + url = "" + ) + ), + consumes = {"application/json", "application/xml"}, + produces = {"application/json", "application/xml"}, + schemes = {SwaggerDefinition.Scheme.HTTP, SwaggerDefinition.Scheme.HTTPS}, + tags = { + @Tag(name = "Private", description = "Tag used to denote operations as private") + }, + externalDocs = @ExternalDocs(value = "Rings", url = "") +) +public interface ExampleApiConfig { /* empty */ } \ No newline at end of file diff --git a/src/main/java/com/example/app/resource/FunResource.java b/src/main/java/com/example/app/resource/FunResource.java index e5efb14..2b7f7c3 100644 --- a/src/main/java/com/example/app/resource/FunResource.java +++ b/src/main/java/com/example/app/resource/FunResource.java @@ -2,6 +2,10 @@ import com.codahale.metrics.annotation.Timed; import com.example.app.ExampleConfig; + +import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.ApiResponse; +import io.swagger.annotations.ApiResponses; import lombok.Data; import javax.inject.Inject; import javax.ws.rs.Consumes; @@ -33,6 +37,10 @@ public static class Stuff { private final String foo; } + @ApiOperation(tags={"stuff", "configuration"}, value="Returns some of this application configuration e.g. foo") + @ApiResponses(value={ + @ApiResponse(code=200, message="OK") + }) @GET public Stuff stuff() { return new Stuff(cfg.getFoo()); diff --git a/test.yml b/test.yml index 6adce80..48f3a13 100644 --- a/test.yml +++ b/test.yml @@ -9,6 +9,10 @@ foo: bar +swagger: + resourcePackages: + - com.example.app.resource + web: port: 8081