DropWizard:用Java轻轻的写一个RESTful Service

 1.关于Dropwizard的一些闲扯java

  在个人上一篇博客《 Embedded Server:像写main函数同样写Web Server》中,提到了使用Jetty Embedded Server进行Java Web Server的开发比传统的Web Container的方式进行开发的优点。若是直接使用Jetty提供的API进行Web Server的开发,特别是RESTful service的开发,不免看起来仍是简单粗暴了一些。git

  而后指望着更多的程序员逃离java世界,奔向看起来很美好的Ruby, Python等等。我本身而言,我不关心java是否是已经死在沙滩上了,更多的是想,新语言引入的新的技术,或者一些新的思潮,可以折射出咱们Java语言或者开发环境中的哪些不足。程序员

  有不少用Ruby on Rails的人,在对ruby知之甚少的状况下,就开始吭哧吭哧的写Server了。Rails提供了开发一个Server的完整技术栈,能够很方便的进行开发,后来又有不少人提供了不少RoR的的插件。github

  Dropwizard也是这么一个东西,提供了使用Java进行RESTful开发的所须要的最小的技术集合,使用了最轻量级的library。如以前博客里提到的Jetty,还有Jersey,Jackson等等。在Dropwizard官网上对其自身的介绍仍是比较客气的,不过看了Dropwizard在github上(https://github.com/dropwizard/dropwizard)的介绍仍是更来的实际些:web

 A damn simple library for building production-ready RESTful web services.bootstrap

  一个damn是那么的霸气侧漏。也但愿有一天我开发的某个library或者框架也能用上这样的词语:RNM,TTMD等等。ruby

2. 启动Dropwizard编译生成的Server框架

  在真正介绍如何使用Dropwizard以前,我想先让你们看看如何运行已经写好的Server的:curl

  java -jar dropwizard-demo-standalone.jar server hello.yamlide

  这样作的缘由,是让你们知道这个东西用起来很简单,这让我本身更火烧眉毛的想要用它。

3. 使用Dropwizard写一个RESTful Service

   用Dropwizard写一个RESTful Service至少须要这么几个部分:一是Configuration,二是Service,而后是Resource。由于Dropwizard中已经包括了最经常使用和最好用的几个开源库,这样编写一个Service会方便和快捷不少。Configuration主要是做为Serivce自己的配置,经过Service能够访问对应的Resource。下面就看一个简单的例子:

  Configuration: 

  Configuration用于从配置文件中读取信息,好比从前面的hello.yaml中读取。Dropwizard中默认的是使用yaml,当你提供的配置文件的后缀名不是.yml或者.yaml时,将会讲你的配置文件看作JSON格式。

   hello.yaml

   template: Hello, %s!
   defaultName: Stranger

   在hello.yaml中提供了两个属性,template和defaultName。对应的是Java中的HelloConfiguration:

public class HelloConfiguration extends Configuration {
@NotEmpty
@JsonProperty
        private String template;


    @NotEmpty
    @JsonProperty
    private String defaultName = "Stranger";

    public String getDefaultName() {
        return defaultName;
    }

    public String getTemplate() {
        return template;
    }
}

  Service:

public class HelloService extends Service<HelloConfiguration> {
public static void main(String[] args) throws Exception {
        new HelloService().run(args);
    }

    @Override
    public void initialize(Bootstrap<HelloConfiguration> bootstrap) {
        bootstrap.setName("hello");
    }

    @Override
    public void run(HelloConfiguration configuration, Environment environment) throws Exception {
        final String template = configuration.getTemplate();
        final String defaultName = configuration.getDefaultName();
        environment.addResource(new HelloResource(template, defaultName));
    }

  在service中比较关键的几个东西包括HelloConfiguration,这个就是咱们先前从hello.yaml中解析出的对象。还有就是Environment,Dropwizard提供了不少组件,包括像Resource, Filter, HealthCheck等等,这些都被包括在了environment中。这里咱们添加了一个HelloResource。

  Resource:

@Path("hello")
@Produces(MediaType.APPLICATION_JSON)
public class HelloResource {
    private final String template;
    private final String defaultName;
    private final AtomicLong counter;

    public HelloResource(String template, String defaultName) {
        this.template = template;
        this.defaultName = defaultName;
        this.counter = new AtomicLong();
    }

    @GET
    public Saying sayHello(@QueryParam("name") Optional<String> name) {
        return new Saying(counter.incrementAndGet(),
                String.format(template, name.or(defaultName)));
    }
}

public class Saying {
    private final long id;
    private final String content;

    public Saying(long id, String content) {
        this.id = id;
        this.content = content;
    }

    public long getId() {
        return id;
    }

    public String getContent() {
        return content;
    }
}

3. 验证Service

    在服务被启动起来之后,咱们能够验证:

    curl http://localhost:8080/?hello=gogo

    返回:

    {"id":1,"content":"Hello, kiwi!"}

4. 更多

 博客中用到的例子其实都出自Dropwizard的官网,但愿你们不要嫌弃我。这里把例子几乎完整的列出来的缘由是在于,想展现给你们使用Dropwizard能够很方便的开发一个RESTful Service。特别是对于小型的Service,使用Dropwizard进行开发就显得极其方便。也许有人要说,大型的Service怎么办呢?个人系统或者业务自己就是很复杂怎么办呢?是否是就不能用Embedded Server了?是否是就不能用Dropwizard提供的生态系统了?其实否则,固然这里面有更多的巧思。另外,Dropwizard中还有不少有意思并且好用的组件。

相关文章
相关标签/搜索