LoginSignup
6
7

More than 5 years have passed since last update.

spring boot で Groovyアプリを作る(Hello worldまで)

Last updated at Posted at 2016-09-10

目的

spring boot でGroovyを利用した簡単なアプリを動作させる
今回はHello world をブラウザから表示する

Groovyを使う理由

Javaでの本格的な開発の前理に、簡易版のアプリをプロトタイプとして作成するのに使える

環境

  • mac
  • Spring CLI v1.4.0.RELEASE
  • Groovy Version: 2.4.5 JVM: 1.8.0_60 Vendor: Oracle Corporation OS: Mac OS X

手順

Groovyのインストール

Spring Boot CLTを入れる

http://docs.spring.io/spring-boot/docs/current/reference/html/getting-started-installing-spring-boot.html
スクリーンショット 2016-09-11 1.08.38.png

今回はmacのため、tar.gz版をダウンロードし、
任意のディレクトリに配置する

パスを貼る

~/.bash_profile
にパスを設定する

export PATH="$PATH:/workspace_springboot/spring-1.4.0.RELEASE/bin"
PATH="/usr/local/go/bin:$PATH"

再読み込みを行い

source ~/.bash_profile

パスが正しく設定されているか、確認する

echo $PATH

Groovyのコーディング

サンプルに以下のファイルを作成し、任意のワークスペースディレクトリに配置する

app.groovy
@RestController
class App {

    @RequestMapping("/")
    def home(){
        "Hello!!"
    }
}

起動する

app.groovyへ移動し、
実行する

spring run app.groovy

出力

$ spring run app.groovy 
Resolving dependencies.......................

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

2016-09-11 01:04:50.048  INFO 4923 --- [       runner-0] o.s.boot.SpringApplication               : Starting application on kato-no-MacBook-Air.local with PID 4923 (started by katoyuki in /workspace_springboot/workspace)
2016-09-11 01:04:50.128  INFO 4923 --- [       runner-0] o.s.boot.SpringApplication               : No active profile set, falling back to default profiles: default
2016-09-11 01:04:50.666  INFO 4923 --- [       runner-0] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@4b1d6420: startup date [Sun Sep 11 01:04:50 JST 2016]; root of context hierarchy
2016-09-11 01:04:54.620  INFO 4923 --- [       runner-0] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2016-09-11 01:04:54.664  INFO 4923 --- [       runner-0] o.apache.catalina.core.StandardService   : Starting service Tomcat
2016-09-11 01:04:54.668  INFO 4923 --- [       runner-0] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.4
2016-09-11 01:04:54.875  INFO 4923 --- [ost-startStop-1] org.apache.catalina.loader.WebappLoader  : Unknown loader org.springframework.boot.cli.compiler.ExtendedGroovyClassLoader$DefaultScopeParentClassLoader@740ab31 class org.springframework.boot.cli.compiler.ExtendedGroovyClassLoader$DefaultScopeParentClassLoader
2016-09-11 01:04:54.922  INFO 4923 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2016-09-11 01:04:54.922  INFO 4923 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 4256 ms
2016-09-11 01:04:55.192  INFO 4923 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
2016-09-11 01:04:55.208  INFO 4923 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'ch

画面を確認する

この状態で、
http://localhost:8080/
をブラウザで確認する
スクリーンショット 2016-09-11 1.17.30.png

以上、
環境構築まで、

参考

6
7
1

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
6
7