SpringBoot(1)Hello World

本文介绍如何使用SpringBoot快速搭建并实现一个简单的Web应用,包括创建项目、配置环境、编写控制器等步骤,并通过示例展示了基本的HTTP请求响应。

SpringBoot学习:先用springboot快速的实现一个HelloWorld。

我使用的是STS(Spring Tool Suite)。

首先,创建一个springboot项目。File->New->Spring Starter Project


下一步设置好项目的属性



WEB项目需要将WEB勾选,然后next->finish。下面是创建好的项目的目录结构:


SpringBootHelloWorldApplication.java是main方法,像普通JAVA类一样运行就行,不需要将项目部署到服务器中,使用spirngboot自带的容器。

SpringBootHelloWorldApplication.java


pom.xml中已经自动添加了spring-boot-starter-web即之前我们勾选的WEB。


自己写一个Controller,

package com.springboot.study.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/hello")
public class HelloController {
	
	@RequestMapping
	public String hello(){
		return "Hello SpringBoot!";
	}
	
	@RequestMapping("/info")
	public Map<String,String> getInfo(String name){
		Map<String,String> map=new HashMap<>();
		map.put("name", name);
		return map;
	}
	
	@RequestMapping("/list")
	public List<Map<String,String>> getList(){
		List<Map<String, String>> list = new ArrayList<>();
        Map<String, String> map = null;
        for (int i = 1; i <= 5; i++) {
            map = new HashMap<>();
            map.put("name", "mkdlp-" + i);
            list.add(map);
        }
        return list;
	}
}

现在我们只要运行SpringBootHelloWorldApplication.java中的main方法就能将它跑起来了,springboot中有WEB容器(默认为Tomcat)现在只需要用浏览器去访问就可以了。

http://localhost:8080/hello

http://localhost:8080/hello/info?name=mkdlp

http://localhost:8080/hello/list










### 使用 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、付费专栏及课程。

余额充值