使用Spring Boot框架的Kotlin语言

首先

根据传闻,Kotlin已经进入了开发领域,我尝试在Spring Boot中使用它进行了实验。
以下是我昨天写的那篇文章的Kotlin版本。

在Spring Boot中使用Groovy

进行步骤

同样如上一次所介绍的那样,关于在Heroku上部署Spring Boot变得非常简单,我们将使用SpringInitializr创建的项目来重复使用。

    • build.gradleに以下を設定

buildscript.extにkotlin_versionを追加する
buildscript.dependenciesにkotlin-gradle-pluginを追加する
pluginにkotlinを追加する
dependenciesにkotlin-stdlibを追加する

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

build.gradle的内容是什么。

编辑完build.gradle文件后,不要忘记进行更新…

buildscript {
    ext {
        springBootVersion = '1.4.2.RELEASE'
        kotlin_version = '1.0.5-2' // ココを追加 
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlin_version}"
    }
}

apply plugin: 'java'
apply plugin: 'kotlin' // ココを追加
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.jetbrains.kotlin:kotlin-stdlib:${kotlin_version}")  // ココを追加
    compile('org.springframework.boot:spring-boot-starter-actuator')
    compile('org.springframework.boot:spring-boot-starter-web')
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

创建 Kotlin 的包(package)。

右键点击Projectペイン的main文件夹,选择“新建”->“目录”。

image

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

image

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

image

请右键点击kotlin文件夹,然后选择“新建”->“包”。

image

请在“新包”对话框中输入com.example.controller。

image

创建一个Kotlin类。

右键点击创建的包,选择“新建”->“Kotlin文件/类”。

image

在「新增 Kotlin 文件/类」对话框中添加一个合适的类名。

image

在Kotlin中创建了一个类。

image

把Kotlin类按以下方式进行修改

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
    fun hello(): String {
        return "hello"
    }
}

在Java中,右键点击Spring Boot的主类,选择”运行DemoApplication”。

image

Spring Boot的启动控制台

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

2016-11-20 10:46:08.812  INFO 36216 --- [           main] com.example.DemoApplication              : Starting DemoApplication on softbank221109076128.bbtec.net with PID 36216 (/Users/tgoto/Develop/git/demo/build/classes/main started by tgoto in /Users/tgoto/Develop/git/demo)
2016-11-20 10:46:08.822  INFO 36216 --- [           main] com.example.DemoApplication              : No active profile set, falling back to default profiles: default
2016-11-20 10:46:09.499  INFO 36216 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@7722c3c3: startup date [Sun Nov 20 10:46:09 JST 2016]; root of context hierarchy
2016-11-20 10:46:10.750  INFO 36216 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2016-11-20 10:46:10.762  INFO 36216 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat
2016-11-20 10:46:10.763  INFO 36216 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.6
2016-11-20 10:46:10.841  INFO 36216 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2016-11-20 10:46:10.841  INFO 36216 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1345 ms
2016-11-20 10:46:11.033  INFO 36216 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
2016-11-20 10:46:11.035  INFO 36216 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'metricsFilter' to: [/*]
2016-11-20 10:46:11.036  INFO 36216 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2016-11-20 10:46:11.036  INFO 36216 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2016-11-20 10:46:11.036  INFO 36216 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2016-11-20 10:46:11.036  INFO 36216 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2016-11-20 10:46:11.036  INFO 36216 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'webRequestLoggingFilter' to: [/*]
2016-11-20 10:46:11.036  INFO 36216 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'applicationContextIdFilter' to: [/*]
2016-11-20 10:46:11.402  INFO 36216 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@7722c3c3: startup date [Sun Nov 20 10:46:09 JST 2016]; root of context hierarchy
2016-11-20 10:46:11.476  INFO 36216 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/demo/hello],methods=[GET]}" onto public final java.lang.String com.example.controller.DemoController.hello()
・・・中略・・・
o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/beans || /beans.json],methods=[GET],produces=[application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2016-11-20 10:46:12.129  INFO 36216 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2016-11-20 10:46:12.301  INFO 36216 --- [           main] o.s.c.support.DefaultLifecycleProcessor  : Starting beans in phase 0
2016-11-20 10:46:12.417  INFO 36216 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2016-11-20 10:46:12.420  INFO 36216 --- [           main] com.example.DemoApplication              : Started DemoApplication in 4.071 seconds (JVM running for 4.458)

如果显示以下日志,则表示OK。

2016-11-20 10:46:11.476  INFO 36216 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/demo/hello],methods=[GET]}" onto public final java.lang.String com.example.controller.DemoController.hello()

开机确认

用Curl进行确认

~/D/g/demo ❯❯❯ curl -X GET localhost:8080/demo/hello                                                                                                                                    hello%   

可以将它部署到 heroku。

bannerAds