1、创建工程
- 创建一个普通的maven工程
-
导入相关依赖
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.4.3</version> </dependency> </dependencies>
2、项目编写
com.yusian.MainApplication.java
- 创建一个普通的类MainApplication
package com.yusian.boot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MainApplication {
public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args);
}
}
com.yusian.controller.HomeController
- 创建一个控制器
package com.yusian.boot.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HomeController {
@GetMapping("/home")
public String show() {
return "Hello SpringBoot!!";
}
}
3、测试
- MainApplication就是项目主类,执行main函数即可;
- 运行成功后在浏览器中输入http://localhost:8080/home 即可看到页面上显示的Hello SpringBoot!!