快速搭建一个SpringBoot应用
一、需求
搭建SpringBoot工程,定义HelloController.hello()方法,返回"hello world~"。
二、步骤
1、创建Maven项目
填写项目名,保存位置,及坐标

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

2、导入SpringBoot起步依赖
在pom.xml文件中导入相关依赖
<!-- SpringBoot工程需要继承父工程 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.10.RELEASE</version>
</parent>
<dependencies>
<!-- web开发的起步依赖:场景启动器依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
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,可以看到,返回信息


浙公网安备 33010602011771号