快速搭建一个SpringBoot应用


一、需求

搭建SpringBoot工程,定义HelloController.hello()方法,返回"hello world~"。

 

二、步骤

1、创建Maven项目

填写项目名,保存位置,及坐标

点击Finish,完成后,项目结构如下

  

2、导入SpringBoot起步依赖

在pom.xml文件中导入相关依赖



    org.springframework.boot
    spring-boot-starter-parent
    2.3.10.RELEASE



    
    
        org.springframework.boot
        spring-boot-starter-web
    

3、编写启动类

/**
 * 引导类(启动类)SpringBoot项目的入口
 */
@SpringBootApplication
public class HelloApplication {
    public static void main(String[] args) {
        SpringApplication.run(HelloApplication.class, args);
    }
}

4、定义HelloController类编写请求方法

package com.it.learn.controller;

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

@RestController
public class HelloController {

    @GetMapping("hello")
    public String hello(){
        return "hello world~";
    }

}

5、启动测试

找到启动类,运行main方法

看到如下,代表成功

 在浏览器访问 http://localhost:8080/hello,可以看到,返回信息