今天我们尝试Spring Boot整合Kotlin,并决定建立一个非常简单的Spring Boot微服务,使用Kotlin作为编程语言进行编码构建。
  
  创建一个简单的Spring Boot应用程序。我会在这里使用maven构建项目:
  
  <?xml version="1.0" encoding="UTF-8"?>
  
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  
  <modelVersion>4.0.0</modelVersion>
  
  <groupId>com.edurt.ski</groupId>
  
  <artifactId>springboot-kotlin-integration</artifactId>
  
  <version>1.0.0</version>
  
  <packaging>jar</packaging>
  
  <name>springboot kotlin integration</name>
  
  <description>SpringBoot Kotlin Integration is a open source springboot, kotlin integration example.</description>
  
  <parent>
  
  <groupId>org.springframework.boot</groupId>
  
  <artifactId>spring-boot-starter-parent</artifactId>
  
  <version>2.1.3.RELEASE</version>
  
  <relativePath/> <!-- lookup parent from repository -->
  
  </parent>
  
  <properties>
  
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  
  <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  
  <java.version>1.8</java.version>
  
  <!-- plugin config -->
  
  <plugin.maven.kotlin.version>1.2.71</plugin.maven.kotlin.version>
  
  </properties>
  
  <dependencies>
  
  <!-- spring boot -->
  
  <dependency>
  
  <groupId>org.springframework.boot</groupId>
  
  <artifactId>spring-boot-starter-web</artifactId>
  
  </dependency>
  
  <!-- kotlin -->
  
  <dependency>
  
  <groupId>com.fasterxml.jackson.module</groupId>
  
  <artifactId>jackson-module-kotlin</artifactId>
  
  </dependency>
  
  <dependency>
  
  <groupId>org.jetbrains.kotlin</groupId>
  
  <artifactId>kotlin-stdlib-jdk8</artifactId>
  
  </dependency>
  
  <dependency>
  
  <groupId>org.jetbrains.kotlin</groupId>
  
  <artifactId>kotlin-reflect</artifactId>
  
  </dependency>
  
  </dependencies>
  
  <build>
  
  <sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
  
  <testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
  
  <plugins>
  
  <plugin>
  
  <groupId>org.springframework.boot</groupId>
  
  <artifactId>spring-boot-maven-plugin</artifactId>
  
  </plugin>
  
  <plugin>
  
  <artifactId>kotlin-maven-plugin</artifactId>
  
  <groupId>org.jetbrains.kotlin</groupId>
  
  <configuration>
  
  <args>
  
  <arg>-Xjsr305=strict</arg>
  
  </args>
  
  <compilerPlugins>
  
  <plugin>spring</plugin>
  
  <plugin>jpa</plugin>
  
  <plugin>all-open</plugin>
  
  </compilerPlugins>
  
  <pluginOptions>
  
  <option>all-open:annotation=javax.persistence.Entity</option>
  
  </pluginOptions>
  
  </configuration>
  
  <dependencies>
  
  <dependency>
  
  <groupId>org.jetbrains.kotlin</groupId>
  
  <artifactId>kotlin-maven-allopen</artifactId>
  
  <version>${plugin.maven.kotlin.version}</version>
  
  </dependency>
  
  <dependency>
  
  <groupId>org.jetbrains.kotlin</groupId>
  
  <artifactId>kotlin-maven-noarg</artifactId>
  
  <version>${plugin.maven.kotlin.version}</version>
  
  </dependency>
  
  </dependencies>
  
  <executions>
  
  <execution>
  
  <id>kapt</id>
  
  <goals>
  
  <goal>kapt</goal>
  
  </goals>
  
  <configuration>
  
  <sourceDirs>
  
  <sourceDir>src/main/kotlin</sourceDir>
  
  </sourceDirs>
  
  <annotationProcessorPaths>
  
  <annotationProcessorPath>
  
  <groupId>org.springframework.boot</groupId>
  
  <artifactId>spring-boot-configuration-processor</artifactId>
  
  <version>${project.parent.version}</version>
  
  </annotationProcessorPath>
  
  </annotationProcessorPaths>
  
  </configuration>
  
  </execution>
  
  </executions>
  
  </plugin>
  
  </plugins>
  
  </build>
  
  </project>
  
  添加所有必需的依赖项:
  
  kotlin-stdlib-jdk8 kotlin jdk8的lib包
  
  kotlin-reflect kotlin反射包
  
  一个简单的应用类:
  
  package com.edurt.ski
  
  import org.springframework.boot.autoconfigure.SpringBootApplication
  
  import org.springframework.boot.runApplication
  
  @SpringBootApplication
  
  class SpringBootKotlinIntegration
  
  fun main(args: Array<String>) {
  
  runApplication<SpringBootKotlinIntegration>(*args)
  
  }
  
  添加Rest API接口功能
  
  创建一个HelloController Rest API接口,我们只提供一个简单的get请求获取hello,kotlin输出信息:
  
  package com.edurt.ski.controller
  
  import org.springframework.web.bind.annotation.GetMapping
  
  import org.springframework.web.bind.annotation.RestController
  
  @RestController
  
  class HelloController {
  
  @GetMapping(value = "hello")
  
  fun hello(): String {
  
  return "hello,kotlin"
  
  }
  
  }
  
  修改SpringBootKotlinIntegration文件增加以下设置扫描路径
  
  @ComponentScan(value = [
  
  "com.edurt.ski",
  
  "com.edurt.ski.controller"
  
  ])
  
  添加页面功能
  
  修改pom.xml文件增加以下页面依赖
  
  <!-- mustache -->
  
  <dependency>
  
  <groupId>org.springframework.boot</groupId>
  
  <artifactId>spring-boot-starter-mustache</artifactId>
  
  </dependency>
  
  在src/main/resources路径下创建templates文件夹
  
  在templates文件夹下创建一个名为hello.mustache的页面文件
  
  <h1>Hello, Kotlin</h1>
  
  创建页面转换器HelloView
  
  package com.edurt.ski.view
  
  import org.springframework.stereotype.Controller
  
  import org.springframework.www.yongshiyule178.com ui.Model
  
  import org.springframework. www.zhenghongyule.cn web.bind.annotation.GetMapping
  
  @Controller
  
  class HelloView {
  
  @GetMapping(value = "hello_view")
  
  fun helloView(model: Model): String {
  
  return "hello"
  
  }
  
  }
  
  浏览器访问http://localhost:8080/hello_view即可看到页面内容
  
  添加数据持久化功能
  
  修改pom.xml文件增加以下依赖(由于测试功能我们使用h2内存数据库)
  
  <!-- data jpa and db -->
  
  <dependency>
  
  <groupId>org.springframework.boot</groupId>
  
  <artifactId>spring-boot-starter-data-jpa</artifactId>
  
  </dependency>
  
  <dependency>
  
  <groupId>com.h2database<www.xinghaiyule1.com /groupId>
  
  <artifactId>h2</artifactId>
  
  <scope>runtime</scope>
  
  </dependency>
  
  创建User实体
  
  package com.edurt.ski.model
  
  import javax.persistence.Entity
  
  import javax.persistence.GeneratedValue
  
  import javax.persistence.Id
  
  @Entity
  
  //class UserModel(
  
  // @Id
  
  // @GeneratedValue
  
  // private www.zhongyiyuL.cn var id: Long? = 0,
  
  // private var name: String
  
  //)
  
  class UserModel {
  
  @Id
  
  @GeneratedValue
  
  var id: Long? = 0
  
  get() = field
  
  set
  
  var name: String? = null
  
  get() = field
  
  set
  
  }
  
  创建UserSupport dao数据库操作工具类
  
  package com.edurt.ski.support
  
  import com.edurt.ski.www.jiahuayulpt.com model.UserModel
  
  import org.springframework.data.repository.PagingAndSortingRepository
  
  interface UserSupport : PagingAndSortingRepository<UserModel, Long> {
  
  }
  
  创建UserService服务类
  
  package com.edurt.ski.service
  
  import com.edurt.ski.model.UserModel
  
  interface UserService {
  
  /**
  
  * save model to db
  
  */
  
  fun save(model: UserModel): UserModel
  
  }
  
  创建UserServiceImpl实现类
  
  package com.edurt.www.feifanyule.cn/ ski.service
  
  import com.edurt.ski.model.UserModel
  
  import com.edurt.ski.support.UserSupport
  
  import org.springframework.www.tiaotiaoylzc.com stereotype.Service
  
  @Service(value = "userService")
  
  class UserServiceImpl(private val userSupport: UserSupport) : UserService {
  
  override fun save(model: UserModel): UserModel {
  
  return this.userSupport.www.mytxyl1.com save(model)
  
  }
  
  }
  
  创建用户UserController进行持久化数据
  
  package com.edurt.ski.controller
  
  import com.edurt.ski.model.UserModel
  
  import com.edurt.ski.service.UserService
  
  import org.springframework.web.bind.annotation.PathVariable
  
  import org.springframework.web.bind.annotation.PostMapping
  
  import org.springframework.web.bind.annotation.RequestMapping
  
  import org.springframework.web.bind.annotation.RestController
  
  @RestController
  
  @RequestMapping(value dasheng178.com= "user")
  
  class UserController(private val userService: UserService) {
  
  @PostMapping(value = "save/{name}")
  
  fun save(@PathVariable name: String): UserModel {
  
  val userModel = UserModel()
  
  // userModel.id = 1
  
  userModel.name = name
  
  return this.userService.save(userModel)
  
  }
  
  }
  
  使用控制台窗口执行以下命令保存数据
  
  curl -X POST http://localhost:8080/user/save/qianmoQ
  
  收到返回结果
  
  {"id":1,"name":"qianmoQ"}
  
  表示数据保存成功
  
  增加数据读取渲染功能
  
  修改UserService增加以下代码
  
  /**
  
  * get all model
  
  */
  
  fun getAll(page: Pageable): Page<UserModel>
  
  修改UserServiceImpl增加以下代码
  
  override fun getAll(page: Pageable): Page<UserModel> {
  
  return this.userSupport.findAll(page)
  
  }
  
  修改UserController增加以下代码
  
  @GetMapping(value = "list")
  
  fun get(): Page<UserModel> = this.userService.getAll(PageRequest(0, 10))
  
  创建UserView文件渲染User数据
  
  package com.edurt.ski.view
  
  import com.edurt.ski.service.UserService
  
  import org.springframework.www.yongshi123.cn data.domain.PageRequest
  
  import org.springframework.stereotype.Controller
  
  import org.springframework.ui.Model
  
  import org.springframework.ui.set
  
  import org.springframework.web.www.yigouyule2.cn bind.annotation.GetMapping
  
  @Controller
  
  class UserView(private val userService: UserService) {
  
  @GetMapping(value = "user_view")
  
  fun helloView(model: Model): String {
  
  model["users"] = this.userService.getAll(PageRequest(0, 10))
  
  return "user"
  
  }
  
  }
  
  创建user.mustache文件渲染数据(自行解析返回数据即可)
  
  {{users}}
  
  浏览器访问http://localhost:8080/user_view即可看到页面内容
  
  增加单元功能
  
  修改pom.xml文件增加以下依赖
  
  <!-- test -->
  
  <dependency>
  
  <groupId>org.springframework.boot</groupId>
  
  <artifactId>spring-boot-starter-test</artifactId>
  
  <scope>test</scope>
  
  <exclusions>
  
  <exclusion>
  
  <groupId>junit</groupId>
  
  <artifactId>junit</artifactId>
  
  </exclusion>
  
  <exclusion>
  
  <groupId>org.mockito</groupId>
  
  <artifactId>mockito-core</artifactId>
  
  </exclusion>
  
  </exclusions>
  
  </dependency>
  
  <dependency>
  
  <groupId>org.junit.jupiter</groupId>
  
  <artifactId>junit-jupiter-engine</artifactId>
  
  <scope>test</scope>
  
  </dependency>
  
  创建UserServiceTest文件进行测试UserService功能
  
  package com.edurt.ski
  
  import com.edurt.ski.service.UserService
  
  import org.junit.jupiter.api.AfterAll
  
  import org.junit.jupiter.api.Test
  
  import org.springframework.beans.factory.annotation.Autowired
  
  import org.springframework.boot.test.context.SpringBootTest
  
  import org.springframework.data.domain.PageRequest
  
  @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
  
  class UserServiceTest(@Autowired private val userService: UserService) {
  
  @Test
  
  fun `get all`() {
  
  println(">> Assert blog page title, content and status code")
  
  val entity = this.userService.getAll(PageRequest(0, 1))
  
  print(entity.totalPages)
  
  }
  
  }
  
  源码地址:GitHub

