dropwizard + Gradle

  • Eclipse Marketplaceでgradleをインストール

  • 新規プロジェクト作成の時に「Gradle(STS)」を選択して作成。

  • build.gradleを編集。

apply plugin: 'idea'
apply plugin: 'java'

def defaultEncoding = 'UTF-8'
def jdkVersion = '1.8'

repositories {
    mavenCentral()
}

dependencies {
    compile 'io.dropwizard:dropwizard-core:1.0.3'
    compile 'io.dropwizard:dropwizard-configuration:1.0.3'
}

compileJava {
    options.encoding = defaultEncoding
}
compileTestJava {
    options.encoding = defaultEncoding
}

idea {
    project {
        jdkName = jdkVersion
        languageLevel = jdkVersion
    }
}

jar {
    from (configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }) {
        exclude 'META-INF/MANIFEST.MF'
        exclude 'META-INF/*.SF'
        exclude 'META-INF/*.DSA'
        exclude 'META-INF/*.RSA'
    }
    manifest {
        attributes('Main-Class': 'org.gradle.service.HelloWorldService')
    }
}
  • gradle jarを叩く。

  • プロジェクトを右クリックしてGradle(STS)> Refresh Dependenciesを選択。
    これでdependenciesが反映される。

  • クラスの内容を、Dropwizard入門 - Qiitaに従って書いていく。

  • java -jar build/libs/gtest.jar server hello-world.ymlで実行。




エラー

java -jar build/libs/gtest.jar server hello-world.ymlを実行すると

エラー: メイン・クラスorg.gradle.service.HelloWorldServiceが見つからなかったかロードできませんでした

と出てくる。

HelloWorldServiceクラスは存在するのにさあどうして。




解決

gradle build

からの

java -jar build/libs/gtest.jar server hello-world.yml

で再びエラー

hello-world.yml has an error:
  * Failed to parse configuration at: logging.appenders.[0]; Could not resolve type id 'console' into a subtype of [simple type, class io.dropwizard.logging.AppenderFactory<ch.qos.logback.classic.spi.ILoggingEvent>]: known type ids = [AppenderFactory]
 at [Source: N/A; line: -1, column: -1] (through reference chain: org.gradle.configuration.HelloWorldConfiguration["logging"]->io.dropwizard.logging.DefaultLoggingFactory["appenders"]->java.util.ArrayList[0])



hello-world.ymlのlogginappendersコメントアウト

hello-world.ymlファイル

template: "Hello, %s!"

# use the simple server factory if you only want to run on a single port

server:
  applicationConnectors:
    - type: http
      port: 28080
  adminConnectors:
    - type: http
      port: 28081

## logging settings.
#logging:
#
#  # the default level of all loggers. Can be OFF, ERROR, WARN, INFO, DEBUG, TRACE, or ALL.
#  level: DEBUG
#
#  appenders:
#    - type: console
#      timeZone: JST

コマンドで再び

java -jar build/libs/gtest.jar server hello-world.yml

http://localhost:28080/hello?name=hogeにアクセスすると
{"message":"Hello, hoge!"}と無事表示されました。