Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add example swagger usage #6

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@
*.iml
target
dependency-reduced-pom.xml
/.classpath
/.project
/.settings/
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@
<artifactId>gwizard-healthchecks</artifactId>
<version>${gwizard.version}</version>
</dependency>
<dependency>
<groupId>org.gwizard</groupId>
<artifactId>gwizard-swagger</artifactId>
<version>${gwizard.version}</version>
</dependency>
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-jackson</artifactId>
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/example/app/ExampleConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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();

}
18 changes: 14 additions & 4 deletions src/main/java/com/example/app/ExampleModule.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
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;
import com.fasterxml.jackson.databind.DeserializationFeature;
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;

/**
* <p>Among the duties of your application module(s), you must explicitly bind every JAXRS resource class.
Expand Down Expand Up @@ -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();
}

}
12 changes: 8 additions & 4 deletions src/main/java/com/example/app/Main.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
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;
import org.gwizard.logging.LoggingModule;
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
Expand All @@ -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();
}
Expand Down
34 changes: 34 additions & 0 deletions src/main/java/com/example/app/resource/ExampleApiConfig.java
Original file line number Diff line number Diff line change
@@ -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 = "<http://localhost:8081/terms.html>",
contact = @Contact(
name = "Gandalf",
email = "[email protected]",
url = "<http://middleearth.org>"
),
license = @License(
name = "Apache 2.0",
url = "<http://www.apache.org/licenses/LICENSE-2.0>"
)
),
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 = "<http://ringsapi.io/middleearth.html>")
)
public interface ExampleApiConfig { /* empty */ }
8 changes: 8 additions & 0 deletions src/main/java/com/example/app/resource/FunResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down
4 changes: 4 additions & 0 deletions test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@

foo: bar

swagger:
resourcePackages:
- com.example.app.resource

web:
port: 8081

Expand Down