Kotlin+SpringBoot服务端restful框架搭建(1)
Kotlin+SpringBoot服务端restful框架搭建(1)
本文的组要目的是完成SpringBoot集成Kotlin的一个基本项目构建,通过这个例子能够对kotlin有一个简单的了解。与其看着枯燥的教程,看看它的使用也许会更有意思。
kotlin简介
Kotlin是一个基于JVM的编程语言,由JetBrains设计开发并开源。Kotlin可以编译成Java字节码也可以编译成JavaScript,支持开发Android,并且谷歌已经明确将Kotlin作为Android的一级开发语言。最新的Spring5版本也专门引入了对于Kotlin的支持。Kotlin拥有更简洁的代码,完全兼容java,而且同java可以实现完全的互调。使用Kotlin你起码可以少些30%的代码。
要求
- JDK 1.8
- maven构建
- Spring4
- IDEA Intellij
- Kotlin 1.2.31
快速构建
首先构建一个maven项目,我们命名为KotlinDemo。(使用idea构建Kotlin项目需要按照Kotlin的插件。)
1.先来修改pom.xml添加需要使用的依赖。 引入spring-boot-starter-parent,这里使用1.5.6版本
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.6.RELEASE</version> </parent>
添加需要的依赖
<properties> <java.version>1.8</java.version> <mybatis.spring.version>1.2.4</mybatis.spring.version> <kotlin.compiler.incremental>true</kotlin.compiler.incremental> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <kotlin.version>1.2.31</kotlin.version> </properties>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-stdlib-jre8</artifactId> <version>${kotlin.version}</version> </dependency> <dependency> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-reflect</artifactId> <version>${kotlin.version}</version> </dependency>
在build中添加插件
<plugin> <artifactId>kotlin-maven-plugin</artifactId> <groupId>org.jetbrains.kotlin</groupId> <version>1.2.31</version> <configuration> <compilerPlugins> <plugin>spring</plugin> </compilerPlugins> <jvmTarget>1.8</jvmTarget> </configuration> <executions> <execution> <id>compile</id> <phase>compile</phase> <goals> <goal>compile</goal> </goals> </execution> <execution> <id>test-compile</id> <phase>test-compile</phase> <goals> <goal>test-compile</goal> </goals> </execution> </executions> <dependencies> <dependency> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-maven-allopen</artifactId> <version>1.2.31</version> </dependency> </dependencies> </plugin>
2.创建application.yml
server: context-path: /kotlin port: 8090
3.创建Application.kt文件
创建类的方法同java相似,也是引入各种包,但是需要注意Kotlin的main方法是class外面和java的main方法区别
添加spring的注解@RestController和@SpringBootApplication,我们在class里面创建一个简单的restfull格式的请求方法hello(),方法返一个String的字符串。
添加@RequestMapping时候需要注意设置method的时候,这里method接收的是一个数组,不是RequestMethod.GE,所以我们需要通过kotlin提供的arrayOf()方法转换一个下,这是一个创建数据的方法,具体的可以参考一下kotlin的文档。
注意一下 SpringApplication.run中参数这个和java的区别有点大
Application.kt类代码
package demo import org.springframework.boot.SpringApplication @RestController @SpringBootApplication class Application{ @RequestMapping(value = "/hello",method =arrayOf(RequestMethod.GET)) fun hello(): String { return "hello kotlin"; } } fun main(args: Array<String>){ //::符号是得到这个类的class对象 SpringApplication.run(Application::class.java,*args); }
3.项目基本搭建完成,试试启动项目访问http://localhost:8090/demo/hello,页面会返回hello kotlin。

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
Windows 10 搭建Redis 集群环境
Windows 10 搭建Redis 集群环境 1、下载最新的redis安装包:redis下载地址 2.解压redis压缩包后,将redis.conf 文件进行修改,修改关键代码如下: [html] view plain copy port7005 cluster-enabledyes cluster-config-filenodes.conf cluster-node-timeout5000 appendonlyyes 文件中的cluster-enabled选项用于开实例的集群模式, 而cluster-conf-file选项则设定了保存节点配置文件的路径, 默认值为nodes.conf。 3、为了能够演示集群3主3从的环境,我们分别在redis解压包的根目录下建立6个文件夹,文件夹名称以端口号命名,将redis.conf 分别拷贝到对应的端口文件夹下面,并修改对应文件中的端口地址及cluster-config-file 后面的文件名 4、使用下面的命令,启动对应的redis 服务器 redis-server ./redis.conf Note:实例会一直使用同一个 ID , 从而在集...
- 下一篇
记安装pyspectator填坑记
原文链接:一只电工的博客 记安装pyspectator填坑记: Environment INFO: windows10 + python3.5 1、正常安装官网 # Run as root user: pip install -U pyspectator_tornado Error INFO: Collecting pyspectator_tornado Using cached pyspectator_tornado-1.1.2-py3-none-any.whl Collecting pyspectator>=1.1.2 (from pyspectator_tornado) Using cached pyspectator-1.2.1.tar.gz Complete output from command python setup.py egg_info: error in pyspectator setup command: 'install_requires' must be a string or list of strings containing valid proj...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- Eclipse初始化配置,告别卡顿、闪退、编译时间过长
- 设置Eclipse缩进为4个空格,增强代码规范
- Hadoop3单机部署,实现最简伪集群
- CentOS7编译安装Gcc9.2.0,解决mysql等软件编译问题
- CentOS7编译安装Cmake3.16.3,解决mysql等软件编译问题
- 2048小游戏-低调大师作品
- CentOS6,CentOS7官方镜像安装Oracle11G
- Jdk安装(Linux,MacOS,Windows),包含三大操作系统的最全安装
- SpringBoot2更换Tomcat为Jetty,小型站点的福音
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池