springboot版helloworld

Spring Boot HelloWorld项目搭建
本文介绍了Spring Boot HelloWorld项目的搭建过程。首先构建Maven工程并添加父依赖,接着编写Java代码,然后运行项目,最后展示了运行效果。
  1. 1构建maven工程,添加父依赖

  1. 2写java代码

  1. 3运行

  1. 4效果

### 使用 Spring Boot 编写 Hello World 示例 为了实现一个简单的 `Hello World` 应用程序,可以遵循以下说明: #### 准备工作环境 确保已安装合适的集成开发环境 (IDE),如 IntelliJ IDEA 或 Eclipse。对于新手来说,IntelliJ IDEA 是一种流行的选择[^3]。 #### 创建新的 Spring Boot 项目 利用 Spring Initializr 来初始化一个新的 Spring Boot 工程。这可以通过访问 [Spring Initializr](https://start.spring.io/) 网站在线完成,或者直接在支持该功能的 IDE 中操作。选择 Maven 构建工具,并添加必要的依赖项,比如 `Spring Web`,以便能够处理 HTTP 请求和响应[^2]。 #### 配置 pom.xml 文件 项目的构建配置文件 `pom.xml` 将自动包含所需的库和插件。如果手动编辑此文件,则应确认包含了如下所示的关键部分: ```xml <dependencies> <!-- 其他可能存在的依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 测试框架 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> ``` #### 编写主应用类 创建名为 `Application.java` 的 Java 类作为应用程序入口点。此类应当标注有 `@SpringBootApplication` 注解来启用自动配置和其他特性。下面是一个典型的例子: ```java package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` #### 定义 REST 控制器 接下来定义一个控制器用于接收并回应来自客户端的请求。这里展示了一个返回字符串 `"Hello World"` 给用户的简单 GET 方法: ```java package com.example.demo.controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloWorldController { @GetMapping("/hello-world") public String sayHello() { return "Hello World"; } } ``` 启动应用程序之后,打开浏览器访问 URL `http://localhost:8080/hello-world` 即可看到页面上显示的文字:“Hello World”。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值