getting started dropwizard 0.7.0
this article is copied from http://dropwizard.io/getting-started.html
Setting Up Maven
% mkdir dropwizard && cd dropwizard % mvn archetype:create -DgroupId=com.example -DartifactId=myapp % cd myapp % git init % git add . % git commit -m 'initial commit'
check it
% mvn package
pom.xml
add a dropwizard.version:
% git diff pom.xml diff --git a/pom.xml b/pom.xml index ecbd708..41a3631 100644 --- a/pom.xml +++ b/pom.xml @@ -12,6 +12,7 @@ <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + <dropwizard.version>0.7.0</dropwizard.version> </properties> <dependencies>
% git add pom.xml % git commit -m 'add a dropwizard.version'
add dropwizard-core library as a dependency:
% git diff --- a/pom.xml +++ b/pom.xml @@ -22,5 +22,10 @@ <version>3.8.1</version> <scope>test</scope> </dependency> + <dependency> + <groupId>io.dropwizard</groupId> + <artifactId>dropwizard-core</artifactId> + <version>${dropwizard.version}</version> + </dependency> </dependencies> </project>
% git add pom.xml % git commit -m 'add dropwizard-core library as a dependency'
check it.
% mvn package [INFO] Building jar: /Users/okamuuu/dropwizard/myapp/target/myapp-1.0-SNAPSHOT.jar [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 01:19 min [INFO] Finished at: 2014-11-06T12:36:38+09:00 [INFO] Final Memory: 17M/154M [INFO] ------------------------------------------------------------------------
you can see 'BUILD SUCCESS' and finished Setting Up Maven.
Creating A Configuration Class
create src/main/java/com/example/helloworld/HelloWorldConfiguration.java:
package com.example.helloworld; import io.dropwizard.Configuration; import com.fasterxml.jackson.annotation.JsonProperty; import org.hibernate.validator.constraints.NotEmpty; public class HelloWorldConfiguration extends Configuration { @NotEmpty private String template; @NotEmpty private String defaultName = "Stranger"; @JsonProperty public String getTemplate() { return template; } @JsonProperty public void setTemplate(String template) { this.template = template; } @JsonProperty public String getDefaultName() { return defaultName; } @JsonProperty public void setDefaultName(String name) { this.defaultName = name; } }
% git add src/main/java/com/example/helloworld/HelloWorldConfiguration.java % git commit -m 'add HelloWorldConfiguration'
create myapp.yml
template: Hello, %s! defaultName: Stranger
% git add myapp.yml % git commit -m 'myapp.yml'
check it.
% mvn package [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------
you can see 'BUILD SUCCESS' and finished Creating A Configuration Class
Creating An Application Class
create src/main/java/com/example/helloworld/HelloWorldApplication.java:
package com.example.helloworld; import io.dropwizard.Application; import io.dropwizard.setup.Bootstrap; import io.dropwizard.setup.Environment; //import com.example.helloworld.resources.HelloWorldResource; //import com.example.helloworld.health.TemplateHealthCheck; public class HelloWorldApplication extends Application<HelloWorldConfiguration> { public static void main(String[] args) throws Exception { new HelloWorldApplication().run(args); } @Override public String getName() { return "hello-world"; } @Override public void initialize(Bootstrap<HelloWorldConfiguration> bootstrap) { // nothing to do yet } @Override public void run(HelloWorldConfiguration configuration, Environment environment) { // nothing to do yet } }
% git add src/main/java/com/example/helloworld/HelloWorldApplication.java % git commit -m 'add HelloWorldApplication'
check it
% mvn package [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------
you can see 'BUILD SUCCESS' and finished Creating An Application Class
Creating A Representation Class
create src/main/java/com/example/helloworld/core/Saying.java:
package com.example.helloworld.core; import com.fasterxml.jackson.annotation.JsonProperty; import org.hibernate.validator.constraints.Length; public class Saying { private long id; @Length(max = 3) private String content; public Saying() { // Jackson deserialization } public Saying(long id, String content) { this.id = id; this.content = content; } @JsonProperty public long getId() { return id; } @JsonProperty public String getContent() { return content; } }
% git add src/main/java/com/example/helloworld/core/Saying.java % git commit -m 'add core/Saying'
check it
% mvn package [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------
you can see 'BUILD SUCCESS' and finished Creating A Representation Class
Creating A Resource Class
create src/main/java/com/example/helloworld/resources/HelloWorldResource.java:
package com.example.helloworld.resources; import com.example.helloworld.core.Saying; import com.google.common.base.Optional; import com.codahale.metrics.annotation.Timed; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; import java.util.concurrent.atomic.AtomicLong; @Path("/hello-world") @Produces(MediaType.APPLICATION_JSON) public class HelloWorldResource { private final String template; private final String defaultName; private final AtomicLong counter; public HelloWorldResource(String template, String defaultName) { this.template = template; this.defaultName = defaultName; this.counter = new AtomicLong(); } @GET @Timed public Saying sayHello(@QueryParam("name") Optional<String> name) { final String value = String.format(template, name.or(defaultName)); return new Saying(counter.incrementAndGet(), value); } }
% git add src/main/java/com/example/helloworld/resources/HelloWorldResource.java % git commit -m 'add resorces/HelloWorldResource'
check it
% mvn package [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------
you can see 'BUILD SUCCESS' and finished Creating A Resource Class
Registering A Resource
edit src/main/java/com/example/helloworld/HelloWorldApplication.java:
% git diff src/main/java/com/example/helloworld/HelloWorldApplication.java diff --git a/src/main/java/com/example/helloworld/HelloWorldApplication.java b/src/main/java/com/example/helloworld/HelloWorldApplication.java index c9601f9..e6f65cf 100644 --- a/src/main/java/com/example/helloworld/HelloWorldApplication.java +++ b/src/main/java/com/example/helloworld/HelloWorldApplication.java @@ -3,7 +3,7 @@ package com.example.helloworld; import io.dropwizard.Application; import io.dropwizard.setup.Bootstrap; import io.dropwizard.setup.Environment; -//import com.example.helloworld.resources.HelloWorldResource; +import com.example.helloworld.resources.HelloWorldResource; //import com.example.helloworld.health.TemplateHealthCheck; public class HelloWorldApplication extends Application<HelloWorldConfiguration> { @@ -24,7 +24,11 @@ public class HelloWorldApplication extends Application<HelloWorldConfiguration> @Override public void run(HelloWorldConfiguration configuration, Environment environment) { - // nothing to do yet + final HelloWorldResource resource = new HelloWorldResource( + configuration.getTemplate(), + configuration.getDefaultName() + ); + environment.jersey().register(resource); } }
% git add src/main/java/com/example/helloworld/HelloWorldApplication.java % git commit -m "add resource instance to the application's Jersey environment"
% mvn package [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------
you can see 'BUILD SUCCESS' and finished Registering A Resource
Creating A Health Check
create src/main/java/com/example/helloworld/health/TemplateHealthCheck.java:
package com.example.helloworld.health; import com.codahale.metrics.health.HealthCheck; public class TemplateHealthCheck extends HealthCheck { private final String template; public TemplateHealthCheck(String template) { this.template = template; } @Override protected Result check() throws Exception { final String saying = String.format(template, "TEST"); if (!saying.contains("TEST")) { return Result.unhealthy("template doesn't include a name"); } return Result.healthy(); } }
% git add src/main/java/com/example/helloworld/health/TemplateHealthCheck.java % git commit -m "add TemplateHealthCheck"
% mvn package [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------
you can see 'BUILD SUCCESS' and finished Creating A Health Check
Adding A Health Check
edit src/main/java/com/example/helloworld/HelloWorldApplication.java:
% git diff src/main/java/com/example/helloworld/HelloWorldApplication.java diff --git a/src/main/java/com/example/helloworld/HelloWorldApplication.java b/src/main/java/com/example/helloworld/HelloWorldApplication.java index e6f65cf..cdfce9b 100644 --- a/src/main/java/com/example/helloworld/HelloWorldApplication.java +++ b/src/main/java/com/example/helloworld/HelloWorldApplication.java @@ -4,7 +4,7 @@ import io.dropwizard.Application; import io.dropwizard.setup.Bootstrap; import io.dropwizard.setup.Environment; import com.example.helloworld.resources.HelloWorldResource; -//import com.example.helloworld.health.TemplateHealthCheck; +import com.example.helloworld.health.TemplateHealthCheck; public class HelloWorldApplication extends Application<HelloWorldConfiguration> { public static void main(String[] args) throws Exception { @@ -28,6 +28,9 @@ public class HelloWorldApplication extends Application<HelloWorldConfiguration> configuration.getTemplate(), configuration.getDefaultName() ); + final TemplateHealthCheck healthCheck = + new TemplateHealthCheck(configuration.getTemplate()); + environment.healthChecks().register("template", healthCheck); environment.jersey().register(resource); }
% git add src/main/java/com/example/helloworld/HelloWorldApplication.java % git commit -m "add healthcheck instance to the Environment"
check it.
% mvn package [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------
you can see 'BUILD SUCCESS' and finished Adding A Health Check.
Now we’re almost ready to go!
Building Fat JARs
edit pom.xml:
% git diff pom.xml diff --git a/pom.xml b/pom.xml index ad0cf1c..322e8dd 100644 --- a/pom.xml +++ b/pom.xml @@ -28,4 +28,43 @@ <version>${dropwizard.version}</version> </dependency> </dependencies> + + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-shade-plugin</artifactId> + <version>1.6</version> + <configuration> + <createDependencyReducedPom>true</createDependencyReducedPom> + <filters> + <filter> + <artifact>*:*</artifact> + <excludes> + <exclude>META-INF/*.SF</exclude> + <exclude>META-INF/*.DSA</exclude> + <exclude>META-INF/*.RSA</exclude> + </excludes> + </filter> + </filters> + </configuration> + <executions> + <execution> + <phase>package</phase> + <goals> + <goal>shade</goal> + </goals> + <configuration> + <transformers> + <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/> + <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> + <mainClass>com.example.helloworld.HelloWorldApplication</mainClass> + </transformer> + </transformers> + </configuration> + </execution> + </executions> + </plugin> + </plugins> + </build> </project>
% git add pom.xml % git commit -m 'add maven-shade plugins'
Versioning Your JARs
edit pom.xml again
% git diff pom.xml diff --git a/pom.xml b/pom.xml index 322e8dd..d4b67a8 100644 --- a/pom.xml +++ b/pom.xml @@ -65,6 +65,18 @@ </execution> </executions> </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-jar-plugin</artifactId> + <version>2.4</version> + <configuration> + <archive> + <manifest> + <addDefaultImplementationEntries>true</addDefaultImplementationEntries> + </manifest> + </archive> + </configuration> + </plugin> </plugins> </build> </project>
% git add pom.xml % git commit -m 'add maven-jar-plugin'
Running Your Application
% java -jar target/myapp-1.0-SNAPSHOT.jar server myapp.yml % open http://localhost:8080/hello-world % open http://localhost:8081/