DropWizard入门实践

Dropwizard是一个操作友好、开发RESTful服务的Java高性能框架,Dropwizard有自己独立的风格,可以辅助以Jetty Jackson Jersey和Metrics提供强大的基于JVM后端服务,Dropwizard将稳定 成熟带给了Java生态系统,大道至简,轻量库包让你聚焦业务,Dropwizard有out-of-the-box支持复杂的配置 应用度量记录、日志等,让你的队伍在短时间内生产出高质量的HTTP+JSON Web服务。

https://www.jdon.com/soa/6-restful.html

DropWizard结构的Web服务组成

  • 1、Configuration:用于设置该服务的配置,比方说在服务开放在哪个端口,数据库配置是怎样的等等。
  • 2、Service:该服务的主入口,定义该服务使用哪个配置文件,开放哪些Resource,该服务需要哪些HealthCheck等等。
  • 3、Resource:定义一个资源,包括如何获取该资源,对该资源做Get/Post/Delete/Query时,对应的各种业务逻辑。
  • 4、Representation:定义了一个服务返回值对象,当服务返回该对象时,会自动的把该对象按属性值生成一个Json格式的字符串返回给服务调用者。
  • 5、HealthCheck:在DropWizard为每个服务提供的OM框架中用到,通过它可以随时检测当前服务是否可用。

使用Dropwizard写一个RESTful Service:
用Dropwizard写一个RESTful Service至少需要这么几个部分:一是Configuration,二是Service,然后是Resource。因为Dropwizard中已经包括了最常用和最好用的几个开源库,这样编写一个Service会方便和快捷很多。Configuration主要是作为Serivce本身的配置,通过Service可以访问对应的Resource。
Configuration用于从配置文件中读取信息。Dropwizard中默认的是使用yaml,当你提供的配置文件的后缀名不是.yml或者.yaml时,将会讲你的配置文件看做JSON格式。

配置文件time-service.yml

defaultTimezone: UTC
server:
  applicationConnectors:
  - type: http
    port: 9000
  adminConnectors:
  - type: http
    port: 9001

配置文件封装类

package com.zte.sunquan.demo.wizard;
 
import io.dropwizard.Configuration;
import org.hibernate.validator.constraints.NotEmpty;
 
import com.fasterxml.jackson.annotation.JsonProperty;

public class TimeZoneConfigure extends Configuration {
    @NotEmpty
    @JsonProperty
    private String defaultTimezone;
 
    public String getDefaultTimezone() {
        return defaultTimezone;
    }
 
}

主体请求处理逻辑

package com.zte.sunquan.demo.wizard;
 
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
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 com.google.common.base.Optional;
 
@Path("/time")
@Produces(MediaType.APPLICATION_JSON)
public class TimeResource {
    private final String defaultTimezone;
 
    public TimeResource(String defaultTimezone) {
        this.defaultTimezone = defaultTimezone;
    }
 
    @GET
    public Time getTime(@QueryParam("timezone") Optional timezone) {
        DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("配置文件中配置:" + defaultTimezone);//配置文件中配置
        TimeZone timeZone = null;
        if (timezone.isPresent()) {
            timeZone = TimeZone.getTimeZone(timezone.get().toString());
            System.out.println("请求中配置:" + timezone.get().toString());
        } else {
            timeZone = TimeZone.getTimeZone(defaultTimezone);
        }
        formatter.setTimeZone(timeZone);
        String formatted = formatter.format(new Date());
        return new Time(formatted);
    }
}

启动类

package com.zte.sunquan.demo.wizard;
 
import io.dropwizard.Application;
import io.dropwizard.setup.Bootstrap;
 
public class TimeApplication extends Application {
    public static void main(String[] args) throws Exception {
        new TimeApplication().run(args);
    }
 
    @Override
    public void initialize(Bootstrap timezoneConfigurationBootstrap) {
    }
 
    @Override
    public void run(TimeZoneConfigure timeZoneConfigure, io.dropwizard.setup.Environment environment) throws Exception {
        final TimeResource resource = new TimeResource(timeZoneConfigure.getDefaultTimezone());
        environment.jersey().register(resource);
    }
}

注意dropwizard需要传入配置文件:

http://localhost:9000/time?timezone=MST
输出:
{"time":"2018-09-08 08:47:15"}

http://localhost:9000/time
输出:
{"time":"2018-09-08 01:47:53"}

你可能感兴趣的:(DropWizard入门实践)