准备
这里只说具体步骤,其他描述略过
打开网址:https://spring.io/projects/spring-boot
- 切换到 Learn选项卡,选择2.2.0版本的(第一个就是,他是当前最新的发布版本)
GA:General Availability,正式发布的版本,在国外都是用GA来说明release版本的。
版本意思参考:https://blog.youkuaiyun.com/linbrain0000/article/details/23872335
Reference Doc 参考文档
API Doc 应用程序接口文档,告诉你什么东西是干什么的(非常详细)
- 我们点开Reference Doc 参考文档(2.2.0版本的)
我们看到 Getting Started,右侧的描述,有 Installing Spring Boot(安装Spring Boot)
点击Getting Started
进入如下界面
看到 左侧的目录分别是:
- Spring Boot介绍
- 系统要求
- 安装 Spring Boot
- 部署你的第一个Spring Boot应用
- 下一个学什么
- 先去看系统要求
Maven 3.3+
Spring Framework 5.2.0.RELEASE
我们这里用的是maven所有不需要关注Gradle
Spring Boot 为我们提供了运行容器
Name | Servlet Version |
---|---|
Tomcat 9.0 | 4.0 |
Jetty 9.4 | 3.1 |
Undertow 2.0 | 4.0 |
更换不符合要求的环境
- 接着看maven安装(3.1.1)
告诉我们要在maven中引入以下
<!-- 在project标签下 -->
<!-- Inherit defaults from Spring Boot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.0.RELEASE</version>
</parent>
<!-- 在dependencies标签下 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
搭建
- 在eclipse里创建maven项目
选择webapp的模版,把maven Web项目约定的目录补全(四个),将jre换成工作空间中的
等待maven加载完依赖包(初次会下载很多依赖较慢,耐心等待,建议maven换成国内的仓库)
- 添加依赖
修改pom.xml,如下:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.oracle</groupId>
<artifactId>sxnd-springboot</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>sxnd-springboot Maven Webapp</name>
<url>http://maven.apache.org</url>
<!-- Inherit defaults from Spring Boot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.0.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.27</version>
</dependency>
</dependencies>
<build>
<finalName>sxnd-springboot</finalName>
</build>
</project>
- 建立我们需要的包和文件
src/main/java下
com.oracle
----------controller
----------entity
----------model
----------------dao
----------------service
-----------Application.java
其中Application是spring boot启动器
内容如下
package com.oracle;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
初步完成
-
小测试
package com.oracle.controller; import java.util.HashMap; import java.util.Map; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class TestController { @RequestMapping("test") public Map<String,String> Hello() { Map<String,String> map = new HashMap<>(); map.put("测试", "hello!"); return map; } }
新建如上文件
运行spring boot(在Application中右键启动)
浏览器中访问:localhost:8080/test
返回结果:
{"测试":"hello!"}
完毕