follow below step.
Add dependencies in pom file
<dependencies>
<dependency>
<groupId>com.yammer.dropwizard</groupId>
<artifactId>dropwizard-core</artifactId>
<version>0.6.2</version>
</dependency>
Create configuration class
import com.yammer.dropwizard.config.Configuration;
public class BlogConfiguration extends Configuration{
}
Create Service class
import com.yammer.dropwizard.Service;
import com.yammer.dropwizard.config.Bootstrap;
import com.yammer.dropwizard.config.Environment;
public class BlogService extends Service<BlogConfiguration> {
public static void main(String[] args) throws Exception {
new BlogService().run(new String[] { "server",
"C:\\LocalEnv\\Workspace\\dropwizarddemo\\configuration.yml" });
}
@Override
public void initialize(Bootstrap<BlogConfiguration> bootstrap) {
bootstrap.setName("blog");
}
@Override
public void run(BlogConfiguration configuration,
Environment environment) throws Exception {
environment.addResource(new IndexResource());
}
}
Note: put below configuration.yml file in current directory
# HTTP-specific options.
http:
# The port on which the HTTP server listens for service requests.
port: 8079
# The port on which the HTTP server listens for administrative
# requests.
adminPort: 8179
# Maximum number of threads.
maxThreads: 100
# Minimum number of thread to keep alive.
minThreads: 10
4. Write Index resources.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import com.yammer.metrics.annotation.Timed;
@Path("/")
public class IndexResource {
@GET
@Produces(value = MediaType.APPLICATION_JSON)
@Timed
public List<Blog> index() {
return Arrays.asList(new Blog("for Java Developers",
"http://stackoverflow.com/questions/13345693/looking-for-a-dropwizard-
example”));
}
@Path("/service")
@GET
@Produces(value = MediaType.APPLICATION_JSON)
@Timed
public List<Users> users() {
List<Users> list = new ArrayList<Users>();
list.add(new Users(25,"Sambhu","SA"));
list.add(new Users(35,"Amit","VP"));
list.add(new Users(45,"Sanket","AVP"));
return list;
}
}
Write POJO for Blog and Users like
public class Users {
Integer id;
String name;
String designation;
public Users(Integer id, String name, String desination){
this.id=id;
this.name=name;
this.designation=desination;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDesignation() {
return designation;
}
public void setDesignation(String designation) {
this.designation = designation;
}
@Override
public String toString() {
return "Users [id=" + id + ", name=" + name + ", designation="
+ designation + "]";
}
Run BlogService which will start the Jetty server and hit the localhost with port such as
http://localhost:8079/