Looking for a dropwizard example

2019-02-01 01:20发布

Looking for a dropwizard example I found:

https://github.com/codahale/dropwizard/tree/master/dropwizard-example

But I am interested in a more complete example with at least:

  1. a 1:n relationship like customer - account
  2. a html gui represenation at least with forms
  3. full crud support for xml

2 out of three would be a start and would earn "accepted" by me.

标签: dropwizard
7条回答
Rolldiameter
2楼-- · 2019-02-01 02:20

follow below step.

  1. Add dependencies in pom file

    <dependencies>
    <dependency>
        <groupId>com.yammer.dropwizard</groupId>
        <artifactId>dropwizard-core</artifactId>
        <version>0.6.2</version>
    </dependency> 
    

  2. Create configuration class

    import com.yammer.dropwizard.config.Configuration;
    public class BlogConfiguration extends Configuration{
    
     }
    
  3. 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;
    }


  }
  1. 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 + "]";
    }
    
  2. Run BlogService which will start the Jetty server and hit the localhost with port such as http://localhost:8079/

查看更多
登录 后发表回答