Skip to content

Instantly share code, notes, and snippets.

@johnewart
Last active February 18, 2016 02:39
Show Gist options
  • Save johnewart/1c78c08526ac954b7040 to your computer and use it in GitHub Desktop.
Save johnewart/1c78c08526ac954b7040 to your computer and use it in GitHub Desktop.

In your POM in the block add the following:

<dependency>
       <groupId>com.smoketurner</groupId>
       <artifactId>dropwizard-swagger</artifactId>
       <version>0.9.1-1</version>
   </dependency>

In your Application class add the following to initialize:

bootstrap.addBundle(new SwaggerBundle<HelloWorldConfiguration>() {
      @Override
      protected SwaggerBundleConfiguration getSwaggerBundleConfiguration(HelloWorldConfiguration configuration) {
          return configuration.swaggerBundleConfiguration;
      }
  });

In your configuration class add the following:

@JsonProperty("swagger")
public SwaggerBundleConfiguration swaggerBundleConfiguration;

In your configuration YAML add the following:

swagger:
  resourcePackage: com.example.helloworld
package com.example.helloworld.resources;
import com.codahale.metrics.annotation.Timed;
import com.example.helloworld.api.Saying;
import com.example.helloworld.core.Template;
import com.google.common.base.Optional;
import io.dropwizard.jersey.caching.CacheControl;
import io.dropwizard.jersey.params.DateTimeParam;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.validation.Valid;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
@Path("/hello-world")
@Produces(MediaType.APPLICATION_JSON)
@Api(value = "/hello-world", description = "Hello world!")
public class HelloWorldResource {
private static final Logger LOGGER = LoggerFactory.getLogger(HelloWorldResource.class);
private final Template template;
private final AtomicLong counter;
public HelloWorldResource(Template template) {
this.template = template;
this.counter = new AtomicLong();
}
@GET
@Timed(name = "get-requests")
@CacheControl(maxAge = 1, maxAgeUnit = TimeUnit.DAYS)
@ApiOperation(
value = "Say hello!",
notes = "Returns a saying",
response = Saying.class)
@ApiResponses(value = { @ApiResponse(code = 400, message = "Invalid name supplied"),
@ApiResponse(code = 404, message = "Name not found") })
public Saying sayHello(
@ApiParam(value = "Name of person to say hello to", required = false) @QueryParam("name") Optional<String> name) {
return new Saying(counter.incrementAndGet(), template.render(name));
}
@GET
@Path("/date")
@Produces(MediaType.TEXT_PLAIN)
public String receiveDate(@QueryParam("date") Optional<DateTimeParam> dateTimeParam) {
if (dateTimeParam.isPresent()) {
final DateTimeParam actualDateTimeParam = dateTimeParam.get();
LOGGER.info("Received a date: {}", actualDateTimeParam);
return actualDateTimeParam.get().toString();
} else {
LOGGER.warn("No received date");
return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment