LoginSignup
1
0

More than 5 years have passed since last update.

初めてのDropwizardチュートリアル

Last updated at Posted at 2015-11-23

Dropwizardとは

公式HPより抜粋
http://www.dropwizard.io/0.9.1/docs/
Dropwizard is a Java framework for developing ops-friendly, high-performance, RESTful web services.

やること

JavaのWebアプリケーションフレームワークDropwizardのチュートリアルを書経していきます(やってることは公式HPのチュートリアルをそのまま日本語で説明しているだけです)

環境

  • 実行環境
    OS: Mac OS X (10.11.1 Ei Capitan)

  • 開発環境
    IntelliJ IDEA 14.1.5
    Java 1.8.0
    Dropwizard 0.9.1
    maven 3.3.3

    (注)本チュートリアルは,Mavenをビルドツールとして使っていきます

手順

1.Mavenプロジェクトの作成
2.各種ソースコードを書経
 2.1.Configurationクラスの作成(フォローなし)
 2.2.Applicationクラスの作成(フォローなし)
 2.3.Represenationクラスの作成(フォローなし)
 2.4.Resourceクラスの作成(フォローなし)
3.ビルド
4.確認

という流れでやっていきます

1. Mavenプロジェクトの作成

IntelliJ IDEA上でMavenプロジェクトを作っていきます
Screen Shot 2015-11-23 at 10.05.26 PM.png

1.1 pom.xmlの記述

公式HPにしたがい,dropwizardの箇所をpom.xmlに追加します
<properties>
<dropwizard.version>INSERT VERSION HERE</dropwizard.version>
</properties>
私の場合,上のversionを0.9.1に設定しました

dependencyも以下のようにpom.xmlに追加します

      <dependencies>
        <dependency>
            <groupId>io.dropwizard</groupId>
            <artifactId>dropwizard-core</artifactId>
            <version>${dropwizard.version}</version>
        </dependency>
       </dependencies>

2.各種ソースコードを書経

これは,公式HPをみながら各自対応お願いします(時間がない方は,コピペでもいいと思います)
各種クラスの説明については公式HPに載っているので,一旦ここでは説明を端折らせてもらいます.
気になる方は公式HPまたはすでにDropwizardについて記述されたこちらの方の記事などを参照お願いします
- http://qiita.com/hina0118/items/0886ed0e1a84d6077444

3.ビルド

dropwizardではすべてのクラスを含んだfat jarを作成することを推奨しています.
したがって,そのようなファイルが作成できるようにpom.xmlを編集します.
ポイント1: 外部jarは署名つきのものとして扱うように以下を設定
ポイント2: 次のようにjarで固めるので,mainClassがどこかを明示的に指定してあげること:

4.実行

プロジェクトのトップディクレクトリ以下で次のコマンドを実行
java -jar target/dropwizard-examples-1.0-SNAPSHOT.jar server hello-world.yml

http://localhost:8080/hello-world
にブラウザでアクセスすると次のようなjsonが確認できると思います

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

最終的な成果物は次のGithubリポジトリを参照ください

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0