温馨提示×

怎么创建一个简单的SpringBoot应用程序

小亿
82
2024-04-09 17:28:37
栏目: 编程语言

要创建一个简单的Spring Boot 应用程序,可以按照以下步骤操作:

  1. 使用 Spring Initializr 创建一个新的 Spring Boot 项目。可以访问 https://start.spring.io/ 并填写项目的基本信息,如项目名称、依赖等。点击 Generate 按钮下载生成的项目压缩包。

  2. 解压项目压缩包,并使用一个集成开发环境(IDE)打开项目。如使用 IntelliJ IDEA,选择 File -> Open,然后选择解压后的项目文件夹。

  3. 编写一个简单的 Spring Boot 应用程序。在 src/main/java 目录下创建一个包,并在包下创建一个主类,如 MainApplication。在该类上添加 @SpringBootApplication 注解,表示这是一个 Spring Boot 应用程序。

  4. 在主类中添加一个请求处理方法,如下所示:

@RestController
public class HelloWorldController {

    @GetMapping("/hello")
    public String hello() {
        return "Hello, World!";
    }
}
  1. 运行应用程序。在 IDE 中运行 MainApplication 类,或者在终端中切换到项目目录下,执行 ./mvnw spring-boot:run 命令。

  2. 访问 http://localhost:8080/hello,应该能看到浏览器中显示 “Hello, World!”。

这样就创建了一个简单的 Spring Boot 应用程序。可以根据需要添加更多的功能和模块,构建更复杂的应用程序。

0