1. Mục đích
Sử dụng Dropwizard để xây dựng Rest application đơn giản.
2. Dropwizard là gì
Trích từ trang chủ của Dropwozard
https://www.dropwizard.io/1.3.11/docs/getting-started.html
Dropwizard straddles the line between being a library and a framework. Its goal is to provide performant, reliable implementations of everything a production-ready web application needs. Because this functionality is extracted into a reusable library, your application remains lean and focused, reducing both time-to-market and maintenance burdens.
3. Môi trường
- OS: macOS High Sierra 10.13.4
- Eclipse: Eclipse Java EE IDE for Web Developers 2018-09 (4.9.0)
- Java: 1.8.0_181
- Dropwizard: 1.3.11
- Gradle: 4.10.2
4. Dependency
Thêm dependency vào build.gradle của gradle project.
1
|
compile 'io.dropwizard:dropwizard-core:1.3.11'
|
5. Class Configuration
Dropwizard mô tả các thông tin config vào một file định dạng YAML(nếu loại file không phải là .yml hoặc .yaml, Dropwizard sẽ nhận định là file json để phân tích). Vì vậy class Confgiguration sẽ đọc các giá trị trong file YAML.
dropwizard.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
appName: "VTI demo dropwizard"
server:
applicationConnectors:
- type: http
port: 8880
logging:
level: INFO
loggers:
# Sets the level for 'vtitech.vn.dropwizard' to DEBUG.
tech.vtijapan.co.jp.dropwizard: DEBUG
appenders:
- type: console
timeZone: JST
|
DropwizardConfiguration.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
package tech.vtijapan.co.jp.dropwizard;
import io.dropwizard.Configuration;
import org.hibernate.validator.constraints.NotEmpty;
public class DropwizardConfiguration extends Configuration {
@NotEmpty
private String appName;
public String getAppName() {
return appName;
}
public void setAppName(String appName) {
this.appName = appName;
}
}
|
6. Class Resource
Các URI sẽ được map với các file resource.
DropwizardResource.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
package tech.vtijapan.co.jp.dropwizard;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
@Path("/appName")
public class DropwizardResource {
private String appName;
public DropwizardResource(String appName) {
this.appName = appName;
}
@GET
public String getAppName() {
return appName;
}
}
|
7. Class Application
Class application là entry point của Dropwizard. Method main của java sẽ được viết tại class application.
DropwizardApplication.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
package tech.vtijapan.co.jp.dropwizard;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.codahale.metrics.health.HealthCheck;
import io.dropwizard.Application;
import io.dropwizard.setup.Environment;
public class DropwizardApplication extends Application<DropwizardConfiguration> {
private static final Logger logger = LoggerFactory.getLogger(DropwizardApplication.class);
public static void main(String[] args) throws Exception {
logger.info("main");
new DropwizardApplication().run(args);
}
@Override
public void run(DropwizardConfiguration configuration, Environment environment) throws Exception {
logger.info("run");
environment.jersey().register(new DropwizardResource(configuration.getAppName()));
environment.healthChecks().register("alive", new HealthCheck() {
@Override
protected Result check() throws Exception {
return Result.healthy();
}
});
}
}
|
8. Run Application
Run main() trong DropwizardApplication với arguments “server dropwizard.yml”
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
21:13:49.663 [main] INFO tech.vtijapan.co.jp.dropwizard.DropwizardApplication - main
INFO [2019-05-22 21:13:50,914] io.dropwizard.server.DefaultServerFactory: Registering jersey handler with root path prefix: /
INFO [2019-05-22 21:13:50,916] io.dropwizard.server.DefaultServerFactory: Registering admin handler with root path prefix: /
INFO [2019-05-22 21:13:50,916] tech.vtijapan.co.jp.dropwizard.DropwizardApplication: run
INFO [2019-05-22 21:13:50,920] io.dropwizard.server.ServerFactory: Starting DropwizardApplication
INFO [2019-05-22 21:13:51,025] org.eclipse.jetty.setuid.SetUIDListener: Opened application@7b4a0aef{HTTP/1.1,[http/1.1]}{0.0.0.0:8880}
INFO [2019-05-22 21:13:51,025] org.eclipse.jetty.setuid.SetUIDListener: Opened admin@45cec376{HTTP/1.1,[http/1.1]}{0.0.0.0:8081}
INFO [2019-05-22 21:13:51,027] org.eclipse.jetty.server.Server: jetty-9.4.18.v20190429; built: 2019-04-29T20:42:08.989Z; git: e1bc35120a6617ee3df052294e433f3a25ce7097; jvm 1.8.0_181-b13
INFO [2019-05-22 21:13:51,509] io.dropwizard.jersey.DropwizardResourceConfig: The following paths were found for the configured resources:
GET /appName (tech.vtijapan.co.jp.dropwizard.DropwizardResource)
INFO [2019-05-22 21:13:51,510] org.eclipse.jetty.server.handler.ContextHandler: Started i.d.j.MutableServletContextHandler@6d6ac396{/,null,AVAILABLE}
INFO [2019-05-22 21:13:51,514] io.dropwizard.setup.AdminEnvironment: tasks =
POST /tasks/log-level (io.dropwizard.servlets.tasks.LogConfigurationTask)
POST /tasks/gc (io.dropwizard.servlets.tasks.GarbageCollectionTask)
INFO [2019-05-22 21:13:51,519] org.eclipse.jetty.server.handler.ContextHandler: Started i.d.j.MutableServletContextHandler@664e848c{/,null,AVAILABLE}
INFO [2019-05-22 21:13:51,530] org.eclipse.jetty.server.AbstractConnector: Started application@7b4a0aef{HTTP/1.1,[http/1.1]}{0.0.0.0:8880}
INFO [2019-05-22 21:13:51,531] org.eclipse.jetty.server.AbstractConnector: Started admin@45cec376{HTTP/1.1,[http/1.1]}{0.0.0.0:8081}
INFO [2019-05-22 21:13:51,532] org.eclipse.jetty.server.Server: Started @2107ms
|
9. Operation Test
1
2
|
user1noMacBook-puro:Server user1$ curl http://localhost:8880/appName
VTI demo dropwizard
|
Leave a Reply