Permalink
Browse files

now we have views!

  • Loading branch information...
Alexey Prohorenko
Alexey Prohorenko committed May 4, 2012
1 parent 612da1d commit 92dbe459ab8e6cd17800fdb5b1a38c7f5f6db18f
View
@@ -7,7 +7,9 @@ $ mvn package
$ java -jar target/dropwizard-example-0.0.1-SNAPSHOT.jar server configs/hello-world.yml
-$ open http://localhost:8080/hello-world http://localhost:8080/hello-world?name=Successful+Dropwizard+User
+$ open http://localhost:8080/hello-world/hi.json http://localhost:8080/hello-world/hi.json?name=Successful+Dropwizard+User
+
+$ open http://localhost:8080/hello-world/hi.html http://localhost:8080/hello-world/hi.html?name=Successful+Dropwizard+User
$ open http://localhost:8081/ http://localhost:8081/metrics http://localhost:8081/threads http://localhost:8081/healthcheck
View
@@ -12,6 +12,11 @@
<artifactId>dropwizard-core</artifactId>
<version>0.4.0</version>
</dependency>
+ <dependency>
+ <groupId>com.yammer.dropwizard</groupId>
+ <artifactId>dropwizard-views</artifactId>
+ <version>0.4.0</version>
+ </dependency>
</dependencies>
<properties>
@@ -1,6 +1,7 @@
package com.alexeypro.samples.resources;
import com.alexeypro.samples.Saying;
+import com.alexeypro.samples.views.SayingView;
import com.google.common.base.Optional;
import com.yammer.metrics.annotation.Timed;
@@ -11,8 +12,8 @@
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;
@@ -26,8 +27,20 @@ public HelloWorldResource(String template, String defaultName) {
@GET
@Timed
+ @Path("/hi.json")
+ @Produces(MediaType.APPLICATION_JSON)
public Saying sayHello(@QueryParam("name") Optional<String> name) {
return new Saying(counter.incrementAndGet(),
String.format(template, name.or(defaultName)));
}
+
+ @GET
+ @Timed
+ @Path("/hi.html")
+ @Produces(MediaType.TEXT_HTML)
+ public SayingView sayHelloView(@QueryParam("name") Optional<String> name) {
+ return new SayingView(new Saying(counter.incrementAndGet(),
+ String.format(template, name.or(defaultName))));
+ }
+
}
@@ -5,6 +5,7 @@
import com.alexeypro.samples.health.TemplateHealthCheck;
import com.yammer.dropwizard.Service;
import com.yammer.dropwizard.config.Environment;
+import com.yammer.dropwizard.views.ViewBundle;
public class HelloWorldService extends Service<HelloWorldConfiguration> {
public static void main(String[] args) throws Exception {
@@ -13,6 +14,7 @@ public static void main(String[] args) throws Exception {
private HelloWorldService() {
super("hello-world");
+ addBundle(new ViewBundle());
}
@Override
@@ -0,0 +1,17 @@
+package com.alexeypro.samples.views;
+
+import com.alexeypro.samples.Saying;
+import com.yammer.dropwizard.views.View;
+
+public class SayingView extends View {
+ private final Saying saying;
+
+ public SayingView(Saying saying) {
+ super("saying.ftl");
+ this.saying = saying;
+ }
+
+ public Saying getSaying() {
+ return saying;
+ }
+}
@@ -0,0 +1,7 @@
+<#-- @ftlvariable name="" type="com.alexeypro.samples.views.SayingView" -->
+<html>
+ <body>
+ <!-- calls getSaying().getContent() and sanitizes it -->
+ <h1>${saying.content?html}!</h1>
+ </body>
+</html>

0 comments on commit 92dbe45

Please sign in to comment.