概述
2019独角兽企业重金招聘Python工程师标准>>>
[KotCloud] maven配置springboot + kotlin
pom.xml
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<kotlin.version>1.2.31</kotlin.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jre8</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-test</artifactId>
<version>${kotlin.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<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>
<configuration>
<jvmTarget>${java.version}</jvmTarget>
</configuration>
</plugin>
</plugins>
</build>
配置maven结构目录:
src --
|--main
|--java
|--com.kotcloud
|--controller
|--IndexController.kt
|--KotCloudApp.kt
|--resource
|--webapp
|--test
.......
.......
- java目录下 , 一定要有二级目录, 否则springboot不会自动识别注解
- App文件需要在子文件的最外层
App.kt
import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication
@SpringBootApplication
open class KotCloudApp
fun main(args : Array<String>){
SpringApplication.run(KotCloudApp::class.java,*args)
}
- class必须是open的,即public的.
IndexController.kt
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
@RestController
open class IndexController{
@RequestMapping("/index")
fun index(id:Int?) : String{
return id?.toString()?:"0"
}
}
- class必须是open的,即public的.
- fun必须在class体内 , 不能写在class外, 否则springboot识别不到注解
以下是错误的:
// 这是错误的方式 方法index不会被识别
@RestController
open class IndexController
@RequestMapping("/index")
fun index(id:Int?) : String{
return id?.toString()?:"0"
}
启动
- 启动服务的host , 及context (context默认根目录: "/")
- 当前已经识别到的url地址
- 服务启动的端口号: 8080 (默认使用tomcat服务,tomcat默认端口号为:8080)
请求服务
- localhost:8080/index
- 0
- localhost:8080/index?id=123
- 123
转载于:https://my.oschina.net/ElEGenT/blog/1791133
最后
以上就是务实钥匙为你收集整理的[kotcloud] kotlin + springboot (一)初始配置的全部内容,希望文章能够帮你解决[kotcloud] kotlin + springboot (一)初始配置所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复