创建 spring boot 项目骨架
- 进入 https://start.spring.io 网页在线创建Web项目
- 工程类型选择
Maven Project
, 语言:Java
, JAVA版本:8
- 点击右上角选择依赖项。可输入
Web
选择Spring Web
- 下载zip文件,并解压到本地电脑。
修改代码
- Before
package my.whq.hellosp;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class HellospApplication {
public static void main(String[] args) {
SpringApplication.run(HellospApplication.class, args);
}
}
- After
package my.whq.hellosp;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class HellospApplication {
public static void main(String[] args) {
SpringApplication.run(HellospApplication.class, args);
}
@GetMapping("/hello")
public String hello(@RequestParam(value = "name", defaultValue = "World") String name) {
return String.format("Hello %s!", name);
}
}
添加依赖
前面网页在线创建Web项目时,如忘记选择 Spring Web
依赖,可接下来手动修改 pom.xml
文件:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
改为:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
运行
- Linux/MacOS:
./mvnw spring-boot:run
- Windows:
mvnw spring-boot:run
浏览器输入链接地址 http://localhost:8080/hello
即可访问。
SpringBoot 官方文档中文版 - 1. 入门指南 https://www.cnblogs.com/youcoding/p/15047719.html
https://spring.io/projects/spring-boot
https://spring.io/quickstart