1. 新建一个Kotlin项目,选择Spring Initializr,并选择Spring Web和Kotlin作为依赖。

  2. 在项目的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!"
    }
}
  1. 在项目的src/main/resources/application.properties文件中添加以下内容:
server.port=8081
  1. 运行程序,打开浏览器访问http://localhost:8081/,即可看到输出的“Hello, World!”。

注意:如果端口号修改为其他值,则需要访问http://localhost:端口号/来看到输出的“Hello, World!”

怎么用Kotlin写一个简单的Helloworld内容的Spring boot30程序并修改端口

原文地址: https://www.cveoy.top/t/topic/ceFa 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录