SpringBoot整合Kotlin构建Web服务的更多相关文章

  1. Spring-Boot:Spring Cloud构建微服务架构

    概述: 从上一篇博客<Spring-boot:5分钟整合Dubbo构建分布式服务> 过度到Spring Cloud,我们将开始学习如何使用Spring Cloud 来搭建微服务.继续采用上 ...

  2. SpringBoot整合阿里短信服务

    导读 由于最近手头上需要做个Message Gateway,涉及到:邮件(点我直达).短信.公众号(点我直达)等推送功能,网上学习下,整理下来以备以后使用. 步骤 点我直达 登录短信服务控制台 点我直 ...

  3. 极简SpringBoot指南-Chapter04-基于SpringBoot的书籍管理Web服务

    仓库地址 w4ngzhen/springboot-simple-guide: This is a project that guides SpringBoot users to get started ...

  4. Spring-boot:5分钟整合Dubbo构建分布式服务

    概述: Dubbo是Alibaba开源的分布式服务框架,它最大的特点是按照分层的方式来架构,使用这种方式可以使各个层之间解耦合(或者最大限度地松耦合).从服务模型的角度来看,Dubbo采用的是一种非常 ...

  5. 第2-1-4章 SpringBoot整合FastDFS文件存储服务

    目录 5 SpringBoot整合 5.1 操作步骤 5.2 项目依赖 5.3 客户端开发 5.3.1 FastDFS配置 5.3.2 FastDFS配置类 5.3.3 文件工具类 5.3.4 文件上 ...

  6. dubbo学习实践(4)之Springboot整合Dubbo及Hystrix服务熔断降级

    1. springboot整合dubbo 在provider端,添加maven引入,修改pom.xml文件 引入springboot,版本:2.3.2.RELEASE,dubbo(org.apache ...

  7. 使用ServiceStack构建Web服务

    提到构建WebService服务,大家肯定第一个想到的是使用WCF,因为简单快捷嘛.首先要说明的是,本人对WCF不太了解,但是想快速建立一个WebService,于是看到了MSDN上的这一篇文章 Bu ...

  8. SpringBoot整合redis哨兵主从服务

    前提环境: 主从配置 http://www.cnblogs.com/zwcry/p/9046207.html 哨兵配置 https://www.cnblogs.com/zwcry/p/9134721. ...

  9. (转)使用ServiceStack构建Web服务

    提到构建WebService服务,大家肯定第一个想到的是使用WCF,因为简单快捷嘛.首先要说明的是,本人对WCF不太了解,但是想快速建立一个WebService,于是看到了MSDN上的这一篇文章 Bu ...

随机推荐

  1. Jmeter性能指标分析

    以下是下载了服务器监控插件的各个组件的功能介绍,有助于以后jmeter的性能测试 1.jp@gc - Actiive Threads Over Time:不同时间的活动用户数量展示(图表) 当前的时间 ...

  2. Unity特殊文件夹详解

    ##1.Editor Editor文件夹可以在根目录下,也可以在子目录里,只要名子叫Editor就可以.比如目录:/xxx/xxx/Editor 和 /Editor 是一样的,无论多少个叫Editor ...

  3. AssetBundle粒度与分配策略

    决定如何将项目内的资源分配到 AssetBundle 是不容易的.简单的规则都很有诱惑性,比如将所有对象都放置到他们自己的 AssetBundle 中或者将所有对象都放到一个 AssetBundle ...

  4. Unity学习笔记(5):动态加载Prefab

    第一种方法,从Resources文件夹读取Prefab Assets/Resources文件夹是Unity中的一个特殊文件夹,在博主当前的认知里,放在这个文件夹里的Prefab可以被代码动态加载 直接 ...

  5. shell解析ini格式文件

    功能 本脚本实现了ini文件中的查询修改指定value 百度云连接地址 链接:https://pan.baidu.com/s/12_T5yST7Y3L1H4_MkVEcvA 密码:fo5p 解压后先看 ...

  6. Appengine直接下载文件并保存到google drive

    一直对下载文件比较感兴趣.前些日子无意搜到google 推出一项服务,可以直接将文件下载到google drive中,原型猛戳这里,但有限额限制.一时脑洞大开,可不可以在appengine 上架设服务 ...

  7. Daily Scrum (2015/10/23)

    这天晚上PM和我一起细算下来这周的确做了不少事儿.由于这天是周五,有的组员今晚有外出活动,有的组员忙了一周想休息一下.所以PM与我讨论提出今晚就布置些阅读的任务,给组员们一些自由分配的时间: 成员 今 ...

  8. SQL之联合查询学习笔记

    定义: 联合查询可合并多个相似的选择查询的结果集.等同于将一个表追加到另一个表,从而实现将两个表的查询组合到一起,使用谓词为UNION或UNION ALL. 语法格式 UNION 可以将两个或两个以上 ...

  9. 404 Note Found Team's First Blood

    团队构成: 队员学号姓名队长标注: 031602114--胡绪佩(队长) 031602113--何宇恒 081600410--胡青元 031602627--刘恺琳 031602525--刘一好 031 ...

  10. Alpha阶段敏捷冲刺②

    1.提供当天站立式会议照片一张 每个人的工作 (有work item 的ID),并将其记录在码云项目管理中: 昨天已完成的工作. 购买云服务器 注册账号 界面布局初步规划 今天计划完成的工作. 界面雏 ...