重生之 SpringBoot3 入门保姆级学习(01、Hello,SpringBoot3)

发布于:2024-05-21 ⋅ 阅读:(109) ⋅ 点赞:(0)

1、快速体验


场景:浏览器发送 /hello 请求,返回 “Hello,SpringBoot3”。

1.1 创建项目


  • 新建一个空项目

image-20240520162459597

  • 空项目新建模块

image-20240520162541262

  • 选择 17 以上的 JDK

image-20240520162624725

1.2 导入 SpringBoot3 依赖


  • 查看官网
https://docs.spring.io/spring-boot/docs/current/reference/html/getting-started.html#getting-started.installing

image-20240520162732547

  • 复制依赖到 pom.xml 里面并且刷新 maven

image-20240520162732547

1.3 导入 SpringBoot3 Web 依赖


  • 查看官网
https://docs.spring.io/spring-boot/docs/current/reference/html/getting-started.html#getting-started.first-application.dependencies.maven

image-20240521191349291

  • 复制依赖到 pom.xml 里面并且刷新 maven
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.zhong</groupId>
    <artifactId>boot3-01-demo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <!--springboot3 依赖-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.2.5</version>
    </parent>
    <!--springboot3 Web 依赖-->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
</project>

1.4 代码编写


  • 包名下新建 MainApplication

image-20240521193239021

  • 配置启动类
package com.zhong;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @ClassName : MainApplication
 * @Description : 启动 SpringBoot 项目的入口程序
 * @Author : zhx
 * @Date: 2024-05-20 16:20
 */
@SpringBootApplication  // 标识这是一个 SpringBoot 应用
public class MainApplication {
    public static void main(String[] args) {
        SpringApplication.run(MainApplication.class, args);
    }
}

image-20240521193127532

  • 编写相应接口
controller.HelloController

image-20240521193239021

image-20240521193519616

package com.zhong.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName : HelloController
 * @Description : 测试类
 * @Author : zhx
 * @Date: 2024-05-21 19:27
 */

@RestController     // 包含 @RequestBody 和 @Controller 标识这是一个请求接口合集
public class HelloController {
    @GetMapping("/hello")   // get 请求访问  http://localhost:8080/hello 即可得到 return 的值
    public String Hello() {
        return "Hello,SpringBoot3";
    }
}

image-20240521193909609

1.5 浏览器测试

image-20240521192956424