在Spring Boot中使用Groovy

首先

下面是使用Spring Boot和Groovy时的Gradle配置介绍。

操作步骤

首先,我们将使用SpringInitializr创建的项目来介绍一个劇变的简单方法——将Spring Boot部署到Heroku。

    • build.gradleに以下を設定

pluginにgroovyを追加する
dependenciesにgroovy-allを追加する

groovyのパッケージを作成
groovyのクラスを作成
起動確認

构建.gradle文件的内容

如果未启用自动导入功能,则在修改build.gradle文件后,必须刷新才能加载添加的软件包。

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文件夹,然后选择「新建」-> 「目录」。

image

在「新目录」对话框中输入groovy。

image

右击已创建的groovy文件夹,选择”标记目录为”菜单中的”源代码根目录”。

image

右键单击groovy文件夹,选择「新建」->「包」。

image

在「新包装」对话框中输入com.example.controller。

image

点击已创建的包,选择“新建”->“Groovy类”。

image

在「New Groovy Class」的对话框中添加一个合适的类名。

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类,然后选择“运行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的请求映射时,表示正常。

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

完成了 le).

广告
将在 10 秒后关闭
bannerAds