Kotlin Spring Boot 3.0 快速入门:编写简单的 Hello World 程序并修改端口
Kotlin Spring Boot 3.0 快速入门:编写简单的 Hello World 程序并修改端口
本文将带您一步一步使用 Kotlin 编写一个简单的 Hello World Spring Boot 3.0 程序,并演示如何修改默认端口。从创建项目到运行程序,详细说明每个步骤,帮助您快速上手 Kotlin Spring Boot 开发。
1. 创建项目
首先,您需要创建一个新的 Kotlin 项目。使用 Spring Initializr,并选择 Spring Web 和 Kotlin 作为依赖。
2. 编写 HelloController.kt
在项目的 src/main/kotlin 文件夹下新建一个名为 HelloController.kt 的文件,代码如下:
package com.example.demo
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RestController
@RestController
class HelloController {
@GetMapping("/")
fun hello(): String {
return 'Hello, World!'
}
}
3. 修改端口
在项目的 src/main/resources/application.properties 文件中添加以下内容:
server.port=8081
4. 运行程序
运行程序后,打开浏览器访问 http://localhost:8081/,即可看到输出的 'Hello, World!'。
注意: 如果您将端口号修改为其他值,则需要访问 http://localhost:端口号/ 来看到输出的 'Hello, World!'。
原文地址: https://www.cveoy.top/t/topic/nrwm 著作权归作者所有。请勿转载和采集!