LoginSignup
5
3

More than 5 years have passed since last update.

Spring Bootでgroovyを使う

Posted at

はじめに

Spring Bootでgroovyを使うときのGradleの設定を紹介します。

手順

まずSpring BootをHerokuにデプロイするのが劇的に簡単になっている件で紹介した、SpringInitializrを使ったプロジェクトを流用します。

  • build.gradleに以下を設定
    • pluginにgroovyを追加する
    • dependenciesにgroovy-allを追加する
  • groovyのパッケージを作成
  • groovyのクラスを作成
  • 起動確認

build.gradleの内容

auto importをONにしていない場合は、build.gradleファイルを修正した後に、refreshしないと追加したパッケージが読み込まれません。

buildscript {
    ext {
        springBootVersion = '1.4.2.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'groovy' // ココを追加
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'

jar {
    baseName = 'demo'
    version = '0.0.1-SNAPSHOT'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
    mavenCentral()
}


dependencies {
    compile('org.codehaus.groovy:groovy-all') // ココを追加
    compile('org.springframework.boot:spring-boot-starter-actuator')
    compile('org.springframework.boot:spring-boot-starter-web')
    runtime('org.postgresql:postgresql')
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

groovyのパッケージを作成

Projectペインのmainフォルダを右クリックして、「New」->「Directory」を選択

image

「New Directory」ダイアログにgroovyと入力

image

作成したgroovyフォルダを右クリックして、「Mark Directory as」メニューから「Sources Root」を選択

image
groovyフォルダが青色に変わる
image

groovyフォルダを右クリックして、「New」->「package」

image

「New Package」ダイアログにcom.example.controllerを入力

※パッケージ名はなんでもよいがSpringBootのmainクラスと同階層、または配下の階層を作成する。(ComponentScanの対象にするため)
image

作成したパッケージを右クリックして、「New」->「Groovy Class」を選択

※この時点でこの「Groovy Class」の選択肢がでてこない場合は、build.gradleの設定ミスか、build.gradleを更新していない可能性があります。
image

「New Groovy Class」ダイアログに適当なクラス名を追加

image
Groovyのクラスが作成される
image

作成したGroovyクラスを以下のとおり編集する(クラス名は変えない)

package com.example.controller

import org.springframework.stereotype.Component
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.ResponseBody

/**
 *
 */
@Component
@RequestMapping("/demo")
class DemoController {
  @GetMapping("hello")
  @ResponseBody
  def hello() {
    return "Hello"
  }
}

JavaのほうのSpringBootのmainクラスを右クリックして、「Run DemoApplication」を選択

image

コンソールに起動ログが表示される

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.4.2.RELEASE)

2016-11-19 23:43:43.459  INFO 29071 --- [           main] com.example.DemoApplication              : Starting DemoApplication on MBP-15UAC-184.local with PID 29071 (/Users/tgoto/Develop/git/demo/build/classes/main started by tgoto in /Users/tgoto/Develop/git/demo)
2016-11-19 23:43:43.463  INFO 29071 --- [           main] com.example.DemoApplication              : No active profile set, falling back to default profiles: default
2016-11-19 23:43:43.900  INFO 29071 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@7722c3c3: startup date [Sat Nov 19 23:43:43 JST 2016]; root of context hierarchy
2016-11-19 23:43:45.222  INFO 29071 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2016-11-19 23:43:45.235  INFO 29071 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat
2016-11-19 23:43:45.236  INFO 29071 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.6
2016-11-19 23:43:45.339  INFO 29071 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2016-11-19 23:43:45.339  INFO 29071 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1441 ms
2016-11-19 23:43:45.565  INFO 29071 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
2016-11-19 23:43:45.570  INFO 29071 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'metricsFilter' to: [/*]
2016-11-19 23:43:45.570  INFO 29071 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2016-11-19 23:43:45.570  INFO 29071 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2016-11-19 23:43:45.570  INFO 29071 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2016-11-19 23:43:45.570  INFO 29071 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2016-11-19 23:43:45.570  INFO 29071 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'webRequestLoggingFilter' to: [/*]
2016-11-19 23:43:45.570  INFO 29071 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'applicationContextIdFilter' to: [/*]
2016-11-19 23:43:45.891  INFO 29071 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@7722c3c3: startup date [Sat Nov 19 23:43:43 JST 2016]; root of context hierarchy
2016-11-19 23:43:45.956  INFO 29071 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/demo/hello],methods=[GET]}" onto public java.lang.Object com.example.controller.DemoController.hello()
2016-11-19 23:43:45.958  INFO 29071 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
・・・中略・・・
org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2016-11-19 23:43:46.827  INFO 29071 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2016-11-19 23:43:46.835  INFO 29071 --- [           main] o.s.c.support.DefaultLifecycleProcessor  : Starting beans in phase 0
2016-11-19 23:43:46.940  INFO 29071 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2016-11-19 23:43:46.944  INFO 29071 --- [           main] com.example.DemoApplication              : Started DemoApplication in 4.224 seconds (JVM running for 4.732)

以下のような/demo/helloのリクエストマップが表示されるとOK

2016-11-19 23:43:45.956  INFO 29071 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/demo/hello],methods=[GET]}" onto public java.lang.Object com.example.controller.DemoController.hello()

curlで確認

~/D/g/demo ❯❯❯ curl -X GET localhost:8080/demo/hello                                                                                                                                    [master]
Hello%                                                                                                                                                                                           ~/D/g/demo ❯❯❯    

できた

5
3
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
5
3