SpringBoot - QuickStart
1. 创建项目
- File - New - Project…
- 选择 Spring Initializr -> Next
2. 项目结构
- 新建项目后,默认目录
3. 启动项目
- StartApplication - 主启动程序
package com.start;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class StartApplication {
public static void main(String[] args) {
SpringApplication.run(StartApplication.class, args);
}
}
-
使用启动类启动项目
-
(或)使用 IDEA 启动
-
启动成功(控制台输出)
- 默认端口 8080
-
浏览器访问(http://127.0.0.1:8080/ 或 http://localhost:8080/ 或 http://IP:8080/)
-
因为没有配置Controller,所以访问结果为 404
4. 第一个 Controller
- 创建目录:com/start/controller
- 创建Controller:StartController.java
package com.start.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class StartController {
@RequestMapping(value = "/hello/")
public Object sayHello() {
return "Hello, SpringBoot.";
}
}
- 再次启动项目,访问 http://localhost:8080/hello/